Skip to content

Latest commit

 

History

History

README.rst

OpenTelemetry Claude Agent SDK Instrumentation

pypi

This library allows tracing LLM requests made by the Claude Agent SDK.

Installation

pip install opentelemetry-instrumentation-genai-claude-agent-sdk

If you don't have a Claude Agent SDK application yet, try our examples which only need a valid Anthropic API key.

Check out the zero-code example for a quick start.

Usage

This section describes how to set up Claude Agent SDK instrumentation if you're setting OpenTelemetry up manually. Check out the manual example for more details.

from opentelemetry.instrumentation.genai.claude_agent_sdk import ClaudeAgentSDKInstrumentor
from claude_agent_sdk import ClaudeAgentOptions, AgentDefinition, AssistantMessage, TextBlock, query

# Instrument Claude Agent SDK
ClaudeAgentSDKInstrumentor().instrument()

# Use Claude Agent SDK as normal
import anyio

async def main():
    options = ClaudeAgentOptions(
        agents={
            "assistant": AgentDefinition(
                description="A helpful assistant",
                prompt="You are a helpful assistant.",
                tools=["Read"],
                model="sonnet",
            ),
        },
    )

    async for message in query(
        prompt="Hello, Claude!",
        options=options,
    ):
        if isinstance(message, AssistantMessage):
            for block in message.content:
                if isinstance(block, TextBlock):
                    print(block.text)

anyio.run(main)

Configuration

Capture Message Content

By default, prompts and completions are not captured. To capture message content, set the environment variable OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT to one of NO_CONTENT, SPAN_ONLY, EVENT_ONLY, or SPAN_AND_EVENT:

export OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT=SPAN_AND_EVENT

References