How to Calculate the Real Cost of LLM Inference: A Practical Guide
Comparing per-token prices in isolation almost always leads to wrong conclusions about LLM inference cost, because the final bill is a weighted sum of several billing items whose weights are set by your workload, not by the vendor's price list. This guide builds a reusable cost model based on cache hit rate and input/output ratio, shows how to find the crossover hit rate between two pricing plans, explains how time-based discounts and monthly subscription quotas convert into an equivalent per-million-token price, and lays out an optimization order that starts with hit rate and ends with plan selection. You will also find a list of common estimation mistakes to avoid.
1. Why “Unit Price Comparison” Almost Always Leads to the Wrong Conclusion
When estimating LLM inference costs, the most common mistake is to focus on a single unit price, such as the cache read price or the output price per million tokens. The problem is that the bill is a weighted sum of multiple billing items, so any individual price only means something under a given set of weights. Those weights are determined by your business workload, not by the vendor’s price list.
The cost of a single request can usually be abstracted into four categories: uncached input, cache-hit input, output (generated) tokens, and, on some platforms, a separately listed inference acceleration add-on. The unit prices of these four often differ by an order of magnitude, so even a few percentage points of change in the hit ratio can reverse the direction of the total cost.
The conclusion is straightforward: comparing unit prices without accounting for hit rate and input/output mix has no decision-making value.
2. Building a Reusable Cost Model
Let the total volume be N (tokens), the cache hit rate be h, the input share be a, and the output share be b (a + b = 1). Then:
uncached input = N * a * (1 - h)cached input = N * a * houtput = N * btotal cost = uncached input * P_uncached+ cached input * P_cached+ output * P_output
Written as code, so you can put different options into the same table for comparison:
def cost(total, hit, in_ratio, p_uncached, p_cached, p_out):out_ratio = 1 - in_ratiouncached = total * in_ratio * (1 - hit)cached = total * in_ratio * hitoutput = total * out_ratioreturn uncached * p_uncached + cached * p_cached + output * p_out
This model has three input assumptions that must be stated explicitly, otherwise the results cannot be reproduced:
- Hit rate h: determined by how repetitive the workload is, not a tunable parameter. Fixed system prompts, repeatedly read code repositories, and multi-turn question answering over long documents produce high hit rates; conversations whose context changes drastically each turn produce low hit rates.
- Input/output mix a/b: code completion workloads have a very low output share, while long-form generation and multi-step AI agent reasoning raise the output share significantly.
- Billing conventions: whether cache is billed separately by read volume, whether cache writes incur an extra charge, and how the minimum billing unit is rounded.
3. Hit Rate Sensitivity: The Crossover Point Is What Drives the Decision
Work through an illustrative set of unit prices (all normalized to per million tokens): Option A has uncached input at 4.0, cache reads at 0.115, and output at 16.0; Option B has uncached input at 2.0, cache reads at 0.05, and output at 8.0. Take N = 100 million, input share 1.5%, output share 0.5%:
| Hit rate | Option A cost | Option B cost | Lower cost |
|---|---|---|---|
| 98% | ~12.6 | ~9.4 | B |
| 95% | ~14.9 | ~18.3 | A |
The option with the higher cache read price is actually more expensive in the high-hit-rate range; when the hit rate drops by 3 percentage points, the conclusion reverses completely. The reason is that the uncached input and output prices of the two options stand in a multiple relationship to each other, and a falling hit rate pushes requests toward the more expensive billing items, while options with different price structures react to this shift with different sensitivity.
Engineering implication: instead of asking “which vendor is cheaper,” calculate the crossover hit rate of the two cost curves, then check which side of it your real hit rate falls on. If you cannot measure it, run a small-scale instrumentation pass to collect statistics first, and only then discuss vendor selection.
4. Converting Time-Based Pricing and Subscription Plans
Beyond pay-as-you-go billing, common industry practices also include time-of-day discounts and monthly subscription quotas. Stacked together, they make the “price list” completely lose its reference value.
The key to time-based pricing is not the size of the discount but the length of the peak window and the time zone it sits in. If one option has a peak window of 4 hours per weekday and another has a longer window, the same batch workload differs enormously in scheduling freedom. Tasks can be classified by how deferrable they are into online interaction and offline batch processing, and the latter should be moved out of peak windows as much as possible.
Subscription plans should be converted uniformly into an “equivalent price per million tokens”:
equivalent unit price = monthly plan fee / monthly available tokensmonthly available = weekly quota * 4.33
Take three monthly tiers of 118 / 538 / 1078 yuan with corresponding weekly quotas of about 317 million / 1.90 billion / 4.433 billion tokens. The equivalent unit prices are about 1.24 / 0.94 / 0.81 yuan per million tokens, only 50%–70% of the original pay-as-you-go API rate. This shows that the plan discount is real, but it is tied to a quota ceiling and time-window constraints, not simply a lower price.
5. Put the Model Back into the Full System Before Adding Up the Total
Inference calls are only part of the bill. A practical cost optimization order:
- Optimize the hit rate first: stabilize and front-load system prompts, reuse long contexts, and add semantic caching for repeated queries. This is the step with the largest payoff and no quality sacrifice.
- Then optimize the input/output mix: compress retrieved snippets, cap maximum generation length, and use constrained decoding for structured output to cut wasted tokens.
- Then do time-based scheduling: move offline batch processing, evaluation, and index-building tasks out of peak hours.
- Only then choose unit prices and plans: plug your measured hit rate and mix into the model, and pick the combination with the lower equivalent unit price and sufficient quota.
- Add observability: use logging services and monitoring alerts to record token consumption, hit rate, and failed retries per business dimension. Retries are a severely underestimated cost source.
If you use Baidu AI Cloud capabilities, the same method applies: use general-purpose services (such as logging, monitoring alerts, object storage, and container platforms) to carry instrumentation and scheduling, and turn the model above into an internal cost dashboard for your team rather than relying on any single price list for judgment.
6. Common Mistakes Checklist
- Comparing only cache prices while ignoring the multiple relationship between uncached input and output prices.
- Budgeting with an ideal hit rate without a sensitivity range.
- Ignoring rounding losses from cache writes and the minimum billing unit.
- Treating plan quotas as unlimited and overlooking peak-hour limits and the billing switch after overage.
- Failing to count repeated billing caused by retries and overly long contexts.
Once you master this model, any new model or new plan can be converted into an equivalent price in the same unit within ten minutes, turning selection from “feels cheaper” into a reproducible calculation.
