OpenTelemetry instrumentation for Claude Agent SDK
Project description
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
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file traceai_claude_agent_sdk-0.1.0.tar.gz.
File metadata
- Download URL: traceai_claude_agent_sdk-0.1.0.tar.gz
- Upload date:
- Size: 20.1 kB
- Tags: Source
- Uploaded using 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}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7be6fcb638ad67fd57a3484eb618b66e7f5f8de1baceaea9b16db27035dffb4d
|
|
| MD5 |
3a33a1bd3fac68dd0bf49d937e3adcfc
|
|
| BLAKE2b-256 |
8a798034257a2591f6b997cac2f0eeed778802d9192cf6f6f6583ac47e452191
|
File details
Details for the file traceai_claude_agent_sdk-0.1.0-py3-none-any.whl.
File metadata
- Download URL: traceai_claude_agent_sdk-0.1.0-py3-none-any.whl
- Upload date:
- Size: 23.7 kB
- Tags: Python 3
- Uploaded using 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}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0c123cac2f45171f94045daecd8a56b3b465a58d25a4b5e722de0517bcc95235
|
|
| MD5 |
f3721049f2e2fde123fe15d72fc3c091
|
|
| BLAKE2b-256 |
a9503457bb1d071b7111ce3e1c3bb3454a4673001be59ee07942173101051673
|