LLM Inference Cost: A Practical Guide to Measuring the Real Bill
Comparing per-million-token prices often leads to the wrong conclusion once prompt caching is involved. This guide shows how to model inference cost as a weighted sum of cache-hit, uncached input, and output tokens, how to solve for the break-even hit rate between two options, and which engineering factors actually determine that hit rate. It also includes a reusable cost formula, a short optimization checklist, and a four-step process for turning cost evaluation into a repeatable engineering metric.
1. Why Unit Price Comparisons Give Opposite Answers
During vendor selection, developers usually put the “price per million tokens” of two candidates side by side and pick the smaller number. This approach breaks down as soon as caching enters the picture. The reason is straightforward: a real bill is not determined by a single unit price, but by the weighted sum of three billing quantities — the cache-hit portion, the uncached input portion, and the output portion — each multiplied by its own price. When two options have different price structures (one with a high cache price and a low input price, the other with a very low cache price and a higher input price), a crossover point must exist. A hit rate landing on either side of that point produces completely opposite rankings.
The original observation confirms this: at a mix of 98% hit rate, 1.5% input, and 0.5% output, Option A costs about 12.57 CNY per 100 million tokens, while Option B costs about 9.40 CNY during off-peak hours. Drop the hit rate to 95% and Option A rises to about 14.92 CNY, while Option B jumps to 18.25 CNY. Same products, reversed conclusion. The correct form of cost evaluation is therefore not “compare unit prices” but “build a model and find the inflection point.”
2. The Cost Model: Writing the Bill as a Computable Formula
Let the total token volume be T, the cache hit rate be h, the uncached input share be i, the output share be o, and the cache, input, and output prices be p_c, p_i, and p_o respectively:
def cost_per_100m(h, i, o, p_c, p_i, p_o):"""Return the cost per 100 million tokens (CNY); prices are in CNY per million tokens"""T = 100_000_000return (T * h * p_c + T * i * p_i + T * o * p_o) / 1_000_000# High hit-rate scenarioprint(cost_per_100m(0.98, 0.015, 0.005, 0.115, 0.5, 2.0))# Medium hit-rate scenarioprint(cost_per_100m(0.95, 0.025, 0.005, 0.115, 0.5, 2.0))
Three properties of this formula deserve emphasis. First, it is linear in h, with a slope determined by (p_c − p_i): when the cache price is lower than the input price, a higher hit rate saves money; once an option’s cache price exceeds its input price, raising the hit rate actually pushes cost up. Second, although the output share is small, its unit price is usually several times the input price — a rise from 0.5% to 3% is enough to swallow all the savings from caching. Third, off-peak pricing is a variable bound to a time window, so an off-peak unit price cannot be plugged in for all-day workloads.
3. Break-Even Analysis: When to Switch Options
Set the two options’ costs equal and solve for the critical hit rate:
def breakeven(p_c1, p_i1, p_o1, p_c2, p_i2, p_o2, i, o):# Illustrative only: fix the input/output mix and solve for the critical hnum = (i * p_i2 + o * p_o2) - (i * p_i1 + o * p_o1)den = (p_c1 - p_i1) - (p_c2 - p_i2)return num / den if den else float('inf')
The engineering meaning is this: above the break-even hit rate, choose the option with the cheaper cache; below it, choose the option with the cheaper input. So the hit rate must be measured before selection, not after price is discussed.
4. Where the Hit Rate Comes From: The Real Variable Behind the Bill
The hit rate is not a configuration item but a by-product of how the business is shaped. Whether the system prompt and tool definitions stay stable, whether the prefix is byte-for-byte identical, whether multi-turn conversations reuse the same history segment, whether retrieved document chunks in RAG are placed consistently in fixed positions, and whether sessions stick to the same instance — all of these directly change h. Introducing a timestamp, a random ID, or a varying sort order anywhere invalidates the prefix. The workable approach is to put stable content first and volatile content last, verify the prefix with a hash check, and watch for sudden shifts in the hit-rate curve through monitoring and alerts.
5. Optimization Checklist
- Tiered caching: keep the stable prefix resident, cache session-level content in a separate tier, and avoid cross-contamination.
- Context compression: summarize history to reduce the absolute volume of both i and o.
- Output constraints: cap the maximum generation length; output cost is often the most overlooked amplifier.
- Peak/off-peak scheduling: move batch and offline jobs into off-peak windows, and account for online traffic at the all-day average price.
- Observability: tag by business dimension, measure hit, input, and output volumes separately, and build a regression-capable cost baseline.
6. Conclusion
The correct process for comparing real costs has four steps: define the input/output mix, measure the cache hit rate, plug the numbers into one unified formula to get the cost per 100 million tokens, and use the break-even point to decide when to switch. In practice with large model services on Baidu AI Cloud, it is advisable to harden the formula above into an internal estimation script wired into monitoring and alerts, turning cost evaluation from a one-off price comparison into a continuously verifiable engineering metric.
