mirror of https://github.com/buster-so/buster.git
3.2 KiB
3.2 KiB
Braintrust Library - Agent Guidance
Purpose & Role
The Braintrust library provides a client for interacting with the Braintrust API, enabling the tracking and logging of AI application performance through spans and traces. It serves as the monitoring and observability layer for LLM interactions.
Key Functionality
- Braintrust API client for logging AI application performance
- Span and trace creation for measuring LLM operations
- Event and metrics tracking
- Prompt and response logging
- Structured metadata collection for AI operations
Internal Organization
Directory Structure
src/
├── client.rs - BraintrustClient implementation
├── types.rs - Core data structures for API interactions
├── trace.rs - TraceBuilder for creating trace spans
├── helpers.rs - Utility functions and helpers
└── lib.rs - Public exports and documentation
Key Modules
client
: Implements theBraintrustClient
for API communicationtypes
: Defines data structures likeSpan
,Metrics
, andEventPayload
trace
: Provides theTraceBuilder
for creating and managing trace spanshelpers
: Utility functions for working with the Braintrust API
Usage Patterns
use braintrust::{BraintrustClient, TraceBuilder};
async fn example_usage() -> Result<(), anyhow::Error> {
// Create a client
let client = BraintrustClient::new("YOUR_API_KEY").await?;
// Create a trace
let trace = TraceBuilder::new("example-trace")
.with_metadata("user_id", "123")
.build();
// Log a span with the client
client.log_span(trace, "llm-call", |span| {
// Your LLM call logic here
span.with_input("What is the capital of France?")
.with_output("Paris is the capital of France.")
}).await?;
Ok(())
}
Common Implementation Patterns
- Initialize the
BraintrustClient
once and reuse it throughout the application - Create traces for logical units of work (e.g., a chat session)
- Use spans to measure individual operations within a trace (e.g., an LLM call)
- Add metadata to traces and spans for better filtering and analysis
- Properly handle errors from the Braintrust API
Dependencies
-
Internal Dependencies:
- None - this is a standalone library that other libraries depend on
-
External Dependencies:
reqwest
: For making HTTP requests to the Braintrust APIserde_json
: For JSON serialization/deserializationtokio
: For async runtimeuuid
: For generating unique IDschrono
: For timestamp handling
Code Navigation Tips
- Start with
lib.rs
to see the exported API - The
BraintrustClient
inclient.rs
is the main entry point for usage types.rs
contains all the data structures used for API interactionstrace.rs
contains theTraceBuilder
for creating traces- When adding new metrics or events, extend the existing types in
types.rs
Testing Guidelines
- Mock the HTTP responses in unit tests using
mockito
- Test error handling by simulating API errors
- Validate the correct structure of API requests in tests
- Run tests with:
cargo test -p braintrust
- Use the examples in the
examples/
directory as reference implementations