OpenTelemetry Instrumentation for Anthropic

OpenTelemetry auto-instrumentation for the Anthropic Python SDK, tracing prompts and completions

Library
PyPI
v0.62.3
7,383stars
Apache License 2.0

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum.How we score it →
81/100Excellent
Development Activity76
Maintenance88
Community72
Maturity48
Momentum40

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation.How we score it →
76/100Good
Architecture78
Code Quality75
Innovation70
Learning Curve82

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.create calls, 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.

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers

Search