How to Calculate the Real Cost of LLM Inference: A Practical Guide
Comparing per-million-token prices is misleading once prompt caching is involved, because a bill is the sum of several independent line items. This guide breaks LLM inference cost into cache-hit input, cache-miss input, output tokens, and optional request fees, then shows how hit rate and input/output ratio can reverse the ranking of two pricing plans. It also covers practical ways to raise cache hit rates, how to evaluate monthly subscription plans against pay-as-you-go pricing, and a short checklist for production rollout.
1. Why Per-Token Price Comparisons Often Lead to Wrong Conclusions
When evaluating LLM inference services, the first thing most developers do is put two vendors’ “price per million tokens” side by side and pick the smaller number. Once prompt caching became common, this approach became seriously misleading. The reason is simple: a request is not billed at a single price but as the sum of several independent line items, and the item that varies most is usually not the model’s base price but the portion of context served from cache.
The pattern in the source material is typical: one model family charges roughly CNY 0.115 per million tokens for cache reads, another roughly CNY 0.05, so the first looks more than twice as expensive. But once you plug the cache hit rate and the input/output mix into a single formula, the conclusion flips depending on the hit rate. In high-hit-rate scenarios the cheaper cache option wins; when the hit rate drops to around 95%, the plan with the higher cache price becomes the better deal. Cost comparisons therefore have to be built on the same workload profile — quoting unit prices without a workload is meaningless.
2. The Cost Model: Breaking the Bill into Computable Parts
Billing for mainstream inference services can usually be abstracted into four items: cache-hit input, cache-miss input, output (generated) tokens, and, for some services, a per-request surcharge. Define:
p_cache: cache read unit pricep_in: cache-miss input unit pricep_out: output unit priceh: cache hit rater_in: share of input in total tokensr_out: share of output in total tokens
The expected cost per unit of tokens is then:
cost = p_cache * h * r_in+ p_in * (1 - h) * r_in+ p_out * r_out
Plugging in a profile of 98% hit rate, 1.5% input, and 0.5% output, the two parameter sets yield roughly CNY 12.57 and CNY 9.40 per 100 million tokens. Change the hit rate to 95% and they become roughly CNY 14.92 and CNY 18.25. The crossover falls between 95% and 98%, which is exactly the range that matters for selection.
In practice, it is worth turning this formula into a script instead of estimating by hand every time:
def estimate(p_cache, p_in, p_out, h, r_in=0.015, r_out=0.005, tokens=1e8):per = p_cache * h * r_in + p_in * (1 - h) * r_in + p_out * r_outreturn per * tokens / 1e6
3. Cache Hit Rate: The Most Sensitive Variable in the Model
The hit rate is not decided by the server alone; it depends heavily on how you call the API. Practices that reliably improve it include: keeping invariant content such as system prompts, tool definitions, and long documents at the front of the request prefix; avoiding volatile fields like timestamps or random IDs inserted mid-conversation; limiting the number of concurrent sessions so the cache is not constantly evicted; and appending incrementally in multi-turn conversations rather than rewriting the whole context each time.
Note that caches usually have a time-to-live window, so a call after a long idle period degrades to a miss. For offline batch jobs, submit requests that share the same prefix together; for interactive scenarios, budget with a hit-rate expectation one notch lower so the bill does not exceed expectations.
4. Peak Hours and Subscription Plan Conversion
Beyond pay-as-you-go, many platforms offer monthly coding subscriptions whose real value depends on three parameters: the plan price, the included token quota, and peak-hour restrictions. Definitions of peak hours vary widely — some platforms count the entire workday, others restrict only about four hours a day, and the latter is clearly friendlier to developers.
The conversion method uses the “equivalent list-price API quota” as the denominator:
discount rate = monthly plan fee / (list-price API cost of the included quota)
In the source material, three plan tiers (CNY 118, CNY 538, and CNY 1078 per month) are equivalent, off-peak at a 98% hit rate, to roughly CNY 160, CNY 955, and CNY 2229 of list-price usage — meaning the plans cost about 50%–70% of pay-as-you-go. In other words, if your monthly usage sits steadily within the quota and your working hours avoid the peak, a plan saves 30%–50%; if usage fluctuates a lot or concentrates in peak hours, pay-as-you-go is more controllable.
5. Practical Recommendations
- Sample real traffic for a week first, measure
h,r_in, andr_out, then plug them into the formula instead of copying the official example ratios. - Build a cost dashboard that groups token consumption and spend by business line, with budget thresholds wired to logging and alerting.
- For cost-sensitive paths, prioritize prefix stabilization — it usually has the highest return on effort.
- When selecting a service, add “peak-hour duration,” “cache TTL policy,” and “whether quota rolls over” to your evaluation checklist; they affect the final bill as much as unit price does.
- If you use Baidu AI Cloud’s model services, run the same general formula with your own numbers and rely on load-test data rather than list prices for the decision.
