# LangChain and LangGraph cost tracking

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

```bash [Python]
pip install langchain-veqen
```

```bash [TypeScript]
npm install @veqen/sdk
```

## Record every call

```python [Python]
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]})
```

```ts [TypeScript]
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](/docs/tags), 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](/docs/tags): customer, feature, user, project, environment and run.
- [Budgets](/docs/budgets): caps that stop a chain before it spends.
- [Reconciliation](/docs/reconciliation): check the ledger against the provider's bill.
