Skip to content

Latest commit

 

History

History

README.rst

OpenTelemetry Anthropic Instrumentation

pypi

This library allows tracing LLM requests made by the Anthropic Python SDK.

Installation

pip install opentelemetry-instrumentation-genai-anthropic

If you don't have an Anthropic application yet, try our examples which only need a valid Anthropic API key.

Check out the zero-code example for a quick start.

Usage

This section describes how to set up Anthropic instrumentation if you're setting OpenTelemetry up manually. Check out the manual example for more details.

from opentelemetry.instrumentation.genai.anthropic import AnthropicInstrumentor
import anthropic

# Instrument Anthropic
AnthropicInstrumentor().instrument()

# Use Anthropic client as normal
client = anthropic.Anthropic()
response = client.messages.create(
    model="claude-3-5-sonnet-20241022",
    max_tokens=1024,
    messages=[
        {"role": "user", "content": "Hello, Claude!"}
    ]
)

Configuration

Capture Message Content

By default, prompts and completions are not captured. To capture message content, set the environment variable OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT to one of NO_CONTENT, SPAN_ONLY, EVENT_ONLY, or SPAN_AND_EVENT:

export OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT=SPAN_AND_EVENT

Uploading prompts and completions

Instead of recording message content inline, prompts and completions can be uploaded to external storage via a completion hook. To enable the built-in upload hook, set:

  • OTEL_INSTRUMENTATION_GENAI_COMPLETION_HOOK=upload
  • OTEL_INSTRUMENTATION_GENAI_UPLOAD_BASE_PATH to an fsspec-compatible URI/path (e.g. /path/to/prompts or gs://my_bucket), and install the upload extra (pip install opentelemetry-util-genai[upload]).

A custom CompletionHook can also be passed programmatically, taking precedence over the environment variable:

AnthropicInstrumentor().instrument(completion_hook=my_hook)

References