Automatically extract token counts, model names, and cost from LLM API responses.
Adapters read the response object returned by an LLM provider SDK and extract a normalized dict of telemetry fields. You pass that dict directly to agentvista.record() or attach it inside an agentvista.run() block — no manual token counting required.Every adapter returns a subset of these fields:
Field
Type
Description
model
str
Model identifier as returned by the API (e.g. "claude-sonnet-4-6-20260205")
input_tokens
int
Input (prompt) token count
output_tokens
int
Output (completion) token count
total_tokens
int
Sum of input and output tokens
cost_usd
float
Estimated cost in USD, calculated from known model pricing
Fields present in the response are included; fields that cannot be determined are omitted from the dict.
Install with pip install agentvista[anthropic].AnthropicAdapter works with both sync responses (client.messages.create(...)) and streaming responses (stream.get_final_message()).
The Anthropic adapter includes cache token counts (cache_creation_input_tokens and cache_read_input_tokens) in the input_tokens total and uses them for accurate cost calculation when prompt caching is active.
Install with pip install agentvista[openai].OpenAIAdapter handles both the Chat Completions API (client.chat.completions.create) and the Responses API (client.responses.create). It auto-detects which response shape is present.
Install langchain-core in addition to agentvista. No extra install extra is required.AgentVistaCallbackHandler is a drop-in LangChain callback handler. Add it to any chain or agent via the callbacks argument and spans are produced automatically — no manual span() calls needed.
from langchain_core.prompts import ChatPromptTemplatefrom langchain_openai import ChatOpenAIimport agentvistafrom agentvista.adapters.langchain import AgentVistaCallbackHandleragentvista.init(api_key="av_xxxxx")handler = AgentVistaCallbackHandler(agent_name="lead-qualifier")llm = ChatOpenAI(model="gpt-4.1")prompt = ChatPromptTemplate.from_template("Qualify this lead: {lead}")chain = prompt | llm# All LLM and chain events are captured automaticallyresult = chain.invoke({"lead": "Acme Corp, 200 employees"}, config={"callbacks": [handler]})
When you add the handler inside an existing run() context, its spans become children of that root trace. This lets you attach an outcome signal:
with agentvista.run("lead-qualifier") as r: result = chain.invoke( {"lead": "Acme Corp"}, config={"callbacks": [handler]}, ) qualified = result.content.strip().lower() == "yes" r.set_outcome(success=qualified, outcome="qualified" if qualified else "rejected")
When used without an enclosing run(), the handler auto-creates a trace that is flushed when the outermost chain completes.
If langchain_core is not installed, AgentVistaCallbackHandler is still
importable but behaves as a silent no-op. This means you can add the handler
to shared code without making langchain_core a hard dependency for all
consumers.