Skip to main content
The Claude Agent SDK is Anthropic’s official SDK for building production-ready AI agents with Claude. Braintrust traces Claude Agent SDK runs end-to-end, covering queries, tool executions, and multi-step reasoning.

Setup

Install the Braintrust SDK alongside the Claude Agent SDK:

Auto-instrumentation

To trace the Claude Agent SDK without wrapping it in your application code, run your app with Braintrust’s import hook. The hook patches query on import, so agent queries, tool calls, and sub-agent runs are traced while you use the SDK as normal.
1

Initialize Braintrust and run your agent

trace-claude-agent-auto.js
2

Run with the import hook

The auto-instrumentation example uses plain JavaScript so node --import can run the file directly. The Braintrust APIs work the same in TypeScript projects — compile your TypeScript to JavaScript, then run the compiled file with the import hook.
If you’re using a bundler, see Trace LLM calls for plugin and loader setup.

Manual instrumentation

To trace the Claude Agent SDK explicitly, wrap the SDK module with wrapClaudeAgentSDK and use the returned query, tool, and createSdkMcpServer functions. This example creates a calculator tool and traces a multi-step agent query.

Trace nested agent operations

To organize agent calls into parent-child traces, wrap coordinating functions with wrapTraced. Braintrust captures the hierarchical relationships, making multi-step workflows easy to follow in your logs.
nested-agents.ts

What Braintrust traces

Braintrust captures the Claude Agent SDK span tree for each query:
  • Agent run spans (Claude Agent), with the prompt as input and the query options as metadata.
  • Model call spans (anthropic.messages.create), with input messages, model, and assistant output; token metrics including prompt, completion, and cache read and creation tokens.
  • Tool call spans (named by the tool, such as calculator), with tool input and output, tool name, tool call ID, and MCP server.
  • Sub-agent spans (named by the sub-agent), with sub-agent metadata and token usage.
  • Parent-child nesting under any enclosing Braintrust span.

Resources