SDK & Integration · 2 min · Updated 2026-10-20

LangChain cost tracking

Track LangChain LLM calls with a callback handler

Setup

Install with the LangChain extra (pip install "tokeven[langchain]"), set TOKEVEN_INGEST_TOKEN to your ingest token, then add the handler to your model:

from langchain_anthropic import ChatAnthropic
from tokeven.integrations.langchain import TokevenCallbackHandler

llm = ChatAnthropic(model="claude-haiku-4-5", callbacks=[TokevenCallbackHandler()])

The handler works the same way with any LangChain chat model, including OpenAI and Gemini.

What it captures

One event per LLM call. Token counts come from the usage_metadata LangChain standardizes across providers. The handler never touches message text.

Budget guard

When a tokeven.guard budget is active, the handler checks it before each call and raises TokevenBudgetExceeded before the request is made.

Track your real costs

Calculators estimate. The dashboard shows what you actually spend and where you can save.