Skip to main content
The AnthropicAdapter extracts telemetry from Anthropic Message responses — model name, input and output token counts, and cost in USD — and passes them directly into agentvista.record() or a traced run. Cost is calculated automatically from the model name using AgentVista’s built-in pricing table; no manual configuration is required.

Setup

1

Install the SDK with the Anthropic extra

pip install agentvista[anthropic]
This installs agentvista along with the anthropic package.
2

Initialize AgentVista

Call agentvista.init() once at application startup with your API key.
import agentvista

agentvista.init(api_key="av_xxxxx")
3

Import the adapter

from agentvista.adapters.anthropic import AnthropicAdapter

adapter = AnthropicAdapter()

Usage examples

Use adapter.extract() on any anthropic.types.Message response and unpack the result directly into agentvista.record().
import anthropic
import agentvista
from agentvista.adapters.anthropic import AnthropicAdapter

agentvista.init(api_key="av_xxxxx")
adapter = AnthropicAdapter()

client = anthropic.Anthropic()
response = client.messages.create(
    model="claude-sonnet-4-6",
    max_tokens=1024,
    messages=[{"role": "user", "content": "Qualify this lead..."}],
)

# Extract model, input_tokens, output_tokens, total_tokens, cost_usd
telemetry = adapter.extract(response)
agentvista.record(agent="lead-qualifier", success=True, **telemetry)
adapter.extract() returns a dict with any of these keys present:
KeyTypeDescription
modelstrModel ID returned by the API (e.g. "claude-sonnet-4-6-20260205")
input_tokensintPrompt tokens plus any cache creation and cache read tokens
output_tokensintCompletion tokens
total_tokensintSum of input and output tokens
cost_usdfloatTotal cost in USD, rounded to 6 decimal places

Supported models

Cost calculation is automatic for the following Claude models. If your model is not listed, cost_usd will be absent from the extracted telemetry; all other fields (tokens, model name) are still captured.
ModelInput (per M tokens)Output (per M tokens)
claude-opus-4-6$5.00$25.00
claude-sonnet-4-6$3.00$15.00
claude-sonnet-4-5$3.00$15.00
claude-haiku-4-5$1.00$5.00
claude-opus-4-0$15.00$75.00
claude-haiku-3-5$0.80$4.00
claude-3-haiku$0.25$1.25
Models with prompt caching enabled (Opus 4.6, Sonnet 4.6, Sonnet 4.5, Haiku 4.5) also track cache_creation_input_tokens and cache_read_input_tokens from the Anthropic response and apply the correct cached read and cache write rates automatically.The adapter matches both full versioned IDs (e.g. claude-sonnet-4-6-20260205) and short aliases (e.g. claude-sonnet-4-6).