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.

Run a free scan

Mistakes that cost the most

MistakeWhy it hurtsDo 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

  1. Total your captured spend for a full month and put it beside the provider invoice.
  2. Work out what share of your input is a repeated cached prefix.
  3. Check whether any cached-token figure reaches your ledger at all.
  4. Divide the gap by your cached volume. If it lands near the cached discount, you've found the cause.
  5. 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.

True cost: (30 x $0.50) + (10 x $5.00) + (6 x $25.00) = $15 + $50 + $150 = $215
If all 40M input is reported as one uncached number: (40 x $5.00) + (6 x $25.00) = $200 + $150 = $350
The ledger reads $350 against a $215 invoice, a 63% over-statement
The gap is 30M x ($5.00 - $0.50) = $135, exactly the cached discount not applied
Divide the gap by the cached volume and you recover the missing rate: $135 / 30 = $4.50

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

FigureWhat it meansConfidenceSource
$215 to $350modelled monthly cost of one workload, priced with the cached split and priced without itestimatedBoth 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 ledgercalculated$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.

1

Change one base URL.

Or drop in the Python or TypeScript library.

2

Find your most expensive conversation.

In the first session, not the first week.

3

Cost your next feature before you ship it.

Base URLhttp://localhost:4545/v1Your traffic keeps flowing if Culpa ever stops.

Why the bill went up

Example dashboard

Calls 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

$0$20$40$60$8024262830020406
user_384report_generatorconv_91fprompt_v1894,220 tokens3 retries$6.81

Most expensive users

user_384$38.42
user_119$21.07
user_562$14.90
user_204$8.30
user_871$5.10

Next week forecast

Best$180
Median$240
p90$310
p99$395

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.

Run a free scan

Keep reading


Sources: Anthropic pricing. Last reviewed 2026-08-02. Plain text version.