A Langfuse generation observation is logged with model='gpt-4o-mini', usage={input_tokens: 1200, output_tokens: 350}. The Langfuse price table for gpt-4o-mini lists input=$0.15 per 1M tokens, output=$0.60 per 1M tokens. Langfuse will compute the generation's totalCost attribute in USD. Predict the value (round to 6 decimal places).Compute each side as tokens divided by 1,000,000 times price per million, then sum. Input is 0.00018, output is 0.00021, total is 0.000390 USD.
Think of a calling plan with two rates: one for incoming minutes, one for outgoing. You used 1,200 incoming and 350 outgoing. The bill multiplies your incoming minutes by the incoming rate and your outgoing minutes by the outgoing rate, then adds them up. Langfuse does exactly that for tokens. Input tokens at the input rate, output tokens at the output rate, totalCost is the sum. The price table is a JSON file the backend reloads when the vendor changes prices, so today's bill uses today's rates.
Concept explanation~2 min read
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Concept explanation~2 min read
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
LLM cost calculation looks trivial at first glance: multiply tokens by price, add up the parts. The actual production architecture has a few non-obvious design choices that matter once you operate it at scale. Cost is derived in the backend, not the SDK. Prices are effective-dated. Different tenants can have different rates. Cached input tokens are priced separately. Knowing these properties is what separates 'can compute the bill' from 'can run an LLM platform'.
This deep dive walks through the arithmetic, then unpacks why the architecture is designed the way it is, what fails when you get it wrong, and how the same pattern shows up across Langfuse, LangSmith, Braintrust, and Datadog LLM Observability.
Mental model: the SDK reports raw token counts; the backend joins to a price table to derive cost. Centralizing cost calculation is what makes price updates, per-tenant pricing, and historical recompute possible.
Computing the answer step by step
The scenario has three quantities: token counts, prices, and the unit (per million).
Input cost
1,200 input tokens at 0.15 dollars per million tokens.
Fraction of a million: 1,200 / 1,000,000 = 0.0012.
Cost: 0.0012 multiplied by 0.15 = 0.00018 dollars.
Output cost
350 output tokens at 0.60 dollars per million tokens.
Fraction of a million: 350 / 1,000,000 = 0.00035.
Cost: 0.00035 multiplied by 0.60 = 0.00021 dollars.
Total
0.00018 + 0.00021 = 0.00039 dollars.
Rounded to six decimal places, 0.000390 USD. Langfuse stores this on the generation observation as totalCost.
The general formula
Where T_in and T_out are input and output tokens and p_in and p_out are the per-million rates.
Sanity check the magnitude
A reasonable single LLM call on a small model costs fractions of a cent. If your formula produces dollars per call, you used a per-thousand price as if it were per-million (off by 1,000). Operations dashboards typically include a per-trace cost alert to catch this kind of unit error early.
Situations where this technique stops working.
2–4 min · Everything important, quickly.
Real products, models, and research that use this idea.
- Langfuse computes totalCost at ingest using its built-in price table indexed by model and effective date.
- LangSmith follows the same model: token counts in, derived cost at ingest, dashboard rollups per project.
What an interviewer would ask next. Try answering before peeking at the approach.
QHow does prompt caching change the cost calculation?
Cached input tokens are priced at roughly 10x less than uncached. The usage object reports them separately. The price table must have two input rates (cached and uncached). Total cost is the sum of three terms: cached input, uncached input, and output.
Red flags & common mistakes
The phrases that signal junior thinking. Click to expand.
Red flags & common mistakes
The phrases that signal junior thinking. Click to expand.
Confusing per-1,000-tokens pricing with per-1,000,000-tokens pricing. 2026 frontier model prices are quoted per million, and using the wrong divisor produces a result 1,000x off.
60 second bullets to scan on the way to the call.
The formula for cost from token counts and per-million prices
Why 2026 prices are quoted per million, not per thousand
Primary sources. Browse if you want the original framing.
Same topic, related formats. Practice these next.