This library allows tracing LLM requests made by the Anthropic Python SDK.
pip install opentelemetry-instrumentation-genai-anthropic
If you don't have an Anthropic application yet, try our examples which only need a valid Anthropic API key.
Check out the zero-code example for a quick start.
This section describes how to set up Anthropic instrumentation if you're setting OpenTelemetry up manually. Check out the manual example for more details.
from opentelemetry.instrumentation.genai.anthropic import AnthropicInstrumentor
import anthropic
# Instrument Anthropic
AnthropicInstrumentor().instrument()
# Use Anthropic client as normal
client = anthropic.Anthropic()
response = client.messages.create(
model="claude-3-5-sonnet-20241022",
max_tokens=1024,
messages=[
{"role": "user", "content": "Hello, Claude!"}
]
)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
Instead of recording message content inline, prompts and completions can be uploaded to external storage via a completion hook. To enable the built-in upload hook, set:
OTEL_INSTRUMENTATION_GENAI_COMPLETION_HOOK=uploadOTEL_INSTRUMENTATION_GENAI_UPLOAD_BASE_PATHto anfsspec-compatible URI/path (e.g./path/to/promptsorgs://my_bucket), and install theuploadextra (pip install opentelemetry-util-genai[upload]).
A custom CompletionHook can also be passed programmatically, taking precedence over the
environment variable:
AnthropicInstrumentor().instrument(completion_hook=my_hook)