LLM API Cost Estimation: Cache Hit Rate and Pricing Model Impacts
This article explains why comparing only a single API unit price can lead to incorrect cost conclusions. It introduces a four-variable cost model covering cache hit rate, input-to-output ratio, billing windows, and subscription utilization; provides a worked Python example; and outlines a practical workflow for estimating, validating, and reviewing LLM API costs.
Why Comparing Only Unit Prices Leads to Wrong Conclusions
Model services usually publish multiple unit prices, including standard input, cache-hit input, and output. Some also distinguish between peak and off-peak periods. Looking at only one of these prices, such as the cache-hit unit price, can easily lead to a one-sided conclusion. A real bill is the weighted sum of each usage type and its corresponding unit price.
A particular unit price may be high, but if it accounts for a very small share of total usage, its overall cost impact may not be large. Conversely, a lower-priced item that accounts for a large share of usage can have an amplified effect on total cost. Therefore, cost evaluation should first establish a consistent calculation basis, then make comparisons across options.
The Four Variables in the Cost Model
1. Cache Hit Rate
A cache hit means that the prefix of a request matches a previous request, so the server can reuse the already computed portion and bill it at a lower unit price. Scenarios such as multi-turn conversations, long system prompts, and fixed knowledge-base retrieval often have relatively high hit rates. For every percentage point the hit rate drops, the portion originally billed at the cache price shifts to the full input price, and the difference scales linearly with call volume. Estimations should use the actual hit rate recorded in logs, not an assumed value.
2. Input-to-Output Ratio
Output tokens usually have a significantly higher unit price than input tokens. In question-answering scenarios, input is long and output is short, so cost is mainly concentrated on the input side. In code generation, long-form writing, and similar scenarios, output accounts for a larger share, which changes the cost structure. These two ratios should be calculated separately by endpoint and business line, because different functions under the same account may have very different ratios.
3. Billing Windows
Some services use time-based pricing. Peak-hour unit prices may be higher, or plan quotas may be consumed faster. Estimations should distribute requests by time window and calculate each window separately rather than simply applying an all-day average. If the workload includes offline batch jobs, scheduling them to run during lower-priced periods can often deliver meaningful savings with relatively low implementation effort.
4. Subscription Plans vs. Pay-as-You-Go
Monthly or weekly subscription plans fix costs, but quotas usually have upper limits and may also have window limits, concurrency limits, or usage-term restrictions. To determine whether a plan is cost-effective, the key is to calculate the effective unit price: the plan fee divided by the actually available token quantity.
Let the monthly plan fee be F yuan, the monthly quota be Q million tokens, and the pay-as-you-go unit price be P yuan per million tokens. When actual usage as a share of the quota is u, the ratio of the plan cost to the pay-as-you-go cost for the same usage is:
F / (u × Q × P)
Setting this ratio equal to 1 gives the break-even utilization rate:
u* = F / (Q × P)
When actual utilization is below u*, pay-as-you-go is cheaper. When actual usage exceeds the quota, the excess is usually billed separately, so both average usage and peak usage need to be monitored.
Worked Example
The following example uses fictional parameters solely to demonstrate the calculation method. It does not represent the real pricing of any vendor.
def blended_cost_per_million(cache_ratio, input_ratio, output_ratio,price_cache, price_input, price_output):"""Calculate the weighted cost per million tokens (yuan).The three ratios must sum to 1; prices are yuan per million tokens."""assert abs(cache_ratio + input_ratio + output_ratio - 1.0) < 1e-6return (cache_ratio * price_cache+ input_ratio * price_input+ output_ratio * price_output)PRICE = dict(price_cache=0.2, price_input=1.0, price_output=2.0)scenarios = {"High-hit scenario": (0.95, 0.04, 0.01),"Medium-hit scenario": (0.80, 0.15, 0.05),}for name, (c, i, o) in scenarios.items():unit = blended_cost_per_million(c, i, o, **PRICE)print(f"{name}: {unit:.3f} yuan/million tokens, about {unit * 100:.2f} yuan for 100 million tokens")
The output is:
High-hit scenario: 0.250 yuan/million tokens, about 25.00 yuan for 100 million tokensMedium-hit scenario: 0.410 yuan/million tokens, about 41.00 yuan for 100 million tokens
When the hit rate falls from 95% to 80%, the cost for the same 100 million tokens rises from 25 yuan to 41 yuan, an increase of about 64%. This shows that cache hit rate is one of the most cost-sensitive variables and must be included in the estimation model rather than relying only on a unit-price table.
Recommended Selection Workflow
- Collect real usage: Count daily cache-hit, input, and output token quantities from call logs, and split them by endpoint and time window.
- Build a weighted model: Use the formula above to calculate the effective unit price for each business scenario, and calculate monthly costs under multiple billing options.
- Run sensitivity analysis: Assume variables such as hit rate and output share fluctuate up and down, and observe whether the conclusion changes. When a conclusion is especially sensitive to a variable, prioritize verifying the data quality for that variable.
- Validate with small traffic: Switch a portion of real traffic to the target option, compare the actual bill with the model prediction, and adjust parameters accordingly.
- Review regularly: Business patterns, prompt lengths, and user behavior all change, so it is recommended to rerun the estimation every quarter.
Implementation Notes
- Verify billing definitions: Confirm whether cache-hit usage counts toward plan quotas, whether it affects concurrency limits, and what billing rules apply after overage.
- Control prompt length: Stable prefixes are more likely to hit the cache. Placing fixed instructions at the beginning of a request and dynamic content at the end usually improves the hit rate.
- Distinguish cost from value: A lower unit cost does not necessarily mean a lower total cost. If insufficient model capability leads to multiple retries, total cost may actually rise, so retry counts should also be included in the statistics.
- Keep estimation records: Archive the parameters, data sources, and conclusions of each estimation to support later comparison and traceability.
In short, API cost is the result of both usage structure and billing rules. Only by putting cache hit rate, input-output ratio, billing windows, and plan utilization into the same model can you obtain a judgment that is close to the actual bill.
