Skip to Content
Launching the open-source gateway

Anthropic SDK (tracing)

The Anthropic SDK  is the official client for Anthropic’s Claude models, supporting messages, streaming, and tool use. Respan gives you full observability over every Claude call, streamed response, and tool invocation — and gateway routing through the Anthropic-compatible Respan endpoint.

Set up Respan

Create an account at platform.respan.ai  and grab an API key .

Paste this into your coding agent:

Help me set up Respan tracing with the Anthropic SDK integration. Follow https://www.respan.ai/docs/integrations/anthropic.md

Setup

Install packages

Terminal
pip install respan-ai respan-instrumentation-anthropic anthropic

Set environment variables

Terminal
export ANTHROPIC_API_KEY="YOUR_ANTHROPIC_API_KEY" export RESPAN_API_KEY="YOUR_RESPAN_API_KEY"

ANTHROPIC_API_KEY is used for Claude requests. RESPAN_API_KEY is used to export traces to Respan.

Initialize and run

Python
from anthropic import Anthropic from respan import Respan from respan_instrumentation_anthropic import AnthropicInstrumentor respan = Respan(instrumentations=[AnthropicInstrumentor()]) client = Anthropic() 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)

View your trace

Open the Traces page  to see your auto-instrumented LLM spans with messages, tokens, and tool calls.

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()).
customer_identifierstr | NoneNoneDefault customer identifier for all spans.
metadatadict | NoneNoneDefault metadata attached to all spans.
environmentstr | NoneNoneEnvironment tag (e.g. "production").

Attributes

In Respan()

Set defaults at initialization — these apply to all spans.

Python
from respan import Respan from respan_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 scope.

Python
from anthropic import Anthropic from respan import Respan, propagate_attributes from respan_instrumentation_anthropic import AnthropicInstrumentor respan = Respan(instrumentations=[AnthropicInstrumentor()]) client = Anthropic() def handle_request(user_id: str, question: str): with propagate_attributes( customer_identifier=user_id, thread_identifier="conv_abc_123", metadata={"plan": "pro"}, ): 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 (optional)

Decorators are not required. All Anthropic calls are auto-traced by the instrumentor. Use @workflow and @task to add structure when you want to group related calls into a named workflow with nested tasks.

Python
from anthropic import Anthropic from respan import Respan, workflow, task from respan_instrumentation_anthropic import AnthropicInstrumentor 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")

Examples

Streaming

Stream Claude responses with real-time text deltas.

Python
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

Tool calls are automatically captured as spans with inputs, outputs, and timing.

Python
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?"}], )

Gateway

See Anthropic SDK gateway setup to route this integration through the Respan gateway.