How to Calculate the Real Cost of LLM Inference: A Practical Guide
Comparing per-token prices across LLM providers almost always leads to wrong conclusions. This guide introduces a three-part cost model based on cache hit rate and input/output ratio, explains why cache hit rate is the real inflection point of the cost curve, shows how to run sensitivity scans and break-even analysis for subscription plans versus pay-as-you-go, and outlines a repeatable measurement workflow for capacity planning.
1. Why “Price-per-Token Comparisons” Almost Always Lead to the Wrong Conclusion
Once an LLM application moves into production, its cost structure shifts from a one-off experimental expense to a core operating cost that grows linearly with call volume. At this point, the most common analytical mistake is to put the cached-input prices of different service plans side by side and conclude that one plan is “twice as expensive.” Such a comparison is unreliable because it assumes that every token has the same billing identity. Real inference traffic is never homogeneous.
A typical request passes through prefix matching, cache reuse or recomputation, context appending, and result generation. Billing schemes usually divide tokens into mutually exclusive categories: cache-hit input, cache-miss input, and model output. Their unit prices often differ by an order of magnitude, so the sensitive variable for total cost is not the listed price but how traffic is distributed across these categories.
Put differently, the unit price is only a coefficient in the model; the hit rate and the input/output mix are the independent variables. Any conclusion drawn without those variables applies only to one very narrow slice of workload.
2. Building a Unified Three-Part Cost Model
For a rigorous judgment, first abstract the bill into a computable function. Let the total number of processed tokens be N, the cache hit rate be h, the input share be α, the output share be β, and the three unit prices be p_cache, p_in, and p_out:
def cost_per_100m(n=100_000_000, h=0.98, alpha=0.015, beta=0.005,p_cache=0.115, p_in=8.0, p_out=28.0):"""Estimate inference cost per 100 million tokens (CNY).p_* is in CNY per million tokens."""n_cache = n * hn_in = n * alphan_out = n * betareturn (n_cache * p_cache + n_in * p_in + n_out * p_out) / 1_000_000
The value of this function lies in three things: a consistent measurement basis, explicit variables, and the ability to sweep parameters in batches. Fill in the prices of different plans using the same set of parameters and the comparison becomes meaningful; run a sensitivity sweep on h and you can see the range in which the ranking flips.
Note that apart from the cache price, all values above are illustrative. Any real calculation must use the billing rules published by the chosen service for the current period, not numbers copied from someone else’s article.
3. Cache Hit Rate: The Real Inflection Point of the Cost Curve
Sweeping h as the only variable reveals a counterintuitive phenomenon: two cost curves cross.
In high-hit-rate scenarios (close to 98%, for example), the vast majority of tokens are billed at the cache price, and the plan with the lower cache price wins. When the hit rate drops to a medium level (around 95%, for example), the miss portion is amplified roughly twentyfold, its unit price difference begins to dominate total cost, and the ranking may reverse completely. The two sets of numbers described in the original text are essentially two samples of this crossover point.
The engineering implication is clear: any cost conclusion must come with a hit rate assumption, otherwise it is not transferable. The hit rate is not a configuration item but is determined by the shape of the business:
- A fixed and lengthy system prompt yields a naturally high hit rate.
- In multi-turn conversations, history is appended stably, so the hit rate rises with each turn.
- In RAG scenarios, if retrieved snippets change every turn, the prefix is broken and the hit rate drops significantly.
- With tool calls and structured output, concatenating dynamic fields after the system prompt causes large-scale cache invalidation.
The right way to raise the hit rate is therefore to adjust prompt layout, placing stable content first and volatile content last:
[Stable] role definition / output spec / tool schema / long document body[Semi-stable] conversation history[Volatile] this turn's retrieved snippets / timestamps / user input
Any dynamic variable inserted in the middle of the stable segment makes everything after it non-reusable. This is the most common and most easily overlooked cost leak in practice.
4. Input/Output Mix and the Amplification Effect in Long-Context Scenarios
α and β determine the weight of the output-side bill. Tasks such as code generation, structured extraction, and long-form rewriting have a high output share, so a small difference in output unit price is multiplied; classification, tagging, and summarization tasks produce very short output, and their cost is determined almost entirely by the input side.
Long-context scenarios need one extra consideration: cache billing is usually tied to mechanisms such as the minimum reuse granularity and cache lifetime, and the exact rules vary by service. If the interval between requests exceeds the cache survival window, the hit rate collapses periodically, and the measured average is far below the theoretical value. Capacity planning should be based on measured distributions rather than peak assumptions.
5. Break-Even Analysis: Subscription Plans vs. Pay-as-You-Go
Subscription plans follow pricing logic entirely different from pay-as-you-go: they package uncertain token consumption into a fixed monthly fee, grant quota weekly or per cycle, and distinguish peak from off-peak hours. To decide whether a purchase is worthwhile, calculate the “equivalent multiple of list price”:
def plan_ratio(month_fee, weekly_tokens, api_price_per_m, weeks=4.345):"""Estimate the plan's discount ratio versus pay-as-you-go"""monthly_tokens = weekly_tokens * weeksequivalent = monthly_tokens / 1_000_000 * api_price_per_mreturn month_fee / equivalent, equivalent
The sample in the original text shows that the equivalent unit price of plans at different tiers lands at roughly 50% to 70% of list price, and a higher tier does not necessarily mean a deeper discount. This shows that the real value of a plan comes not from the discount rate but from whether the quota is fully consumed. If a tier uses only 60% of its quota, its equivalent unit price immediately climbs back to near or above the pay-as-you-go level.
Time-of-day strategy affects the conclusion as well. Platforms define peak windows very differently: some count only a few hours per day, others cover the entire workday. The narrower the peak window, the more usable quota the plan offers, and the friendlier it is to interactive development. When choosing, treat “peak window length × weekly quota” as a composite metric rather than looking at the listed price alone.
6. A Practical Measurement Workflow
- Sample: instrument each request to record input, output, and cache-hit token counts, forming a real distribution.
- Group: segment by business scenario and measure
h,α, andβseparately, so a global average does not hide the long tail. - Substitute: use the unified model to compute the unit cost of each plan under the measured distribution.
- Sweep: run a sensitivity analysis on
hto find the critical hit rate at which the ranking reverses. - Validate: verify prediction error with a small amount of real traffic; if the deviation exceeds expectations, return to step 1 and check the instrumentation definitions.
- Review: recheck monthly, because prices and quota rules change and historical conclusions have a limited shelf life.
7. Conclusion
Comparing LLM inference costs must rest on three premises: the same measurement basis, the same distribution, and the same time window. The unit price is only a surface signal; the cache hit rate and the input/output mix are the main variables that determine the bill, and the merits of a subscription plan depend on quota utilization and peak window width rather than the nominal discount. Building the model above into your team’s capacity evaluation process turns vendor selection from subjective impression into a reproducible numerical experiment, and makes it easy to answer questions such as “how much do we scale out, and how much does cost increase” as the business grows. For more complete engineering support, you can combine general capabilities such as container platforms, log services, and monitoring and alerting on Baidu AI Cloud to set up instrumentation collection and a cost dashboard.
