OpenTelemetry Instrumentation for Anthropic
OpenTelemetry auto-instrumentation for the Anthropic Python SDK, tracing prompts and completions
Repository Health
Technical Analysis
opentelemetry-instrumentation-anthropic is part of Traceloop’s OpenLLMetry monorepo and adds OpenTelemetry tracing to the official Anthropic Python SDK. Calling AnthropicInstrumentor().instrument() monkey-patches the SDK’s client methods so every messages.create call (streaming or not) emits a span capturing the model name, prompt content, completion content, and token usage, following OpenTelemetry semantic conventions for generative AI. Spans are emitted through the standard OpenTelemetry SDK, so they flow into any OTLP-compatible backend (Traceloop, Jaeger, Honeycomb, Datadog, etc.) alongside the rest of an application’s traces, making Claude API calls visible in the same trace tree as your web framework, database, and queue spans.
What You Get
- Automatic span creation around Anthropic
messages.createcalls, both sync and streaming, via monkey-patching at import time - Prompt and completion content captured as span attributes/events, following OpenTelemetry’s generative-AI semantic conventions
- Token usage (input/output) recorded per call for cost and usage tracking alongside latency
- Works with the AWS Bedrock-backed Anthropic client (
anthropic[bedrock]) as well as the direct API client - A privacy toggle (
TRACELOOP_TRACE_CONTENT) to disable prompt/completion content capture when handling sensitive data
Common Use Cases
- Adding end-to-end observability to a Claude-powered backend so prompt latency and token cost show up next to HTTP and DB spans in the same trace
- Debugging why a specific Claude call in a larger agent pipeline was slow or returned an unexpected completion by inspecting its span
- Feeding LLM call traces into an existing OTLP observability stack (Grafana Tempo, Honeycomb, Datadog) without hand-instrumenting the Anthropic SDK
- Auditing token usage per request across a fleet of services that call Claude, for cost attribution
Under The Hood
Architecture - AnthropicInstrumentor (in opentelemetry/instrumentation/anthropic/__init__.py) extends OpenTelemetry’s BaseInstrumentor and wraps the Anthropic SDK’s client methods at instrument-time; streaming.py handles span lifecycle for streamed responses (spans stay open until the stream is exhausted), while span_utils.py and event_emitter.py/event_models.py translate SDK request/response objects into span attributes and semantic-convention-compliant events. Tech Stack - Python 3.10+, built on opentelemetry-api, opentelemetry-instrumentation, and Traceloop’s opentelemetry-semantic-conventions-ai package, with the anthropic SDK itself only required as an optional (instruments) dependency so this package can be installed without pulling in Anthropic’s client. Code Quality - The package ships 12 test files under tests/, using pytest-recording/VCR-style cassettes to replay real Anthropic API responses so tests don’t need live API calls; ruff and autopep8 enforce style. API Design - Following the standard OpenTelemetry auto-instrumentation pattern, integration is a single AnthropicInstrumentor().instrument() call at process startup with zero changes to call sites, matching the same shape used across every other OpenLLMetry instrumentation package (Bedrock, LangChain, VertexAI, etc.), so teams instrumenting multiple LLM providers reuse one mental model.
Used by 3 apps in this directory
Helicone
Monitoring · AI Development · Analytics
An open-source AI gateway and LLM observability platform that routes requests to 100+ models while logging cost, latency, and full traces for every call.
highlight.io
Developer Tools · Analytics · Monitoring
Open-source full-stack monitoring that unifies session replay, error tracking, logging, and distributed tracing so you can stop context-switching between tools.
Latitude
AI Agents · Monitoring
Open-source AI agent monitoring that catches what will break next before your users do.