Guides / track llm cost langchain
How to track LLM cost in LangChain
Culpa's LangChain adapter is a callback handler you attach to your calls, and it reports the token counts LangChain itself surfaces. That's an honest floor rather than a ceiling, because the adapter sees what the framework reports and not the raw provider response. Culpa, a local-first LLM cost, margin, and forecast ledger, prices what arrives and reconciles it against the invoice so the floor is measurable.
Why this happens
A framework sits between your code and the provider, and it decides what to pass on. Anything it summarises or drops is invisible to anything downstream of it, including a cost adapter. The fields most at risk are the ones that change the price rather than the count, and cached input is the clearest example: if a framework reports prompt tokens as one number, a ledger has no way to know which of them were cached reads at a tenth of the rate. The result is a ledger that's internally consistent and reads above the invoice.
What this usually looks like
- Your captured spend runs above the provider invoice rather than below it.
- Your prompts use a large cached prefix and nothing in your ledger shows cached tokens.
- The gap is proportional to your cached share rather than a fixed amount.
- Per-call token counts look right while per-call cost looks high.
Free, no card, no account
Run the free Cost Leak Scan
It shows your most expensive conversation before you install anything.
Mistakes that cost the most
| Mistake | Why it hurts | Do instead |
|---|---|---|
| Assuming a framework passes through every usage field the provider returned. | The fields that decide price, cached reads especially, are the ones most often collapsed. | Reconcile against the invoice in the first week and treat any gap as a fidelity measurement. |
| Reading a ledger that sits above the invoice as an accounting error. | It's usually a capture-fidelity gap, and looking in the accounting will find nothing. | Compare the shape of the gap against your cached share. A proportional gap names its own cause. |
| Running only one capture path and trusting it because it's consistent. | Consistency is what a floor looks like from the inside. | Run the request path or the in-process SDK alongside it for a week and compare the two. |
Run this check tonight
- Total your captured spend for a full month and put it beside the provider invoice.
- Work out what share of your input is a repeated cached prefix.
- Check whether any cached-token figure reaches your ledger at all.
- Divide the gap by your cached volume. If it lands near the cached discount, you've found the cause.
- Run a second capture path for a week and compare the two ledgers on the same traffic.
What a collapsed cached-token field does to a ledger
Illustrative example
A feature on Claude Opus 4.8 at real rates of $5.00 per million input, $25.00 output and $0.50 cached read. It sends 40 million input tokens a month of which 30 million is a cached prefix, plus 6 million output. The cached share is modelled.
The count was right and the price was wrong, and the arithmetic points straight at the missing field. A gap that divides cleanly into a rate difference is a fidelity problem, not an accounting one.
Every number, with its confidence and source
| Figure | What it means | Confidence | Source |
|---|---|---|---|
| $215 to $350 | modelled monthly cost of one workload, priced with the cached split and priced without it | estimated | Both endpoints from the teardown arithmetic at real Claude Opus 4.8 rates from the price book, effective 2026-07-02. A range because the cached share is modelled. |
| 63% | modelled over-statement when a cached-token field never reaches the ledger | calculated | $350 divided by $215, from the teardown arithmetic. |
What a generic answer can’t know
What your framework reports, and what your provider actually billed, are two numbers that exist in two different places and are never compared by either of them. Putting them side by side is the whole exercise. Culpa does it on your infrastructure, keeps your prompts and responses there, and counts the calls to run your plan.
Questions founders ask next
How accurate is LangChain cost tracking?
As accurate as what LangChain reports, which is a floor rather than a ceiling. The adapter builds an openai-shaped body from the framework's own token counts, so any usage field the framework collapses is a field no downstream ledger can recover.
Will the adapter break my chain if Culpa is unavailable?
No. Every hook swallows its own failures by design, so a Culpa problem can't propagate into your chain. That's the same fail-open contract the rest of the capture surface follows.
How do I get full-fidelity capture instead?
Capture on the request path by pointing your base URL at Culpa, or use the in-process SDK, which reports the raw provider response. Both see the usage fields the provider actually returned rather than the framework's summary of them.
On your infrastructure
Culpa runs on your infrastructure. Your prompts and responses never leave it. Culpa counts calls to run your plan, and it fails open, so if it ever breaks your app keeps running.
How Culpa works
Find the culprit. Not just the total.
Your dashboard shows what you spent. It stops short of who spent it. Culpa shows the conversation, the user and the feature behind it.
Your prompts stay local.
Culpa runs on your own infrastructure. What you send to a model reaches us at no point.
Every dollar has a name.
Follow any charge to the conversation, the user, the feature and the customer behind it.
See the bill before it lands.
Cost your next feature before you ship it. You get the likely bill and the worst case, at best, median, p90 and p99.
Three steps to your first answer.
Change one base URL.
Or drop in the Python or TypeScript library.
Find your most expensive conversation.
In the first session, not the first week.
Cost your next feature before you ship it.
Why the bill went up
Example dashboardCalls traced
418,209
across 3 projects
Spend this week
$378.41
+ $182 vs last week
Failed calls
312
74% retried, and you paid for all of them
+ $182 this week traced to one culprit
Spend over 14 days
Most expensive users
Next week forecast
Graded against reality. Accuracy shown as results land.
Free, no card, no account
Run the free Cost Leak Scan
It shows your most expensive conversation before you install anything.
Keep reading
Sources: Anthropic pricing. Last reviewed 2026-08-02. Plain text version.