TraceAI Claude Agent SDK Instrumentation
OpenTelemetry instrumentation for the Claude Agent SDK (formerly Claude Code SDK).
Installation
pip install traceai-claude-agent-sdk
Quick Start
from traceai_claude_agent_sdk import ClaudeAgentInstrumentor
# Initialize instrumentation
ClaudeAgentInstrumentor().instrument()
# Use Claude Agent SDK as normal - tracing is automatic
import asyncio
from claude_agent_sdk import query, ClaudeAgentOptions
async def main():
async for message in query(
prompt="What files are in this directory?",
options=ClaudeAgentOptions(allowed_tools=["Glob", "Read"])
):
print(message)
asyncio.run(main())
Features
Comprehensive Tracing
- Conversation Spans: Root spans for each query/conversation
- Assistant Turn Spans: Individual assistant response turns
- Tool Execution Spans: Every tool call (built-in, MCP, custom)
- Subagent Spans: Task tool and subagent coordination
- Session Tracking: Resume and fork operations
Span Kinds
| Kind | Description |
|---|---|
conversation |
Root span for a query/conversation |
assistant_turn |
Individual assistant response turn |
tool_execution |
Tool invocation |
subagent |
Subagent (Task tool) execution |
mcp_tool |
MCP server tool call |
Semantic Attributes
The instrumentation captures comprehensive attributes:
Conversation Level:
claude_agent.prompt- User promptclaude_agent.model- Model used (claude-3-opus, etc.)claude_agent.session_id- Session identifierclaude_agent.permission_mode- Permission mode setting
Tool Level:
claude_agent.tool.name- Tool nameclaude_agent.tool.use_id- Unique tool use IDclaude_agent.tool.input- Tool input parametersclaude_agent.tool.output- Tool resultclaude_agent.tool.source- "builtin", "mcp", or "custom"
Usage/Cost:
gen_ai.usage.input_tokens- Input token countgen_ai.usage.output_tokens- Output token countclaude_agent.cost.total_usd- Total cost in USD
Configuration
With Custom Tracer Provider
from opentelemetry import trace
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import ConsoleSpanExporter, BatchSpanProcessor
# Set up custom provider
provider = TracerProvider()
provider.add_span_processor(BatchSpanProcessor(ConsoleSpanExporter()))
trace.set_tracer_provider(provider)
# Instrument with custom provider
from traceai_claude_agent_sdk import ClaudeAgentInstrumentor
ClaudeAgentInstrumentor().instrument(tracer_provider=provider)
Using Convenience Function
from traceai_claude_agent_sdk import instrument_claude_agent_sdk
instrumentor = instrument_claude_agent_sdk()
Subagent Tracing
The instrumentation automatically tracks subagent (Task tool) executions with proper parent-child span relationships:
# Subagents are traced automatically when using Task tool
# Span hierarchy:
# claude_agent.conversation (root)
# └── claude_agent.assistant_turn
# └── claude_agent.tool (Task)
# └── claude_agent.subagent.Explore
# └── claude_agent.tool (Read)
Features:
- Nested subagent tracking - Full hierarchy preserved
- Cost aggregation - Costs roll up through the hierarchy
- Parent-child linking -
parent_tool_use_idfor correlation
Session Tracking
Session continuity is tracked across queries with resume and fork support:
# Session tracking happens automatically
# Attributes captured:
# - claude_agent.session.id
# - claude_agent.session.is_new
# - claude_agent.session.is_resumed
# - claude_agent.session.previous_id (for resumed sessions)
# - claude_agent.session.fork_from (for forked sessions)
Features:
- Resume linking - Trace links between resumed sessions
- Fork tracking - Branches tracked with source reference
- Metrics carryover - Aggregated metrics across session chain
MCP Server Support
MCP (Model Context Protocol) server tools are automatically traced:
from claude_agent_sdk import ClaudeSDKClient, ClaudeAgentOptions
options = ClaudeAgentOptions(
mcp_servers={
"playwright": {"command": "npx", "args": ["-y", "@anthropic/mcp-playwright"]},
}
)
# MCP tools will have span kind "mcp_tool" and include server attribution
# Attributes:
# - claude_agent.mcp.server_name
# - claude_agent.mcp.server_command
# - claude_agent.mcp.tool_name (parsed from mcp__server__tool format)
Development
Running Tests
cd python/frameworks/claude-agent-sdk
python -m venv .venv
source .venv/bin/activate
pip install -e ".[dev]"
pytest tests/ -v
Test Coverage
- 141 unit tests covering:
- Semantic attributes and span kinds (21 tests)
- Hook injection and tool tracing (17 tests)
- Client wrapper functionality (32 tests)
- Instrumentor lifecycle (16 tests)
- Subagent tracking (20 tests)
- Session tracking (17 tests)
- MCP server tracking (18 tests)
License
MIT
Metadata
Release files for traceai-claude-agent-sdk 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| traceai_claude_agent_sdk-0.1.0.tar.gz | 20.1 kB | Without provenance | Details for traceai_claude_agent_sdk-0.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| traceai_claude_agent_sdk-0.1.0-py3-none-any.whl | Python 3 | none | any | Details for traceai_claude_agent_sdk-0.1.0-py3-none-any.whl |
Total release size: 43.8 kB
Release files / traceai_claude_agent_sdk-0.1.0.tar.gz
| Download URL | traceai_claude_agent_sdk-0.1.0.tar.gz |
|---|---|
| Size | 20.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
7be6fcb638ad67fd57a3484eb618b66e7f5f8de1baceaea9b16db27035dffb4d
|
|
BLAKE2b-256 checksum How to use checksums |
8a798034257a2591f6b997cac2f0eeed778802d9192cf6f6f6583ac47e452191
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.9.21 {"installer":{"name":"uv","version":"0.9.21","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
|
Release files / traceai_claude_agent_sdk-0.1.0-py3-none-any.whl
| Download URL | traceai_claude_agent_sdk-0.1.0-py3-none-any.whl |
|---|---|
| Size | 23.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
0c123cac2f45171f94045daecd8a56b3b465a58d25a4b5e722de0517bcc95235
|
|
BLAKE2b-256 checksum How to use checksums |
a9503457bb1d071b7111ce3e1c3bb3454a4673001be59ee07942173101051673
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.9.21 {"installer":{"name":"uv","version":"0.9.21","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
|
Security for traceai-claude-agent-sdk 0.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.