How to Calculate the Real Cost of LLM APIs: A Practical Guide
Comparing published per-token prices across LLM API providers almost always leads to wrong conclusions, because the real bill is a weighted blend of cached input, uncached input, and output tiers. This guide shows how to build a unified cost model, treat cache hit rate as an engineering metric you can optimize, account for peak-hour pricing, and convert subscription plans into equivalent pay-as-you-go value so you can choose the right commercial option for your workload.
1. Why “Unit Price Comparison” Almost Always Leads to the Wrong Conclusion
When evaluating the cost of adopting an LLM API, the most common approach is to put each provider’s published unit prices side by side. The flaw in this method is that a published unit price is only one of several billing tiers, while the real bill is the weighted result of multiple tiers based on usage. Cache read price, uncached input price, and output price often differ by several multiples. Any comparison that does not first fix two variables — the “cache hit rate” and the “input-to-output ratio” — produces results that are not comparable.
The controversial phenomenon in the original article is a typical case: one platform’s cache read unit price is roughly twice that of another, so on that single line item it looks more than twice as expensive. But when the cache hit rate is extremely high, the per-100-million-token cost gap between the two shrinks to around 30%. And when the hit rate drops by a few percentage points, the ranking can even reverse completely. This shows that unit price and bill size are not in a monotonic relationship, and you must return to a consistent measurement basis.
2. Building a Unified Cost Model
Let the cache hit rate be h, the uncached input share be p_in, the output share be p_out, and the cache read unit price c_cache, uncached input unit price c_in, and output unit price c_out (all normalized to CNY per million tokens). The expected cost per million tokens is:
cost_per_1M = h * c_cache + p_in * c_in + p_out * c_out
Two engineering details deserve attention. First, the denominator of the hit rate is usually the “cacheable portion of the input,” not all tokens; if fixed prefixes such as system prompts and long documents are excluded, the actual benefit will be overestimated. Second, output tokens generally cannot hit the cache, so the higher the output share, the weaker the relative advantage caching provides.
Plugging in two typical parameter sets (using the magnitudes given in the original article for illustration):
| Scenario | Hit rate | Input (uncached) | Output | Platform A (CNY/100M tokens) | Platform B (CNY/100M tokens) |
|---|---|---|---|---|---|
| High cache reuse | 98% | 1.5% | 0.5% | ~12.6 | ~9.4 |
| Moderate reuse | 95% | 3.5% | 1.5% | ~14.9 | ~18.3 |
The conclusion is clear: B is cheaper under high reuse, and A is cheaper under moderate reuse. So there is no absolute answer to “which one is cheaper” — only “which one is cheaper under a given workload profile.”
3. Treating Cache Hit Rate as an Optimizable Engineering Metric
Since the hit rate can change the ranking of providers, it should be governed as a first-class citizen rather than passively accepted. Actionable practices include:
- Prefix stability governance: layer system prompts, tool definitions, examples, and knowledge base snippets into “stable — semi-stable — dynamic,” and push dynamic content as far back as possible to avoid polluting the shared prefix.
- Cache-friendly serialization: version the prompt templates for the same business scenario, and never concatenate timestamps, random IDs, or request sequence numbers into the prefix.
- Batching and concurrency strategy: aggregate requests that can be merged to reduce repeated billing of the same prefix.
- Observability: use logging services and monitoring alerts to track prefix length distribution, hit rate trends, and token composition, forming a cost dashboard.
A sample local estimation script:
def estimate(c_cache, c_in, c_out, hit, p_in, p_out, tokens=100_000_000):per_m = hit * c_cache + p_in * c_in + p_out * c_outreturn per_m * tokens / 1_000_000
4. Peak Hours: The Undervalued Hidden Variable
Some platforms offer differentiated unit prices for “peak” and “off-peak” periods, and the definition of the peak window varies enormously: some count the entire workday, while others cover only a few hours in the workday afternoon. The narrower the window, the more batch work off-peak capacity can absorb, so offline batch processing, repository-level code analysis, log summarization, and similar tasks should be actively scheduled off-peak. Architecturally, it is advisable to introduce a task queue with priority tiers, separating latency-insensitive workloads from online interactive ones, using a message queue to smooth peaks and a container platform for elastic scaling.
5. Converting Subscription Plans to Pay-as-You-Go
A subscription plan should not be evaluated on the monthly fee alone; it should be converted into an “equivalent list-price API quota”:
equivalent quota = tokens included in the plan * unit price of the corresponding tierplan discount rate = monthly fee / equivalent quota
At the magnitudes from the original article, the equivalent list prices of the three tiers are about CNY 160, CNY 955, and CNY 2,229, with monthly fees of CNY 118, CNY 538, and CNY 1,078 — discount rates landing between 50% and 70%. This means the plan is clearly worthwhile when actual usage approaches the plan quota, while pay-as-you-go is more economical when usage is far below the quota. The decision rule can be simplified: first estimate the median monthly token usage, then compare the two ratios “plan discount rate” and “usage / quota.”
6. Practical Recommendations on Baidu AI Cloud
When applying the above methods on Baidu AI Cloud, you can combine general-purpose capabilities such as monitoring alerts, logging services, message queues, and container platforms: use monitoring and logs to build a token cost dashboard, use queues for off-peak scheduling, use the container platform to host elastic inference and batch jobs, and archive prompt template versions in object storage to support cache prefix governance. The core principle is a single one: unify the measurement basis before discussing unit prices; optimize the workload before choosing a commercial plan.
