LLM Inference Cost: A Practical Guide to Calculating the Real Bill
Comparing per-million-token prices is usually where cost analysis goes wrong. This guide breaks LLM inference billing down into four variables—cache hit rate, input/output mix, peak-hour windows, and subscription tiers—and shows how to model total cost across a range of hit rates instead of a single number. It includes a normalized cost formula, a runnable Python snippet, a peak-traffic evaluation method, a subscription discount calculation, and a five-item optimization checklist for cutting cost without sacrificing output quality.
1. Why “Price Comparison” Often Misleads
When evaluating LLM inference services, the first thing most developers do is compare quotes per million tokens. But a real bill almost never equals “unit price × token count,” because billing is multi-dimensional: cached input, uncached input, and output are often priced separately, and some services also charge differently during peak and off-peak windows. Comparing just one of those numbers—the cache price, for example—can easily lead to a conclusion that is the opposite of what actually happens.
A more rigorous approach is to build a small cost model that treats hit rate, input/output structure, and time-of-day distribution as variables, then look at the range in which total cost is sensitive.
2. The Cost Model: Four Variables Behind the Bill
Define:
- Total tokens:
T - Cache hit rate:
h - Input share of total tokens:
r_in; output share:r_out(r_in + r_out = 1) - Unit prices:
p_cachefor cached input,p_infor uncached input,p_outfor output
Cost for a single billing cycle is then:
Cost = T * ( h * r_in * p_cache+ (1 - h) * r_in * p_in+ r_out * p_out )
This equation makes three things clear:
- The cache price only dominates total cost when
his high enough; at lowh, the uncached input term grows quickly. - Output price is usually far higher than input price, so a small increase in
r_outhas a non-linear effect. - Two services can differ by 2x on cache price alone, yet if their hit-rate ranges differ, the total-cost ranking can flip.
3. Hit-Rate Sensitivity: High-Hit vs. Medium-Hit Worlds
Using 100 million tokens as the normalization unit, and assuming an input/output structure of roughly 1.5% input and 0.5% output (i.e., the vast majority of requests are long-context reuse scenarios), two typical conclusions emerge.
High-hit scenario (about 98% hit rate): the cache term dominates outright, total cost is highly sensitive to the cache price, and the provider with the lower cache quote is clearly ahead—typically by a gap of roughly 25% to 35%.
Medium-hit scenario (about 95% hit rate): the uncached input term is amplified to 5x weight, and the cost structure shifts from “cache-dominated” to “shared between cache and uncached input.” Here the provider with the higher cache price but a different input-price structure can end up more than 20% more expensive overall, and the ranking flips.
The engineering implication is clear: don’t decide on a single hit-rate number, decide on a range. Compute total cost at h = 90%, 95%, and 98%, and see where the crossover falls.
def cost_per_100m(h, r_in=0.015, r_out=0.005, p_cache=0.115, p_in=1.0, p_out=2.0):# Unit: CNY per 100M tokens; p_* are CNY per 1M tokens * 100return 1e8 * (h * r_in * p_cache + (1 - h) * r_in * p_in + r_out * p_out) / 1e6for h in (0.90, 0.95, 0.98):print(h, round(cost_per_100m(h), 2))
4. Time-of-Day Strategy: Peak Window Length Decides Plan Value
Many inference services treat “peak hours” differently, and the difference usually lies not in the size of the discount but in the absolute length of the peak window. A definition limited to 4 hours a day covering weekday afternoons has a very different impact on the monthly bill than one covering most of the day—potentially larger than the unit-price difference itself.
How to evaluate:
- Measure your team’s actual request-time distribution to get the share of requests falling in peak hours,
q. - Estimate with
Monthly cost = plan price + peak overflow volume × peak unit price. - If
qis small (say below 15%), a time-of-day discount is nearly equivalent to an all-day discount.
5. Converting Subscription Plans to Pay-as-You-Go
Subscription plans (tiered as basic/professional/advanced, for example) are essentially “prepay for a discount.” The conversion is to translate the plan into an equivalent amount of list-price API credit, then compute the discount rate:
Discount rate = plan price / (tokens included in plan × list unit price)
In long-context, high-hit scenarios, the discount rate commonly lands in the 50%–70% range—meaning the plan works out to roughly 50–70% of list price. Note, however, that plans usually come with usage caps and peak-hour restrictions; usage beyond those reverts to pay-as-you-go billing. The effective discount rate should therefore be weighted across “in-plan usage + overflow usage.”
6. Optimization Checklist: Cut Cost Without Sacrificing Quality
- Raise context reuse: put stable system prompts, tool definitions, and knowledge snippets at the front so the prefix can be cached.
- Control output length: output is the most expensive part, so set a sensible maximum output limit and apply structured constraints.
- Shift load off peak: schedule batch and offline jobs into off-peak windows, and keep interactive requests in peak windows.
- Route by tier: send simple requests to smaller models and complex requests to larger ones, with routing thresholds driven by monitoring metrics.
- Build observability: use logging and monitoring/alerting to track hit rate, token distribution, and time-of-day distribution so the cost model keeps getting recalibrated.
7. Conclusion
Real cost = billing structure × workload characteristics × time-of-day distribution. Developers should first use a normalized model to compute the cost range across several hit-rate tiers, then layer in time-of-day and plan factors as weights, and finally close the loop by correcting parameters with observability data. Baidu AI Cloud provides end-to-end support across model services and general cloud capabilities such as monitoring and logging, on which a sustainable cost-governance process can be built.
