LangChain and LangGraph cost tracking
Price every LangChain and LangGraph model call and split the cost by customer, feature and agent run, with one callback handler.
By Keyveve, Inc. · Updated 2026-10-23
One callback handler records every chat model call a chain, agent or graph makes: the model, its tokens (cached input and cache writes kept apart) and the tags you set. No prompt or response text leaves your process.
Install
pip install langchain-veqen
npm install @veqen/sdk
Record every call
import veqen
from langchain_veqen import VeqenCallbackHandler
veqen.init() # reads VEQEN_API_KEY, an ingest key
handler = VeqenCallbackHandler(feature="support-agent")
with veqen.tags(customer=org.id, run=conversation.id):
graph.invoke(inputs, config={"callbacks": [handler]})
import { withTags } from "@veqen/sdk";
import { VeqenCallbackHandler } from "@veqen/sdk/langchain";
const handler = new VeqenCallbackHandler({ feature: "support-agent" });
await withTags({ customer: org.id, run: conversation.id }, () =>
graph.invoke(inputs, { callbacks: [handler] }));
Passing the handler on the invocation reaches every nested model call in a LangGraph graph, tool loops included. Tagging the run puts the whole conversation on Runs, with its total cost and call count.
What it reads
LangChain's standard usage_metadata, so it works with every chat model integration that reports usage: OpenAI, Anthropic, Google, Bedrock, Groq, Together, Ollama and the rest. When streaming, turn on usage in the model (stream_usage=True for ChatOpenAI). Calls without usage are skipped rather than estimated.
Budgets are checked before each model call starts: a hard cap raises BudgetExceeded inside the chain, so it stops before spending.
Don't record twice
Use one path per call. If the handler is attached, don't also wrap() the provider client the chat model uses, or the call is counted twice.
Next
- Tags: customer, feature, user, project, environment and run.
- Budgets: caps that stop a chain before it spends.
- Reconciliation: check the ledger against the provider's bill.
Veqen records what every AI request cost and who it was for, checks it against your provider bills and prepares the entries for your accounting system. Start free or read the quickstart.