> ## Documentation Index
> Fetch the complete documentation index at: https://traceroot.ai/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Claude Agent SDK

> Auto-instrument the Claude Agent SDK for agent and tool tracing

Automatically capture agent invocations, subagent delegations, tool calls, and token usage from the Claude Agent SDK (Claude Code as a library).

## Install

> [tab] Python
>
> ```bash theme={null}
> pip install traceroot claude-agent-sdk
> ```

> [tab] TypeScript
>
> ```bash theme={null}
> npm install @traceroot-ai/traceroot @anthropic-ai/claude-agent-sdk
> ```

## Set up

> [tab] Python
>
> ```python theme={null}
> import traceroot
> from traceroot import Integration
>
> traceroot.initialize(integrations=[Integration.CLAUDE_AGENT_SDK])
> ```

> [tab] TypeScript
>
> ```typescript theme={null}
> import * as claudeAgentSDK from '@anthropic-ai/claude-agent-sdk';
> import { TraceRoot } from '@traceroot-ai/traceroot';
>
> TraceRoot.initialize({
>   instrumentModules: { claudeAgentSDK },
> });
> ```

## Usage

> [tab] Python
>
> ```python theme={null}
> from claude_agent_sdk import query, ClaudeAgentOptions, AgentDefinition
>
> # Define subagents
> researcher = AgentDefinition(
>     description="Research specialist for gathering information.",
>     prompt="You are a research specialist. Use WebSearch to find info.",
>     tools=["WebSearch"],
>     model="haiku",
> )
>
> # Run the agent with subagents
> async for message in query(
>     prompt="Research the latest trends in AI observability",
>     options=ClaudeAgentOptions(
>         allowed_tools=["Agent", "Bash", "Read", "Glob", "Grep"],
>         agents={"researcher": researcher},
>     ),
> ):
>     if hasattr(message, "result"):
>         print(message.result)
> ```

> [tab] TypeScript
>
> ```typescript theme={null}
> import { query, AgentDefinition } from '@anthropic-ai/claude-agent-sdk';
>
> const researcher: AgentDefinition = {
>   description: 'Research specialist for gathering information.',
>   prompt: 'You are a research specialist. Use WebSearch to find info.',
>   tools: ['WebSearch'],
>   model: 'haiku',
> };
>
> async function main() {
>   for await (const message of query({
>     prompt: 'Research the latest trends in AI observability',
>     options: {
>       allowedTools: ['Agent', 'Bash', 'Read', 'Glob', 'Grep'],
>       agents: { researcher },
>     },
>   })) {
>     if ('result' in message) {
>       console.log(message.result);
>     }
>   }
> }
> main().catch(console.error);
> ```

## Verify

Run your code and confirm the trace arrives:

1. **In the traceroot UI**: Navigate to **Tracing > Traces** in [traceroot Cloud](https://app.traceroot.ai). You should see a trace with root span `ClaudeAgent.query`.
2. **Via the CLI**: Run `traceroot traces list` to inspect your recent traces from the terminal.

## What gets captured

| Attribute | Description |
| - | - |
| Agent queries | Each `query()` call as a top-level agent span |
| Subagent invocations | Each subagent delegation via the Agent tool |
| Tool calls | Built-in tools (Read, Write, Bash, Glob, Grep, WebSearch) |
| Token usage | Aggregated input/output tokens per query |
| Cost | Aggregated when the model ID matches traceroot's pricing table |
| Model name | The model used (e.g., claude-sonnet-4-6, claude-haiku-4-5) |

## Limitations

None known.

## Run the example

Clone the repo and run a complete agent end-to-end.

> [card] [Python](https://github.com/traceroot-ai/traceroot/tree/main/examples/python/claude-agent-sdk)
>
> Run the Python example

> [card] [TypeScript](https://github.com/traceroot-ai/traceroot/tree/main/examples/typescript/claude-agent-sdk)
>
> Run the TypeScript example

## Next steps

> [card] [Python SDK Reference](https://traceroot.ai/docs/tracing/python-sdk.md)
>
> Explore the full Python SDK reference and instrumentation options.

> [card] [TypeScript SDK Reference](https://traceroot.ai/docs/tracing/typescript-sdk.md)
>
> Explore the full TypeScript SDK reference and configuration options.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.