Skip to main content
Phoenix provides auto-instrumentation for the Anthropic Python SDK and Anthropic TypeScript SDK, allowing you to trace all calls to Claude models. Anthropic’s Claude models are state-of-the-art LLMs with extended context windows and strong performance on reasoning tasks. Phoenix makes it easy to observe Claude’s behavior in production.

Installation

pip install openinference-instrumentation-anthropic anthropic

Setup

1

Set your Anthropic API key

export ANTHROPIC_API_KEY=sk-ant-...
2

Register Phoenix tracer

from phoenix.otel import register

# Configure the Phoenix tracer
tracer_provider = register(
  project_name="my-llm-app",  # Default is 'default'
  auto_instrument=True  # Auto-instrument based on installed packages
)
3

Use Anthropic as normal

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)

What Gets Traced

The Anthropic instrumentation automatically captures:
  • Messages: Model, system prompt, messages, parameters
  • Streaming: Real-time token streaming
  • Tool Use: Tool definitions, tool calls, results
  • Token Usage: Input and output token counts
  • Errors: API errors, rate limits, validation errors

Advanced Examples

Streaming Responses

import anthropic

client = anthropic.Anthropic()

with client.messages.stream(
    model="claude-3-5-sonnet-20240620",
    max_tokens=1024,
    messages=[
        {"role": "user", "content": "Write a short poem about AI observability"}
    ],
) as stream:
    for text in stream.text_stream:
        print(text, end="", flush=True)

# Phoenix captures the full streamed response

Tool Use (Function Calling)

import anthropic
import json

client = anthropic.Anthropic()

# Define tools
tools = [
    {
        "name": "get_weather",
        "description": "Get the current weather in a given location",
        "input_schema": {
            "type": "object",
            "properties": {
                "location": {
                    "type": "string",
                    "description": "The city and state, e.g. San Francisco, CA"
                },
                "unit": {
                    "type": "string",
                    "enum": ["celsius", "fahrenheit"],
                    "description": "The unit of temperature"
                }
            },
            "required": ["location"]
        }
    }
]

message = client.messages.create(
    model="claude-3-5-sonnet-20240620",
    max_tokens=1024,
    tools=tools,
    messages=[{"role": "user", "content": "What's the weather like in San Francisco?"}]
)

# Phoenix captures tool definitions and calls
if message.stop_reason == "tool_use":
    for content in message.content:
        if content.type == "tool_use":
            print(f"Tool: {content.name}")
            print(f"Input: {content.input}")

System Prompts and Multi-turn Conversations

import anthropic

client = anthropic.Anthropic()

message = client.messages.create(
    model="claude-3-5-sonnet-20240620",
    max_tokens=1024,
    system="You are a helpful AI assistant specialized in Python programming.",
    messages=[
        {"role": "user", "content": "How do I read a CSV file?"},
        {"role": "assistant", "content": "You can use the pandas library..."},
        {"role": "user", "content": "What if I don't have pandas?"}
    ]
)

print(message.content[0].text)

Observability in Phoenix

Once instrumented, you can:
  • View all Claude API calls with full message history
  • Monitor token usage for cost tracking
  • Track response latency and streaming performance
  • Inspect tool use patterns and tool call sequences
  • Debug system prompts and conversation context
  • Analyze error patterns and rate limiting

Resources

Example Messages

Basic message example

Tool Calling Example

Multiple tool calls

OpenInference Package

View source code

Anthropic Documentation

Official API docs