OpenInference Claude Agent SDK Instrumentation
Project description
OpenInference Claude Agent SDK Instrumentation
Python auto-instrumentation for the Claude Agent SDK (Python). Traces query() and ClaudeSDKClient as OpenInference AGENT spans with prompt input, result output, session/model metadata, token counts, and tool child spans via hook injection.
query()– One span per call (one-off sessions).ClaudeSDKClient– One span per response turn: each time you iteratereceive_response()(orreceive_messages()), a span is created for that turn. Use for continuous conversations.- Tools – Tool calls are captured as child TOOL spans via Claude Agent SDK hooks (PreToolUse/PostToolUse/PostToolUseFailure).
For detailed LLM and tool spans inside agent runs, use openinference-instrumentation-anthropic together with this package; the Agent SDK uses the Anthropic API under the hood.
Traces are OpenTelemetry-compatible and can be sent to any OTLP collector, Arize Phoenix (local), or Phoenix Cloud.
Installation
pip install openinference-instrumentation-claude-agent-sdk
Quickstart
pip install openinference-instrumentation-claude-agent-sdk claude-agent-sdk arize-phoenix opentelemetry-sdk opentelemetry-exporter-otlp
Option A – Phoenix Cloud: Create a free Phoenix Cloud account, create a space, and set PHOENIX_COLLECTOR_ENDPOINT and PHOENIX_API_KEY. Use your collector endpoint (e.g. https://<host>/v1/traces) as endpoint below.
Option B – Local Phoenix: Start Phoenix, then run your script:
python -m phoenix.server.main serve
Then in Python:
import asyncio
import os
from claude_agent_sdk import query, ClaudeAgentOptions
from openinference.instrumentation.claude_agent_sdk import ClaudeAgentSDKInstrumentor
from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter
from opentelemetry.sdk import trace as trace_sdk
from opentelemetry.sdk.trace.export import SimpleSpanProcessor
# Phoenix Cloud: set PHOENIX_COLLECTOR_ENDPOINT (and PHOENIX_API_KEY for auth). Else local.
endpoint = os.environ.get("PHOENIX_COLLECTOR_ENDPOINT", "http://127.0.0.1:6006/v1/traces")
tracer_provider = trace_sdk.TracerProvider()
tracer_provider.add_span_processor(SimpleSpanProcessor(OTLPSpanExporter(endpoint)))
ClaudeAgentSDKInstrumentor().instrument(tracer_provider=tracer_provider)
async def main():
async for message in query(
prompt="What files are in this directory?",
options=ClaudeAgentOptions(allowed_tools=["Bash", "Glob"]),
):
if hasattr(message, "result"):
print(message.result)
asyncio.run(main())
View traces in Phoenix Cloud or at http://localhost:6006 when running Phoenix locally.
Examples
Run the examples in this repo:
pip install -r examples/requirements.txt
export ANTHROPIC_API_KEY=your-key
python examples/basic_query.py
| Example | Description |
|---|---|
| basic_query.py | Simple query() with OTLP export to Phoenix |
| query_with_tools.py | query() with ClaudeAgentOptions and tools (Bash, Glob) |
| client_basic.py | ClaudeSDKClient: one turn (query + receive_response) |
| client_multi_turn.py | ClaudeSDKClient: multi-turn conversation |
| query_with_phoenix.py | In-process Phoenix via phoenix.otel.register() (works with Phoenix Cloud or local; requires arize-phoenix) |
See examples/README.md for details.
What is instrumented
-
query()– Each call is wrapped in a single AGENT span namedClaudeAgentSDK.querywith:- Input: prompt text or JSON (for async message iterables)
- Output: result text/JSON from the SDK result message
- Metadata:
session.id,llm.model_name, token counts, andllm.cost.totalwhen available - Tools: TOOL child spans created via SDK hooks
-
ClaudeSDKClient– For multi-turn conversations:connect(prompt=...)andquery(prompt)record the prompt for the next response.- Each
receive_response()iteration is wrapped in an AGENT span namedClaudeAgentSDK.ClaudeSDKClient.receive_responsewith the same input/output/metadata/tool spans as above.
Child LLM/tool spans (from the SDK’s internal Anthropic usage) are not created by this package; add openinference-instrumentation-anthropic and instrument Anthropic for that.
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 openinference_instrumentation_claude_agent_sdk-0.1.0.tar.gz.
File metadata
- Download URL: openinference_instrumentation_claude_agent_sdk-0.1.0.tar.gz
- Upload date:
- Size: 14.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
468006742000e874031ad834daadbd4d63d814433033728cb82fcccdb191e7b7
|
|
| MD5 |
e038e8d3ba9f1b677c2bb105966917b7
|
|
| BLAKE2b-256 |
d6c57dbbb74749bb1a6aa74abdc0eec97f56ebd0fa1e3091941abebd56c3723b
|
File details
Details for the file openinference_instrumentation_claude_agent_sdk-0.1.0-py3-none-any.whl.
File metadata
- Download URL: openinference_instrumentation_claude_agent_sdk-0.1.0-py3-none-any.whl
- Upload date:
- Size: 16.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1338abd5da7ca5cd7bd69d007e10eb3ad18523074603663f628e83bb2b937c85
|
|
| MD5 |
98ccc97cd944b7f3d3da3e0f68447169
|
|
| BLAKE2b-256 |
2078b17d02b2238a54d5838cba6cf784a9e2900b5ac1e775285037db5bb78839
|