Skip to main content

LoongSuite Claude Agent SDK instrumentation

Project description

This library provides automatic instrumentation for the Claude Agent SDK, adding OpenTelemetry tracing and metrics for agent conversations, LLM calls, and tool executions.

Installation

pip install opentelemetry-distro opentelemetry-exporter-otlp
opentelemetry-bootstrap -a install

pip install claude-agent-sdk

# Install this instrumentation
pip install ./instrumentation-loongsuite/loongsuite-instrumentation-claude-agent-sdk

# Note: This instrumentation uses ExtendedTelemetryHandler from opentelemetry-util-genai
pip install ./util/opentelemetry-util-genai

Usage

Auto-instrumentation

Use the opentelemetry-instrument wrapper:

opentelemetry-instrument \
    --traces_exporter console \
    --metrics_exporter console \
    python your_claude_agent_app.py

Manual Instrumentation

from opentelemetry.instrumentation.claude_agent_sdk import ClaudeAgentSDKInstrumentor
from claude_agent_sdk import query
from claude_agent_sdk.types import ClaudeAgentOptions

ClaudeAgentSDKInstrumentor().instrument()

options = ClaudeAgentOptions(model="claude-3-5-sonnet-20241022", max_turns=5)

async def run_agent():
    async for message in query(prompt="Hello!", options=options):
        print(message)

ClaudeAgentSDKInstrumentor().uninstrument()

Configuration

Export to OTLP Backend

export OTEL_SERVICE_NAME=my-claude-agent-app
export OTEL_EXPORTER_OTLP_PROTOCOL=http/protobuf
export OTEL_EXPORTER_OTLP_TRACES_ENDPOINT=<trace_endpoint>
export OTEL_EXPORTER_OTLP_METRICS_ENDPOINT=<metrics_endpoint>

opentelemetry-instrument python your_app.py

Content Capture

Control message content capture using environment variables:

# Enable experimental GenAI semantic conventions
export OTEL_SEMCONV_STABILITY_OPT_IN=gen_ai_latest_experimental

# Capture content in spans only
export OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT=SPAN_ONLY

# Capture content in events only
export OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT=EVENT_ONLY

# Capture in both spans and events
export OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT=SPAN_AND_EVENT

# Disable content capture (default)
export OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT=NO_CONTENT

Using with Alibaba Cloud DashScope

This instrumentation works with Alibaba Cloud’s DashScope service via the Anthropic-compatible API endpoint:

import os

# Set environment variables for DashScope
os.environ["ANTHROPIC_BASE_URL"] = "https://dashscope.aliyuncs.com/apps/anthropic"
os.environ["ANTHROPIC_API_KEY"] = "your-dashscope-api-key"

Supported Components

  • Agent Sessions: query function for conversational agent interactions

  • Tool Executions: Automatic tracing via PreToolUse/PostToolUse hooks

  • Token Tracking: Via MessageComplete hook

  • Context Compaction: Via PreCompact hook

Visualization

Export telemetry data to:

Span Hierarchy

invoke_agent (parent span)
  ├── User prompt event
  ├── execute_tool (child span)
  │   ├── gen_ai.tool.input.* attributes
  │   └── gen_ai.tool.response.* attributes
  ├── execute_tool (child span)
  │   └── ...
  └── Agent completed event

Examples

See the main README for complete usage examples.

License

Apache License 2.0

References

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

File details

Details for the file loongsuite_instrumentation_claude_agent_sdk-0.6.0-py3-none-any.whl.

File metadata

File hashes

Hashes for loongsuite_instrumentation_claude_agent_sdk-0.6.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d3bc8ceaadfbcfc7d97bbcbb148d28dcec681e0cf122c51411ba2f600f53b43b
MD5 cd48680fb41cf068e1b1489470423f1a
BLAKE2b-256 e494e1d1201f277d4fc1d1a891efcd18c7fbae3b07581b1df7b4800448d4813e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page