OpenInference Anthropic Instrumentation
openinference-instrumentation-anthropic is a Python auto-instrumentation library for the Anthropic package. It captures traces for Anthropic client calls (e.g., Messages, Completions, AsyncMessages, AsyncCompletions, BetaMessagesParse) and aligns them with OpenInference semantic conventions. These traces are fully OpenTelemetry compatible and can be sent to any OpenTelemetry collector, such as Arize Phoenix, for observability and evaluation. The library is currently at version 1.0.0 and is actively maintained with frequent updates to the broader OpenInference project.
Common errors
-
ModuleNotFoundError: No module named 'openinference.instrumentation.anthropic'
cause The `openinference-instrumentation-anthropic` package has not been installed.fixRun `pip install openinference-instrumentation-anthropic`. -
anthropic.APIStatusError: 401: Invalid Authentication
cause The `ANTHROPIC_API_KEY` environment variable is either not set or contains an invalid key, or the key has expired/been revoked.fixSet the `ANTHROPIC_API_KEY` environment variable with a valid API key, e.g., `export ANTHROPIC_API_KEY='sk-...'`, or pass it directly to the `Anthropic` client constructor. -
No traces appear in my OpenTelemetry collector (e.g., Phoenix) despite running the application.
cause The Anthropic client was not successfully instrumented, or the OpenTelemetry `TracerProvider` and `SpanProcessor` are not correctly configured/initialized, or the collector is not running.fixEnsure `AnthropicInstrumentor().instrument(tracer_provider=...)` is called early, `tracer_provider` is correctly set up with `OTLPSpanExporter`, and your OpenTelemetry collector (like Phoenix) is actively running and accessible at the configured endpoint.
Warnings
- breaking Future updates to the underlying `anthropic` SDK (e.g., new API types, changed response structures) may require corresponding updates to `openinference-instrumentation-anthropic` to maintain full compatibility and accurate tracing. Always check release notes for `anthropic` and `openinference` for potential version conflicts or required upgrades.
- gotcha The `AnthropicInstrumentor().instrument(tracer_provider=tracer_provider)` call must occur *before* any Anthropic client instances are created or API calls are made to ensure proper instrumentation. If instrumentation is initialized too late, calls may not be traced.
- gotcha Incorrectly configured OpenTelemetry exporter endpoint will result in traces not being sent or visible in your observability backend. The default for many local collectors like Phoenix is `http://127.0.0.1:6006/v1/traces`.
Install
-
pip install openinference-instrumentation-anthropic anthropic opentelemetry-sdk opentelemetry-exporter-otlp
Imports
- AnthropicInstrumentor
from openinference.instrumentation.anthropic import AnthropicInstrumentor
Quickstart
import os
from anthropic import Anthropic
from openinference.instrumentation.anthropic import AnthropicInstrumentor
from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter
from opentelemetry.sdk import trace as trace_sdk
from opentelemetry.sdk.trace.export import SimpleSpanProcessor
# Set your Anthropic API key (replace with actual key or use environment variable)
os.environ['ANTHROPIC_API_KEY'] = os.environ.get('ANTHROPIC_API_KEY', 'your_anthropic_api_key_here')
# Configure OpenTelemetry TracerProvider to send traces to an OTLP endpoint (e.g., Arize Phoenix)
# Default Phoenix endpoint: http://localhost:6006/v1/traces
endpoint = os.environ.get('OTEL_EXPORTER_OTLP_ENDPOINT', 'http://127.0.0.1:6006/v1/traces')
tracer_provider = trace_sdk.TracerProvider()
tracer_provider.add_span_processor(SimpleSpanProcessor(OTLPSpanExporter(endpoint)))
# Instrument the Anthropic client
AnthropicInstrumentor().instrument(tracer_provider=tracer_provider)
# Create an Anthropic client and make a request
client = Anthropic()
try:
print("Making Anthropic API call...")
message = client.messages.create(
model="claude-3-5-sonnet-20240620",
max_tokens=1000,
temperature=0,
messages=[
{"role": "user", "content": "Explain the concept of quantum entanglement in simple terms."}
]
)
print("Anthropic Response:")
print(message.content[0].text)
print("Traces should now be visible in your OpenTelemetry collector (e.g., Phoenix).")
except Exception as e:
print(f"An error occurred: {e}")
print("Ensure ANTHROPIC_API_KEY is set and your OpenTelemetry collector is running.")