OpenInference Anthropic Instrumentation

1.0.0 · active · verified Thu Apr 16

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

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to instrument Anthropic API calls using `openinference-instrumentation-anthropic` and send the traces to an OpenTelemetry collector. It configures a `TracerProvider` with an OTLP HTTP exporter, instruments the Anthropic library, and then makes a sample API call. You can run a local collector like Arize Phoenix (<code>python -m phoenix.server.main serve</code>) to view the traces.

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.")

view raw JSON →