Skip to main content
  1. Sign up — Create an account at platform.respan.ai
  2. Create an API key — Generate one on the API keys page
  3. Add credits or a provider key — Add credits on the Credits page or connect your own provider key on the Integrations page
Add the Docs MCP to your AI coding tool to get help building with Respan. No API key needed.
{
  "mcpServers": {
    "respan-docs": {
      "url": "https://docs.respan.ai/mcp"
    }
  }
}

What is Anthropic SDK?

The Anthropic SDK is the official Python client for Anthropic’s Claude models, supporting messages, streaming, and tool use. Respan can auto-instrument all Anthropic calls for tracing, route them through the Respan gateway, or both.

Setup

1

Install packages

2

Set environment variables

export ANTHROPIC_API_KEY="YOUR_ANTHROPIC_API_KEY"
export RESPAN_API_KEY="YOUR_RESPAN_API_KEY"
3

Initialize and run

4

View your trace

Open the Traces page to see your auto-instrumented LLM spans.
This step applies to Tracing and Both setups. The Gateway-only setup does not produce traces.

Configuration

ParameterTypeDefaultDescription
api_keystr | NoneNoneFalls back to RESPAN_API_KEY env var.
base_urlstr | NoneNoneFalls back to RESPAN_BASE_URL env var.
instrumentationslist[]Plugin instrumentations to activate (e.g. AnthropicInstrumentor()).
is_auto_instrumentbool | NoneFalseAuto-discover and activate all installed instrumentors via OpenTelemetry entry points.
customer_identifierstr | NoneNoneDefault customer identifier for all spans.
metadatadict | NoneNoneDefault metadata attached to all spans.
environmentstr | NoneNoneEnvironment tag (e.g. "production").

Attributes

Attach customer identifiers, thread IDs, and metadata to spans.

In Respan()

Set defaults at initialization — these apply to all spans.
from respan import Respan
from openinference.instrumentation.anthropic import AnthropicInstrumentor

respan = Respan(
    instrumentations=[AnthropicInstrumentor()],
    customer_identifier="user_123",
    metadata={"service": "chat-api", "version": "1.0.0"},
)

With propagate_attributes

Override per-request using a context manager.
from respan import Respan, workflow, propagate_attributes
from openinference.instrumentation.anthropic import AnthropicInstrumentor

respan = Respan(
    instrumentations=[AnthropicInstrumentor()],
    metadata={"service": "chat-api", "version": "1.0.0"},
)

@workflow(name="handle_request")
def handle_request(user_id: str, question: str):
    with propagate_attributes(
        customer_identifier=user_id,
        thread_identifier="conv_001",
        metadata={"plan": "pro"},  # merged with default metadata
    ):
        message = client.messages.create(
            model="claude-sonnet-4-5-20250929",
            max_tokens=1024,
            messages=[{"role": "user", "content": question}],
        )
        print(message.content[0].text)
AttributeTypeDescription
customer_identifierstrIdentifies the end user in Respan analytics.
thread_identifierstrGroups related messages into a conversation.
metadatadictCustom key-value pairs. Merged with default metadata.

Decorators

Use @workflow and @task to create structured trace hierarchies.
from respan import Respan, workflow, task
from openinference.instrumentation.anthropic import AnthropicInstrumentor
from anthropic import Anthropic

respan = Respan(instrumentations=[AnthropicInstrumentor()])
client = Anthropic()

@task(name="generate_outline")
def outline(topic: str) -> str:
    message = client.messages.create(
        model="claude-sonnet-4-5-20250929",
        max_tokens=1024,
        messages=[
            {"role": "user", "content": f"Create a brief outline about: {topic}"},
        ],
    )
    return message.content[0].text

@workflow(name="content_pipeline")
def pipeline(topic: str):
    plan = outline(topic)
    message = client.messages.create(
        model="claude-sonnet-4-5-20250929",
        max_tokens=2048,
        messages=[
            {"role": "user", "content": f"Write content from this outline: {plan}"},
        ],
    )
    print(message.content[0].text)

pipeline("Benefits of API gateways")
respan.flush()

Examples

Basic message

message = client.messages.create(
    model="claude-sonnet-4-5-20250929",
    max_tokens=1024,
    messages=[{"role": "user", "content": "Say hello in three languages."}],
)
print(message.content[0].text)

Streaming

with client.messages.stream(
    model="claude-sonnet-4-5-20250929",
    max_tokens=1024,
    messages=[{"role": "user", "content": "Write a haiku about Python."}],
) as stream:
    for text in stream.text_stream:
        print(text, end="", flush=True)

Tool calls

import json

tools = [
    {
        "name": "get_weather",
        "description": "Get the weather for a city.",
        "input_schema": {
            "type": "object",
            "properties": {"city": {"type": "string"}},
            "required": ["city"],
        },
    }
]

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

if message.stop_reason == "tool_use":
    tool_block = next(b for b in message.content if b.type == "tool_use")
    result = f"Sunny, 72F in {tool_block.input['city']}"

    final = client.messages.create(
        model="claude-sonnet-4-5-20250929",
        max_tokens=1024,
        tools=tools,
        messages=[
            {"role": "user", "content": "What's the weather in Paris?"},
            {"role": "assistant", "content": message.content},
            {"role": "user", "content": [
                {"type": "tool_result", "tool_use_id": tool_block.id, "content": result}
            ]},
        ],
    )
    print(final.content[0].text)

Gateway features

The features below require the Gateway or Both setup from Step 3.

Switch models

Change the model parameter to use different Claude models through the same gateway.
# Claude Sonnet
message = client.messages.create(model="claude-sonnet-4-5-20250929", max_tokens=1024, messages=messages)

# Claude Haiku
message = client.messages.create(model="claude-3-5-haiku-20241022", max_tokens=1024, messages=messages)

# Claude Opus
message = client.messages.create(model="claude-3-opus-20240229", max_tokens=1024, messages=messages)
See the full model list.

Respan parameters

Pass additional Respan parameters via metadata under the respan_params key for gateway features.
message = client.messages.create(
    model="claude-sonnet-4-5-20250929",
    max_tokens=1024,
    messages=[{"role": "user", "content": "Hello"}],
    metadata={
        "respan_params": {
            "customer_identifier": "user_123",
            "metadata": {"session_id": "abc123"},
            "thread_identifier": "conversation_456",
        }
    },
)
See Respan parameters for the full list.