How to Calculate the Real Cost of LLM APIs: A Practical Cost Model
Comparing per-million-token prices is the most common way to estimate LLM API costs, but it often leads to wrong conclusions because it ignores cache hit rate, input-output ratio, and peak-hour pricing. This article breaks down the three elements of a reusable cost model, provides a Python function for same-basis comparisons, explains sensitivity to cache hit rate, and shows how to convert subscription plans into equivalent pay-as-you-go costs. Readers will learn to build their own cost model and evaluate any model or price change in minutes.
1. Why “Price Comparison” Often Gets the Math Wrong
When evaluating the cost of adopting an LLM API, the most common approach is to place the “price per million tokens” of two options side by side. The flaw in this method is that it only looks at the most surface-level part of the billing items and ignores the composition of real requests.
Tokens in a live request usually fall into three categories: the cached context portion, the new uncached input portion, and the output generated by the model. Their unit prices often differ by an order of magnitude, and the cached portion may cost only a few percent of the input price. As a result, when system prompts, long documents, or code repository context are reused repeatedly in a business scenario, the cache hit rate becomes the dominant variable on the bill.
The conclusion is straightforward: comparing unit prices without accounting for cache hit rate and input-output ratio will almost certainly lead to the wrong result. The right approach is to build your own cost model first, then plug candidate options into it.
2. The Three Elements of a Cost Model
1. Cache Hit Rate
The cache hit rate is the proportion of reused context in total input. It is determined by the business pattern, not by the vendor:
- High-hit scenarios: fixed system prompt plus repeated Q&A over long documents, code completion (same repository context), reuse of historical messages in multi-turn conversations;
- Low-hit scenarios: entirely new context for each request, batch summarization, one-off classification of logs or text.
In engineering practice, the typical hit range is roughly 70%–98%. Note that the higher the hit rate, the more sensitive the bill is to the “cache unit price”; the lower the hit rate, the closer the bill is to the conventional structure of “input price + output price.”
2. Input-Output Ratio
Generation tasks have a high output proportion, while understanding and extraction tasks have a high input proportion. Empirically, output may account for 20%–40% in conversational workloads, while structured extraction and moderation/classification workloads often have output below 5%. The output unit price is usually several times the input price, so changes in the ratio often affect total cost more than differences in unit price itself.
3. Time-of-Day Structure (Peak/Off-Peak)
Some platforms apply differentiated pricing for periods of high and low compute demand. The key is “how long the peak period is defined as”: if peak is limited to a few hours on workdays, most traffic naturally falls into off-peak windows; if peak covers the entire workday daytime, the cost structure is completely different. For capacity planning, overlay the time distribution of business traffic rather than assuming a uniform distribution.
3. Reusable Calculation Code
Writing the three elements above into a function allows same-basis comparison of any option:
def cost_per_100m(total=100_000_000,cache_hit=0.98,input_ratio=0.015,output_ratio=0.005,p_cache=0.00115, # CNY per 1K tokens, placeholder examplep_input=0.05,p_output=0.20):c = total * cache_hiti = total * input_ratioo = total * output_ratioreturn c * p_cache / 1000 + i * p_input / 1000 + o * p_output / 1000
In the example above, the three token proportions sum to less than 100%, which is an implicit assumption of the original framing (i.e., the vast majority of input is absorbed by cache). In actual modeling, ensure cache_hit + input_ratio + output_ratio = 1, otherwise the total will be underestimated or overestimated. A more rigorous approach is to derive “uncached input” directly from 1 - cache_hit to avoid definition drift.
4. Cache Hit Rate Sensitivity Analysis
Holding other variables fixed and varying only the hit rate reveals a crossover between two curves:
- At an extremely high hit rate (around 98%), the bill is dominated by the cache unit price, and the option with the lower cache price wins;
- When the hit rate falls to around 95% or below, the weight of uncached input and output rises quickly, and the option with the lower input price takes the lead.
This explains a common confusion: the same set of products can yield completely opposite “cheaper” conclusions under different workloads. Therefore, selection should use range-based estimation rather than point estimation: output a “hit rate–cost” curve, find the real operating point for your business, and leave cost elasticity for hit rate fluctuations.
5. How to Convert Subscription Plans
In addition to pay-as-you-go billing, many platforms offer monthly subscription coding/inference plans, usually granting quotas weekly or per cycle and applying separate throttling during peak hours. A three-step conversion is recommended:
- Multiply the official quota (tokens/week) by the number of cycles in the month to get the monthly available volume;
- Use the same set of unit prices to convert that quota into an “equivalent pay-as-you-go amount,” yielding the discount range of the plan versus pay-as-you-go (commonly 50%–70%);
- Multiply the peak-hour duration proportion by the business traffic distribution to estimate the proportion of requests blocked by throttling; this should be counted as hidden cost.
Illustration:
Equivalent pay-as-you-go amount = weekly quota × 4.3 × blended unit priceDiscount rate = plan monthly fee / equivalent pay-as-you-go amountEffective cost = plan monthly fee / (available quota × (1 - peak throttling rate))
6. Implementation Recommendations
- Instrument first, then select: measure the real cache hit rate, input-output ratio, and time distribution; these three data points are more valuable than any benchmark leaderboard;
- Use a blended unit price for a unified basis, combining cache, input, output, and time-of-day into a single number before comparing;
- Pay attention to quota validity and throttling policies to avoid “cheap on paper, throttled in practice”;
- Improve hit rate on the architecture side: stabilize system prompt prefixes, reuse long context, and design conversation truncation policies reasonably;
- If you need a balance of elasticity and cost control, you can build a cost observability and scheduling system using relevant general capabilities from Baidu AI Cloud (such as monitoring and alerting, log service, and container platform) to route traffic by business priority.
Once you master this model, any new model or price adjustment can be evaluated on the same basis in ten minutes, rather than being led by a single unit price number.
