Anthropic Integration
Wrap the Anthropic Python client to automatically trace all Claude API calls. AgentLens captures model, token usage, cost, latency, and the full message exchange.
Installation
terminal
pip install vectry-agentlens anthropicQuick setup
main.py
import agentlens
from agentlens.integrations.anthropic import wrap_anthropic
import anthropic
agentlens.init(
api_key="your-api-key",
endpoint="https://agentlens.vectry.tech",
)
client = wrap_anthropic(anthropic.Anthropic())
response = client.messages.create(
model="claude-sonnet-4-20250514",
max_tokens=1024,
messages=[
{"role": "user", "content": "Explain the halting problem."},
],
)What gets captured
| Field | Description |
|---|---|
| input.model | Model name (claude-sonnet-4-20250514, claude-haiku, etc.) |
| input.messages | Full message array sent to the API |
| input.system | System prompt if provided |
| output.content | Response content blocks |
| tokenCount | Input tokens + output tokens |
| costUsd | Estimated cost based on model pricing |
| durationMs | Wall-clock request time |
| metadata.stop_reason | How generation ended (end_turn, max_tokens, tool_use) |
Async client
async_example.py
from agentlens.integrations.anthropic import wrap_anthropic
import anthropic
async_client = wrap_anthropic(anthropic.AsyncAnthropic())
response = await async_client.messages.create(
model="claude-sonnet-4-20250514",
max_tokens=1024,
messages=[{"role": "user", "content": "Hello!"}],
)Combining with @trace
combined.py
import agentlens
from agentlens import trace
from agentlens.integrations.anthropic import wrap_anthropic
import anthropic
agentlens.init(api_key="...", endpoint="...")
client = wrap_anthropic(anthropic.Anthropic())
@trace(name="analysis-agent")
async def analyze(document: str) -> str:
response = client.messages.create(
model="claude-sonnet-4-20250514",
max_tokens=2048,
system="You are a document analysis expert.",
messages=[{"role": "user", "content": f"Analyze: {document}"}],
)
return response.content[0].textTool use
When Claude invokes tools, AgentLens captures each tool use as a TOOL_SELECTION decision point automatically:
tools.py
@trace(name="claude-tool-agent")
async def tool_agent(prompt: str):
response = client.messages.create(
model="claude-sonnet-4-20250514",
max_tokens=1024,
tools=[{
"name": "get_stock_price",
"description": "Get the current stock price for a ticker symbol",
"input_schema": {
"type": "object",
"properties": {
"ticker": {
"type": "string",
"description": "Stock ticker symbol"
}
},
"required": ["ticker"]
}
}],
messages=[{"role": "user", "content": prompt}],
)
return responseSupported API methods
messages.create()— Message creation (including streaming)messages.count_tokens()— Token counting