Skip to main content
Pydantic AI is a Python agent framework built on Pydantic. Braintrust traces Pydantic AI agents, including agent runs, the underlying model calls, and tool calls.

Setup

Install the Braintrust SDK and Pydantic AI:
Tracing Pydantic AI 2.0 or later requires the Braintrust Python SDK v0.26.0 or later.

Auto-instrumentation

To trace Pydantic AI without modifying each agent call, call init_logger() and auto_instrument() once at startup. auto_instrument() patches Pydantic AI’s agent and model APIs along with every other supported library.
To patch Pydantic AI without enabling Braintrust’s other integrations, call setup_pydantic_ai() instead. It initializes a logger for you when one isn’t already active.

Tools and streaming

Tools and streaming responses are traced without extra configuration:

Nest under existing spans

If you already have a Braintrust span context (e.g., from @traced or start_span), Pydantic AI traces nest under it:

OpenTelemetry

To trace Pydantic AI through an existing OpenTelemetry pipeline, attach a BraintrustSpanProcessor and enable Pydantic AI’s built-in instrumentation.
1

Install packages

Install the Braintrust SDK with the OpenTelemetry extra, alongside Pydantic AI:
2

Configure tracing

Register the BraintrustSpanProcessor with your tracer provider, then call Agent.instrument_all() to emit Pydantic AI’s instrumentation to Braintrust:

What Braintrust traces

Braintrust captures:
  • Agent run spans (agent_run, agent_run_sync, agent_run_stream, agent_run_stream_sync, agent_to_cli_sync), with the run input (including system_prompt, instructions, and message_history when present), agent metadata, output, and timing. The model and provider in the metadata are those of the model that produced the final response, so a fallback model is reported when it answers. When you pass a conversation, the input records its conversation_id and its messages as message_history. With Python SDK v0.54.0 or later:
    • Runs that you iterate with Agent.iter() create an agent_run span that parents the run’s model and tool calls.
    • When Pydantic AI Harness step persistence resolves a run ID, metadata.step_persistence records the run_id, plus the parent_run_id of the source run for a restored or forked run.
  • Direct model-request spans (model_request, model_request_sync), for pydantic_ai.direct API calls.
  • Model call spans (chat <model>), for the underlying LLM calls made during a run, including each fallback model attempt after a failed request, with invocation parameters (temperature, max_tokens, and similar model settings when provided) and tools in metadata. completion_reasoning_tokens is captured when the model produces reasoning tokens (Anthropic extended thinking, Google thinking). Audio duration is recorded in metadata.pydantic_ai_usage.audio_seconds and native web search request counts in metadata.pydantic_ai_usage.web_search_requests when the model reports them.
  • Tool call spans, named after the tool, for tools invoked during an agent run, with the tool arguments, result, and tool_call_id. When a tool defers its call by raising CallDeferred or ApprovalRequired, the span records the deferral as deferred metadata (external, unapproved, or question) rather than as an error. With Python SDK v0.54.0 or later:
    • metadata.tool_outcome is executed for a tool that ran, deferred for a deferred call, or reconstructed for a span built from the run’s messages after the run.
    • metadata.tool_approval is approved or denied for a call that went through tool approval. A tool that requires approval gets a span once it runs or is denied.
    • Pydantic AI Harness background tool spans cover the tool handler’s run and record its return value or error. Agent runs that the handler starts nest under the tool span.
    • Structured output and external tools don’t create tool spans.
  • Pydantic AI Harness decision spans, as task spans in the agent run when Pydantic AI Harness is installed (Python SDK v0.54.0 or later, with Pydantic AI v2.75 or later):
    • Message compaction spans (harness_message_compaction), with the message count before and after compaction and the compaction strategy.
    • Memory injection spans (harness_memory_injection), with memory sizes, truncation, and the number of characters injected, without the memory text.
    • Guardrail block spans (harness_guardrail_block), with the block direction and, for a tool guardrail, the tool name.
    • Model selection spans (harness_model_selection), with the model choices, default, selected model, and fallback reason.

Resources