Claude Agent SDK
Trace Claude Agent SDK runs with cetra from Python and TypeScript, through Claude Code’s OpenTelemetry spans. The SDK runs Claude Code, and Claude Code exports the spans.
Install
Section titled “Install”pip install claude-agent-sdknpm install @anthropic-ai/claude-agent-sdkSet up
Section titled “Set up”Set the same environment variables as Claude Code. When your application starts a turn inside its own OpenTelemetry span, the SDK passes that span’s context to Claude Code, and the turn nests under your application’s span in one trace.
from claude_agent_sdk import ClaudeAgentOptions
options = ClaudeAgentOptions( env={ "CLAUDE_CODE_ENABLE_TELEMETRY": "1", "CLAUDE_CODE_ENHANCED_TELEMETRY_BETA": "1", "OTEL_TRACES_EXPORTER": "otlp", "OTEL_EXPORTER_OTLP_ENDPOINT": "<CETRA_URL>", "OTEL_EXPORTER_OTLP_HEADERS": "Authorization=Bearer%20<SK_CETRA_KEY>", },)The TypeScript SDK starts Claude Code the same way and reads the same variables.
export CLAUDE_CODE_ENABLE_TELEMETRY=1export CLAUDE_CODE_ENHANCED_TELEMETRY_BETA=1export OTEL_TRACES_EXPORTER=otlpexport OTEL_EXPORTER_OTLP_ENDPOINT=<CETRA_URL>export OTEL_EXPORTER_OTLP_HEADERS="Authorization=Bearer%20<SK_CETRA_KEY>"Replace <CETRA_URL> with your server’s URL and <SK_CETRA_KEY> with your sk-cetra-… key. These variables do not run the hooks of cetra agents setup, so a turn that starts this way has no branch from the hook.
What you get
Section titled “What you get”A turn started by the SDK is one trace, with the shape the Claude Code page describes:
- the turn as an
agentspan, with the prompt as its input when content is on; - each model call as an
llmspan, with the model and its token counts; - each tool call as a
toolspan named after the tool.
Limits
Section titled “Limits”- The captured spans are Claude Code’s, recorded from the Claude Code process that the SDK runs.
- The application’s own span (the parent of the Claude Code turn) is whatever your application’s tracing produces.
- Tested with Claude Code 2.1.323. Claude Code’s span names and attributes are in beta, and they can change between versions.
- If a field is missing, look at the span’s Attributes tab and compare it with Span attributes.
- Traces: read the trace as a tree and a timeline.
- Cost and tokens: how cost is computed from the token counts.
- Online rules: score these traces automatically.