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.mdSetup
Install packages
pip install respan-ai respan-instrumentation-anthropic anthropicSet environment variables
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
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
| Parameter | Type | Default | Description |
|---|---|---|---|
api_key | str | None | None | Falls back to RESPAN_API_KEY env var. |
base_url | str | None | None | Falls back to RESPAN_BASE_URL env var. |
instrumentations | list | [] | Plugin instrumentations to activate (e.g. AnthropicInstrumentor()). |
customer_identifier | str | None | None | Default customer identifier for all spans. |
metadata | dict | None | None | Default metadata attached to all spans. |
environment | str | None | None | Environment tag (e.g. "production"). |
Attributes
In Respan()
Set defaults at initialization — these apply to all spans.
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.
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)| Attribute | Type | Description |
|---|---|---|
customer_identifier | str | Identifies the end user in Respan analytics. |
thread_identifier | str | Groups related messages into a conversation. |
metadata | dict | Custom 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.
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.
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.
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.