Skip to main content
The Netra SDK automatically instruments Anthropic Claude API calls, capturing prompts, completions, token usage, and performance metrics through OpenTelemetry tracing.

Installation

Install the Netra SDK and the Anthropic client library:
pip install netra-sdk anthropic

Usage

Initialize the Netra SDK with your API key to automatically trace all Anthropic Claude API calls.

Basic Setup

from netra import Netra
import anthropic
import os

# Initialize Netra with your API key
Netra.init(headers=f"x-api-key={os.getenv('NETRA_API_KEY')}")

# Use Anthropic client as normal
client = anthropic.Anthropic(api_key=os.getenv("ANTHROPIC_API_KEY"))

message = client.messages.create(
    model="claude-3-5-sonnet-20241022",
    max_tokens=1024,
    messages=[
        {"role": "user", "content": "Hello, Claude"}
    ]
)

print(message.content)

Streaming Responses

The SDK automatically traces streaming responses from Claude.
from netra import Netra
import anthropic
import os

Netra.init(headers=f"x-api-key={os.getenv('NETRA_API_KEY')}")

client = anthropic.Anthropic(api_key=os.getenv("ANTHROPIC_API_KEY"))

with client.messages.stream(
    model="claude-3-5-sonnet-20241022",
    max_tokens=1024,
    messages=[{"role": "user", "content": "Tell me a story"}]
) as stream:
    for text in stream.text_stream:
        print(text, end="", flush=True)

Session Tracking

Track user sessions and conversations across multiple Claude API calls.
from netra import Netra, ConversationType
import anthropic
import os

Netra.init(headers=f"x-api-key={os.getenv('NETRA_API_KEY')}")

# Set session and user context
Netra.set_session_id("session-123")
Netra.set_user_id("user-456")

client = anthropic.Anthropic(api_key=os.getenv("ANTHROPIC_API_KEY"))

# Track conversation
Netra.add_conversation(
    content="What is machine learning?",
    conversation_type=ConversationType.INPUT
)

response = client.messages.create(
    model="claude-3-5-sonnet-20241022",
    max_tokens=1024,
    messages=[{"role": "user", "content": "What is machine learning?"}]
)

Netra.add_conversation(
    content=response.content[0].text,
    conversation_type=ConversationType.OUTPUT
)

Custom Attributes

Add custom metadata to your Claude API traces for filtering and analysis.
from netra import Netra
import anthropic
import os

Netra.init(headers=f"x-api-key={os.getenv('NETRA_API_KEY')}")

# Set custom attributes
Netra.set_custom_attributes({
    "environment": "production",
    "feature": "chat-assistant",
    "model_version": "v2"
})

client = anthropic.Anthropic(api_key=os.getenv("ANTHROPIC_API_KEY"))

message = client.messages.create(
    model="claude-3-5-sonnet-20241022",
    max_tokens=1024,
    messages=[{"role": "user", "content": "Hello"}]
)

Next Steps

Last modified on January 30, 2026