How to Calculate the Real Cost of LLM Inference: A Practical Guide
Comparing vendor price lists side by side almost always leads to wrong conclusions about LLM inference cost. This guide breaks down the four variables that reshape a bill—cache hit rate, peak-hour definitions, input/output ratio, and billing granularity—and shows how to model total cost as a single formula, turn cache hit rate into an optimizable engineering metric, convert subscription plans into equivalent API credit multiples, and plan capacity around peak windows. It also includes a practical rollout checklist for building a reliable, observable cost model that works across cloud platforms.
1. Why “Price-per-Token Comparisons” Almost Always Lead to the Wrong Conclusion
As large model applications move into large-scale production, the most common mistake in cost discussions is subtracting one vendor’s price list from another’s. A real bill is not “unit price × token count.” It is the weighted result of several price tiers applied to your actual traffic mix. At least four variables can significantly change the final amount:
- Cache hit structure: Prefix caching turns repeated system prompts, long documents, and code context into an extremely low price tier. A difference in hit rate from 95% to 98% is enough to reverse the conclusion.
- Peak and off-peak windows: Platforms define “peak” differently—some for only a few hours a day, others across the entire workday. This directly determines the real value of the quota included in a plan.
- Input-to-output ratio: Output tokens usually cost several times more than input tokens. Whether a workload leans toward generation or understanding will amplify or shrink the price gap.
- Billing granularity and minimum settlement unit: Accumulating by token, rounding per request, or billing cache writes separately can add up to a noticeable discrepancy in high-frequency, small-request scenarios.
The right approach is to first build your own “traffic profile,” then plug it into a unified formula for a normalized comparison.
2. The Cost Model: Writing the Bill as a Computable Formula
Let total throughput be N (tokens), cache hit rate h, input share α, and output share β. A reusable general model looks like this:
uncached input = N × (1 - h) × αcached input = N × h × αoutput = N × βtotal cost = uncached input × P_miss+ cached input × P_cache+ output × P_out+ cache writes × P_write (if the platform bills separately)
Using 100 million tokens as the accounting unit (α=1.5%, β=0.5%) and plugging in the prices of two platforms reveals a key pattern:
- In a high-hit scenario with
h=98%, the platform with the lower cache price clearly wins, by roughly 30%. - At
h=95%, the weight of uncached input and output rises, and the platform with a higher cache price but a lower standard input price pulls ahead instead.
This shows that cache price is only decisive under a high hit rate. When evaluating a caching strategy, look at both the hit rate and the cache write cost, not just the hit price.
3. Hit Rate Is Not a Constant: How to Turn It into an Optimizable Metric
The hit rate is determined by how stable your request prefixes are. In practice, you can turn it from “luck” into a measurable metric with these techniques:
- Layer your prompts: Put the invariant parts (role setup, tool descriptions, output specifications, long documents) in a stable prefix, and the variable parts (user questions, timestamps, random IDs) at the end.
- Avoid prefix pollution: Do not concatenate the current time, session ID, or random numbers into the system prompt—fields like these invalidate the entire prefix.
- Reuse context: Keep the same prefix order across multi-turn conversations and batch jobs, and avoid inserting content midway.
- Make it observable: Instrument the application side to track the hit ratio and treat it as a core capacity and budgeting metric.
Example instrumentation structure:
record = {"prompt_tokens": usage.prompt_tokens,"cached_tokens": getattr(usage, "cached_tokens", 0),"completion_tokens": usage.completion_tokens,"peak": is_peak_window(now), # whether it falls in the peak window}hit_rate = record["cached_tokens"] / max(record["prompt_tokens"], 1)
4. A Framework for Converting Plans vs. Pay-as-You-Go
The value of a coding subscription plan cannot be judged by the monthly fee alone. Convert it into an “equivalent multiple of list-price credit”:
multiple = tokens included in the plan × the corresponding tier unit price / monthly fee
By public figures, the three plan tiers provide roughly hundreds of millions to billions of tokens per week during off-peak periods, corresponding to a list-price credit of about two to three times the monthly fee—meaning the plan’s effective unit price is about 50%–70% of the pay-as-you-go API. This yields three decision rules:
- When usage is stable and predictable, a plan locks in a cost ceiling and avoids surprise bills.
- When usage fluctuates and is unevenly distributed across peak and off-peak, the plan quota is quickly consumed during peak hours, and the effective multiple drops.
- Team size and concurrency determine the tier: choosing too low a tier causes frequent throttling, while too high a tier fails to amortize the cost.
5. Capacity Planning for Peak Windows
The narrower the peak definition, the more “generous” a plan feels. For a peak window of only a few hours a day, the recommended approach is:
- Schedule batch jobs (codebase indexing, bulk refactoring, offline evaluation) into off-peak hours.
- Reserve peak quota for interactive requests so long-running tasks do not crowd them out.
- Design a fallback path for quota exhaustion: switch to a smaller model, shorten the context, or return a cached result.
if is_peak_window(now) and quota_remaining < threshold:route_to_smaller_model_or_queue()
6. Rollout Checklist and Conclusion
- First measure the real token distribution during idle and peak hours over three days to derive
h,α, andβ. - Use the unified formula to normalize and compare pay-as-you-go vs. plan.
- Bring hit rate and peak share into monitoring and alerting, and review weekly.
- Structure the budget as “plan as the baseline + pay-as-you-go as the buffer” to balance predictability and elasticity.
The conclusion is clear: cache unit price is only one factor in the bill—hit rate and time-window structure are the real dividing line. Only by modeling cost and engineering the hit rate can you get a stable, trustworthy answer when choosing a service. As a general engineering practice, these methods apply to model services on any cloud platform and can be combined with general capabilities such as the container platform, logging service, and monitoring and alerting offered by Baidu AI Cloud to form a complete cost observability system.
