How to Calculate LLM API Costs: Cache Hit Rate and Plan Selection
This article explains how to model LLM API costs using three variables: prompt cache hit rate, input/output token ratio, and time-based pricing. Using two sample pricing structures, it derives the break-even cache hit rate (~88.5%) that determines which pricing model is cheaper, shows how to convert subscription plans into effective discounts, and outlines a reusable selection workflow from gateway logs to budget enforcement in CI/CD pipelines.
This article does not target any specific vendor. It uses two common pricing structures as samples: one with a low cache price and a high base price, and one with a high cache price and a low base price. All figures are illustrative and do not constitute a procurement commitment. Actual decisions should be based on vendors’ public price lists and backtesting against your own gateway logs.
1. Standardizing the Calculation
Tokens read from cache are part of the input tokens and must not be added on top of the input count. This article uses the following convention: every 100 million tokens processed consists of 99.5M input tokens and 0.5M output tokens, and the input is further divided into cache hits and cache misses. The cache hit rate equals cached read tokens divided by total input tokens.
Three factors drive total cost. First, the cache hit rate. Repeated prefixes such as system prompts and conversation summaries can be billed at the cache price, which can be as low as roughly one-tenth of the regular input price. Second, the input/output ratio. In typical conversational workloads, input far exceeds output, at roughly 3:1. Third, time-based pricing. Some platforms offer additional discounts during off-peak hours.
Looking only at the list price per million tokens leads to misleading conclusions. Ignoring the cache hit rate hides order-of-magnitude differences; ignoring time windows misses savings available from shifting load. A sound cost model must account for all three.
2. Cache Hit Rate: The Most Sensitive Variable
Consider two models. Model A charges 0.115 CNY per million tokens for cached reads and 1.5 CNY per million for uncached input. Model B charges 0.05 CNY per million for cached reads and 2 CNY per million for uncached input. Both charge 3 CNY per million output tokens.
Model B’s cache price is less than half of Model A’s, but its uncached input price is higher. Which one is cheaper depends on the cache hit rate.
Using 100 million tokens as the unit, compare two scenarios.
Scenario 1: 98M cached reads, 1.5M uncached input, 0.5M output; cache hit rate ≈ 98.49%.
Model A: 98 × 0.115 + 1.5 × 1.5 + 0.5 × 3 = 11.27 + 2.25 + 1.5 = 15.02 CNY.
Model B: 98 × 0.05 + 1.5 × 2 + 0.5 × 3 = 4.9 + 3 + 1.5 = 9.40 CNY.
Model B is cheaper, saving about 5.62 CNY per 100M tokens.
Scenario 2: the hit rate drops to 95%, i.e., 94.525M cached reads, 4.975M uncached input, 0.5M output.
Model A: 94.525 × 0.115 + 4.975 × 1.5 + 1.5 ≈ 19.83 CNY.
Model B: 94.525 × 0.05 + 4.975 × 2 + 1.5 ≈ 16.18 CNY.
Model B still leads, but the gap narrows to about 3.66 CNY.
For verification, results at different hit rates (CNY per 100M tokens):
- 80%: Model A ≈ 40.50, Model B ≈ 45.28 — A is cheaper by ~4.78.
- 85%: Model A ≈ 33.61, Model B ≈ 35.58 — A is cheaper by ~1.97.
- 88.5%: both ≈ 28.79 — break-even.
- 90%: Model A ≈ 26.72, Model B ≈ 25.88 — B is cheaper by ~0.85.
- 95%: Model A ≈ 19.83, Model B ≈ 16.18 — B is cheaper by ~3.66.
- 98.49%: Model A ≈ 15.02, Model B ≈ 9.40 — B is cheaper by ~5.62.
The break-even point is derived as follows. Let total input be I and the hit rate be h. The input cost difference (Model A minus Model B) is:
I × [0.115h + 1.5(1−h)] − I × [0.05h + 2(1−h)] = I × (0.565h − 0.5)
Setting the difference to zero gives h ≈ 0.885. Below roughly 88.5%, Model A is cheaper; above it, Model B is cheaper.
This break-even point depends only on the four input unit prices, not on call volume or total input. It assumes both models charge the same output price. If output prices differ, the break-even point shifts and must be recalculated.
3. Time-Based Pricing and Plan Conversion
Subscription plans introduce another layer of variables. On one example platform, peak hours are defined as 14:00–18:00, Monday through Friday — 4 hours per day. Off-peak hours cover 20 of 24 hours, about 83% of the time window. Note that this describes the time window only, not 83% of call volume. Off-peak discounts only materialize if your traffic can actually shift to those hours.
The plan conversion method: divide the monthly plan fee by the pay-as-you-go list price of the same token quota. The example data:
- 118 CNY tier: quota stated as 317M tokens per week, equivalent to roughly 160 CNY at pay-as-you-go rates; you pay about 74% of list (≈ 26% off).
- 538 CNY tier: quota stated as 1.9B tokens per week, equivalent to roughly 955 CNY; you pay about 56% (≈ 44% off).
- 1078 CNY tier: quota stated as 4.433B tokens per week, equivalent to roughly 2229 CNY; you pay about 48% (≈ 52% off).
Several assumptions behind these numbers need verification. First, whether the conversion uses off-peak pricing. Second, whether it assumes a 98% cache hit rate. Third, whether the quota is fully consumed. Fourth, whether the quota cycle matches the billing cycle. In the source data, quotas are stated weekly while fees are monthly. If the quota resets weekly, the total monthly quota is roughly four times the weekly figure, and the discount must be recalculated over four weeks — which changes the conclusion significantly.
Higher tiers have lower payment ratios, but the improvement is not linear. Entry-level plans suit prototyping and low-frequency internal tools; mid-to-high tiers suit stable, production-grade workloads.
Four categories of risk can void the discount. First, overage fees: pay-as-you-go pricing beyond the quota may be far higher than expected. Second, rate limiting: concurrency or throughput caps can degrade peak-hour experience. Third, cache invalidation: frequently changing context lowers the hit rate, breaking the assumption behind the plan conversion. Fourth, price changes: if the vendor adjusts quotas or unit prices, historical conversions no longer hold.
4. Selection Workflow: From Unit Price to Business Cost
Model selection should not be based on unit price alone; it must also factor in model capability, availability, ecosystem integration, and data residency compliance. The following workflow is reusable:
- Profile your call structure. Extract input tokens, output tokens, cached read tokens, uncached input tokens, and the distribution of calls across time windows from gateway logs.
- Build a candidate pool. Shortlist two or three models that meet your capability bar, and compute monthly cost for each using actual logs.
- Stress test. Vary the cache hit rate by ±10 percentage points, check whether cost stays within budget, and mark where the break-even point sits.
- Compare plans versus pay-as-you-go. Pay-as-you-go is flexible; plans are more economical when usage is stable. An example threshold might be 5,000 CNY per month, but each team should set its own based on real data.
Multi-model routing is a further optimization. Route high-hit-rate, high-concurrency fixed tasks to the model with the lower cache price, and send low-hit-rate tasks requiring stronger reasoning to the more capable model. The savings ratio must be backtested against historical logs; no fixed range should be quoted before backtesting. Run a two-to-four-week proof of concept first, recording hit rates, routing ratios, and overage charges before full rollout.
5. Embedding Cost Estimation into the Delivery Pipeline
For a cost model to remain useful long-term, it should be automated. Integrate the estimation script into the CI/CD pipeline so that the incremental cost of new features is computed before every release. The script needs at least these log fields: request timestamp, model name, input tokens, output tokens, cached read tokens, and the calling module. Log access should be read-only. Budget thresholds should be set jointly by finance and engineering, and exceeding them should block the release or trigger an alert.
Conclusion
Cache hit rate, off-peak discounts, and plan conversion ratios jointly determine the final bill, with cache hit rate having the largest impact. Engineering teams supply real call data; finance teams set budget boundaries. Only by combining the two can you reach a reliable cost judgment. Until backtesting with real data is complete, any fixed cost-reduction figure is only an assumption.
