Setup
Install the Braintrust SDK and the Agent Framework integration, then set your API keys. The examples below use OpenAI.1
Install packages
2
Set environment variables
.env
Manual instrumentation
To trace the Agent Framework, add Braintrust’s middleware to your chat client and agent builder, then wrap the agent.UseBraintrustLLMTracing instruments the LLM calls on the ChatClientBuilder, UseBraintrustFunctionTracing instruments tool invocations on the agent builder, and WithBraintrustAgentTracing wraps the agent so each run is captured as a span.#skip-compile
UseBraintrustLLMTracing alone to trace only LLM calls, or UseBraintrustFunctionTracing alone to trace only tool invocations. UseBraintrustLLMTracing and WithBraintrustAgentTracing accept a captureMessageContent flag to omit message content from spans. UseBraintrustFunctionTracing accepts a captureToolArguments flag to omit function arguments and results.What Braintrust traces
Braintrust captures a span at each level of the Agent Framework pipeline:- Agent run spans (
agent:<name>), with input and output messages, agent name and ID, token usage (prompt, completion, and total), and time to first token. - Chat completion spans (
Chat CompletionandChat Completion Stream), with input and output messages, request and response model, and token usage (prompt, completion, and total). Streamed responses are aggregated and tagged withstream. - Function call spans (
function:<name>), with the function name, arguments, result, call iteration and index, and duration. - Errors on any span, including the exception message and details.
captureMessageContent: false to omit message content, or captureToolArguments: false to omit function arguments and results.