Anthropic
Auto-instrumentation for Anthropic API.
Risicare automatically instruments the Anthropic Python SDK.
Installation
pip install risicare anthropicBasic Usage
from anthropic import Anthropic
import risicare
risicare.init()
client = Anthropic()
# Automatically traced
response = client.messages.create(
model="claude-sonnet-5-5",
max_tokens=1024,
messages=[
{"role": "user", "content": "What is the capital of France?"}
]
)Supported Methods
| Method | Traced |
|---|---|
messages.create | Yes (sync + async) |
A streaming response is traced via the stream=True parameter on messages.create.
Streaming
A streaming response is traced via the stream=True parameter:
stream = client.messages.create(
model="claude-sonnet-5-5",
max_tokens=1024,
messages=[{"role": "user", "content": "Write a poem"}],
stream=True
)
for event in stream:
if event.type == "content_block_delta":
print(event.delta.text, end="")Note: The client.messages.stream() context manager is not instrumented. Always use messages.create(stream=True) to ensure traces are captured.
Async Support
Async clients are automatically instrumented:
import asyncio
from anthropic import AsyncAnthropic
client = AsyncAnthropic()
async def main():
response = await client.messages.create(
model="claude-sonnet-5-5",
max_tokens=1024,
messages=[{"role": "user", "content": "Hello!"}]
)
asyncio.run(main())Tool Use
Tool use is captured with full context:
response = client.messages.create(
model="claude-sonnet-5-5",
max_tokens=1024,
messages=[{"role": "user", "content": "What's the weather in Paris?"}],
tools=[{
"name": "get_weather",
"description": "Get current weather",
"input_schema": {
"type": "object",
"properties": {
"location": {"type": "string"}
}
}
}]
)Risicare captures:
- Tool definitions
- Tool use blocks
- Tool inputs and results
Captured Attributes
| Attribute | Description |
|---|---|
gen_ai.system | anthropic |
gen_ai.request.model | Requested model name |
gen_ai.response.model | Model name returned by API |
gen_ai.response.id | Response ID |
gen_ai.request.max_tokens | Max output tokens |
gen_ai.request.temperature | Sampling temperature (null when the request does not pass it). anthropic 1.11.0 does not accept temperature or top_p: the call raises TypeError before any request, so only its error span carries the value |
gen_ai.request.stream | Whether streaming was requested |
gen_ai.request.has_tools | Whether tools were provided |
gen_ai.usage.prompt_tokens | Input tokens |
gen_ai.usage.completion_tokens | Output tokens |
gen_ai.usage.total_tokens | Total tokens |
gen_ai.response.stop_reason | Stop reason |
gen_ai.completion.tool_uses | Number of tool use blocks |
gen_ai.latency_ms | Request latency in milliseconds |
Cost Tracking
The cost in the dashboard comes from the server's price table, which has Anthropic's list prices for the current Claude models, claude-sonnet-5-5 included. The cost of cached traffic is too low (cached tokens are not priced yet). See Cost Tracking.
System Prompts
System prompts are captured separately:
response = client.messages.create(
model="claude-sonnet-5-5",
max_tokens=1024,
system="You are a helpful assistant.",
messages=[{"role": "user", "content": "Hello!"}]
)