Registry / observability / opentelemetry-instrumentation-anthropic

opentelemetry-instrumentation-anthropic

JSON →
library0.62.3pypypi✓ verified 23d ago

This library provides OpenTelemetry instrumentation for the Anthropic Python client library, enabling automatic tracing of Anthropic API calls. It captures prompts, completions, and other relevant metadata as spans, conforming to OpenTelemetry's Generative AI semantic conventions. The library is actively maintained with frequent releases.

pip install opentelemetry-instrumentation-anthropic anthropic opentelemetry-sdk opentelemetry-exporter-otlp
INSTALL
IMPORT
SIG · OPENTELEMETRY-INST
O
opentelemetry-instrumentation-anthropic
observabilitypythonv0.62.3
Install
8.8s avg
Import
2762ms
Disk
80MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.62.3 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 2.922s · 82.4MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 8.8s · import 2.602s · 79MB
80MB installed
● package 80MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

AnthropicInstrumentor
from opentelemetry.instrumentation.anthropic import AnthropicInstrumentor

This quickstart demonstrates how to set up OpenTelemetry with the Anthropic instrumentation, configure an OTLP HTTP exporter, and make a traced Anthropic API call. Ensure `ANTHROPIC_API_KEY` is set in your environment. An OpenTelemetry Collector or compatible backend should be running to receive traces.

import os from anthropic import Anthropic from opentelemetry.instrumentation.anthropic import AnthropicInstrumentor from opentelemetry import trace from opentelemetry.sdk.resources import Resource from opentelemetry.sdk.trace import TracerProvider from opentelemetry.sdk.trace.export import BatchSpanProcessor from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter # --- OpenTelemetry Setup --- # 1. Configure the OpenTelemetry TracerProvider resource = Resource.create({"service.name": "anthropic-llm-app"}) provider = TracerProvider(resource=resource) trace.set_tracer_provider(provider) # 2. Configure an OTLP exporter to send traces (e.g., to an OTLP collector or a service like SigNoz/Arize) # Default OTLP HTTP endpoint is http://localhost:4318/v1/traces otlp_exporter = OTLPSpanExporter() span_processor = BatchSpanProcessor(otlp_exporter) provider.add_span_processor(span_processor) # 3. Instrument the Anthropic library AnthropicInstrumentor().instrument() # --- Anthropic API Call --- ANTHROPIC_API_KEY = os.environ.get("ANTHROPIC_API_KEY", "YOUR_ANTHROPIC_API_KEY") if ANTHROPIC_API_KEY == "YOUR_ANTHROPIC_API_KEY": print("WARNING: ANTHROPIC_API_KEY not set or placeholder. API calls will fail without a valid key.") else: try: client = Anthropic(api_key=ANTHROPIC_API_KEY) print("Making an Anthropic API call...") response = client.messages.create( model="claude-3-opus-20240229", # Or another suitable model max_tokens=100, messages=[ {"role": "user", "content": "Explain the concept of quantum entanglement in a sentence."} ], ) print("Anthropic API call successful.") print(f"Response: {response.content[0].text[:50]}...") except Exception as e: print(f"Error during Anthropic API call: {e}") # Ensure all spans are exported before the application exits provider.force_flush()
opentelemetry-instrument --version
Debug
Known issues
breakingBreaking changes were introduced in version 0.54.0 to conform to the OpenTelemetry Generative AI Semantic Conventions (GenAI SemConv). This may alter span attribute names and structure.
fix
Review the OpenTelemetry GenAI Semantic Conventions documentation for the updated attribute naming and structure. Your observability backend might require updates to dashboards or alerts.
affects: >=0.54.0
gotchaBy default, this instrumentation logs the full content of prompts, completions, and embeddings to span attributes. This data may contain sensitive information.
fix
To disable logging of sensitive content, set the environment variable `TRACELOOP_TRACE_CONTENT` to `false`.
affects: All versions
gotchaWhen using `client.messages.stream()` with base64 encoded images, particularly with Langfuse, input token counts might be inflated in traces.
fix
This is a known upstream issue (traceloop/openllmetry#3949). Monitor the project's GitHub for a fix. Consider alternative methods for calculating token usage or use non-streaming calls if accurate token counts are critical for images.
affects: Reported in 0.57.0 (likely affects similar versions)
gotchaInstrumentation itself only creates spans. An OpenTelemetry SDK (TracerProvider, SpanProcessor, Exporter) must be explicitly configured to process and send telemetry data to a backend.
fix
Always initialize a `TracerProvider`, add at least one `SpanProcessor` (e.g., `BatchSpanProcessor`), and configure an appropriate `SpanExporter` (e.g., `OTLPSpanExporter`) before calling `instrument()` on the Anthropic instrumentor.
affects: All versions
Upgrade
Version history
0.62.3latest on PyPI · released Aug 10, 2026
Audit
Dependencies
anthropicrequiredRequired for interacting with the Anthropic API.
opentelemetry-apirequiredCore OpenTelemetry API for defining telemetry.
opentelemetry-sdkrequiredOpenTelemetry SDK for trace provider and span processors.
opentelemetry-semantic-conventions-airequiredProvides Generative AI semantic conventions for LLM tracing.
Agent activity
21 hits · last 30 days
node
16
OpenAI (training)
2
Resources
opentelemetry-instrumentation-anthropic — pip install opentelemetry-instrumentation-anthropic · libregistry