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 anthropic

Quick 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

FieldDescription
input.modelModel name (claude-sonnet-4-20250514, claude-haiku, etc.)
input.messagesFull message array sent to the API
input.systemSystem prompt if provided
output.contentResponse content blocks
tokenCountInput tokens + output tokens
costUsdEstimated cost based on model pricing
durationMsWall-clock request time
metadata.stop_reasonHow 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].text

Tool 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 response

Supported API methods

  • messages.create() — Message creation (including streaming)
  • messages.count_tokens() — Token counting