Anthropic

Instrument LLM calls made using Anthropic's SDK

Anthropic is a leading provider for state-of-the-art LLMs. The Anthropic SDK can be instrumented using the openinference-instrumentation-anthropic package.

In this example we will instrument a small program that uses the Anthropic chat completions API and observe the traces in Arize.

Copy

pip install openinference-instrumentation-anthropic anthropic arize-otel opentelemetry-sdk opentelemetry-exporter-grpc

Set the ANTHROPIC_API_KEY environment variable to authenticate calls made using the SDK.

Copy

export ANTHROPIC_API_KEY=[your_key_here]

In a python file, setup the AnthropicInstrumentor and configure the tracer to send traces to Arize.

Copy

# Import open-telemetry dependencies
from arize.otel import register

# Setup OTel via our convenience function
tracer_provider = register(
    space_id = "your-space-id", # in app space settings page
    api_key = "your-api-key", # in app space settings page
    project_name = "your-project-name", # name this to whatever you would like
)

# Import openinference instrumentor to map Anthropic traces to a standard format
from openinference.instrumentation.anthropic import AnthropicInstrumentor

# Turn on the instrumentor
AnthropicInstrumentor().instrument(tracer_provider=tracer_provider)

To test, run the following code and observe your traces in Arize.

Copy

import anthropic

client = anthropic.Anthropic()

message = client.messages.create(
    model="claude-3-5-sonnet-20240620",
    max_tokens=1000,
    temperature=0,
    messages=[
        {
            "role": "user",
            "content": [
                {
                    "type": "text",
                    "text": "Why is the ocean salty?"
                }
            ]
        }
    ]
)
print(message.content)

Last updated

Copyright © 2023 Arize AI, Inc