Anthropic SDK

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.

Create an account at platform.respan.ai and grab an API key. For gateway, also add credits or a provider key.

Run npx @respan/cli setup to set up with your coding agent.

Setup

1

Install packages

$pip install respan-ai respan-instrumentation-anthropic anthropic
2

Set 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.

3

Initialize and run

1from anthropic import Anthropic
2from respan import Respan
3from respan_instrumentation_anthropic import AnthropicInstrumentor
4
5respan = Respan(instrumentations=[AnthropicInstrumentor()])
6
7client = Anthropic()
8
9message = client.messages.create(
10 model="claude-sonnet-4-5-20250929",
11 max_tokens=1024,
12 messages=[{"role": "user", "content": "Say hello in three languages."}],
13)
14print(message.content[0].text)
15respan.flush()
4

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.

1from respan import Respan
2from respan_instrumentation_anthropic import AnthropicInstrumentor
3
4respan = Respan(
5 instrumentations=[AnthropicInstrumentor()],
6 customer_identifier="user_123",
7 metadata={"service": "chat-api", "version": "1.0.0"},
8)

With propagate_attributes

Override per-request using a context scope.

1from anthropic import Anthropic
2from respan import Respan, propagate_attributes
3from respan_instrumentation_anthropic import AnthropicInstrumentor
4
5respan = Respan(instrumentations=[AnthropicInstrumentor()])
6client = Anthropic()
7
8def handle_request(user_id: str, question: str):
9 with propagate_attributes(
10 customer_identifier=user_id,
11 thread_identifier="conv_abc_123",
12 metadata={"plan": "pro"},
13 ):
14 message = client.messages.create(
15 model="claude-sonnet-4-5-20250929",
16 max_tokens=1024,
17 messages=[{"role": "user", "content": question}],
18 )
19 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.

1from anthropic import Anthropic
2from respan import Respan, workflow, task
3from respan_instrumentation_anthropic import AnthropicInstrumentor
4
5respan = Respan(instrumentations=[AnthropicInstrumentor()])
6client = Anthropic()
7
8@task(name="generate_outline")
9def outline(topic: str) -> str:
10 message = client.messages.create(
11 model="claude-sonnet-4-5-20250929",
12 max_tokens=1024,
13 messages=[{"role": "user", "content": f"Create a brief outline about: {topic}"}],
14 )
15 return message.content[0].text
16
17@workflow(name="content_pipeline")
18def pipeline(topic: str):
19 plan = outline(topic)
20 message = client.messages.create(
21 model="claude-sonnet-4-5-20250929",
22 max_tokens=2048,
23 messages=[{"role": "user", "content": f"Write content from this outline: {plan}"}],
24 )
25 print(message.content[0].text)
26
27pipeline("Benefits of API gateways")
28respan.flush()

Examples

Streaming

Stream Claude responses with real-time text deltas.

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

Tool calls

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

1tools = [
2 {
3 "name": "get_weather",
4 "description": "Get the weather for a city.",
5 "input_schema": {
6 "type": "object",
7 "properties": {"city": {"type": "string"}},
8 "required": ["city"],
9 },
10 }
11]
12
13message = client.messages.create(
14 model="claude-sonnet-4-5-20250929",
15 max_tokens=1024,
16 tools=tools,
17 messages=[{"role": "user", "content": "What's the weather in Paris?"}],
18)