How LLM API Costs Are Calculated: Cache Hit Rate and Pricing Plan Selection
When enterprises scale up their LLM applications, API costs often determine whether a project is sustainable. Many teams compare only the per-million-token price of a model while overlooking how cache hit rates, billing time periods, and subscription plan rules affect the actual bill. This article walks through a set of illustrative calculations showing how these factors change final costs, and provides a reusable formula for your own estimates. All prices used here are placeholder figures for demonstration only and do not represent any provider's real pricing—always check the official price list of your platform for actual rates.
1. How Context Caching Is Billed
Many providers offer context-caching billing. When multiple requests share an identical prefix—such as a system prompt, few-shot examples, or fixed business context—the provider can cache the KV Cache for that prefix. Subsequent requests reuse the cache and skip redundant computation.
Under this billing model, tokens typically fall into three categories: cache-hit tokens, regular input tokens, and output tokens. Cache-hit tokens are usually the cheapest, regular input tokens cost more, and output tokens are the most expensive.
This tiered pricing makes the hit rate a critical cost variable. For the same 100 million tokens, a shift of just a few percentage points in hit rate can produce a noticeably different bill.
2. An Illustrative Calculation
To make the math reproducible, this article sets three assumptions. First, the total volume is 100 million tokens (100 million-token units). Second, the hit rate is defined as the proportion of cache-hit tokens out of total tokens, with output tokens counted in the non-hit portion. In real billing, most providers calculate the hit rate based on input tokens only, so adjust accordingly when doing your own math. Third, prices are in currency units per million tokens.
Illustrative prices:
| Billing Item | Model A | Model B |
|---|---|---|
| Cache hit | 0.115 | 0.05 |
| Regular input | 1.0 | 1.5 |
| Output | 2.0 | 6.0 |
The cost per 100 million tokens is:
Cost per 100M tokens = 100 × (hit ratio × cache price + input ratio × input price + output ratio × output price)
Scenario 1: 98% hit rate, 1.5% new input, 0.5% output
Model A: 100 × (0.98 × 0.115 + 0.015 × 1.0 + 0.005 × 2.0) = 13.77
Model B: 100 × (0.98 × 0.05 + 0.015 × 1.5 + 0.005 × 6.0) = 10.15
Model B is about 26% cheaper than Model A.
Scenario 2: 95% hit rate, 3% new input, 2% output
Model A: 100 × (0.95 × 0.115 + 0.03 × 1.0 + 0.02 × 2.0) ≈ 17.93
Model B: 100 × (0.95 × 0.05 + 0.03 × 1.5 + 0.02 × 6.0) = 21.25
Now Model B is about 18.5% more expensive than Model A.
Comparing the two scenarios: for every 3 percentage points the hit rate drops, Model A’s cost rises by about 4.16 per 100M tokens, while Model B’s rises by about 11.10. Model B’s cost grows faster because its non-hit prices are higher—once the non-hit share increases, the expensive portion is amplified.
This reversal depends on the illustrative prices. If, in a real price list, Model B’s regular input and output prices are not higher than Model A’s, the reversal would not occur. Therefore, always compare models using the full price list—judging by cache price alone leads to one-sided conclusions.
3. Finding the Break-Even Hit Rate
The hit rate at which two models cost the same is called the break-even point. Let the average price of non-hit tokens be p_A and p_B, the cache prices be c_A and c_B, and the hit rate be h. Then:
h × (c_A - c_B) = (1 - h) × (p_B - p_A)
Solving for the break-even point:
h* = Δp / (Δc + Δp)
where Δc = c_A - c_B and Δp = p_B - p_A.
Using the non-hit composition from Scenario 1, where input and output are in a 3:1 ratio: p_A = 0.75 × 1.0 + 0.25 × 2.0 = 1.25, p_B = 0.75 × 1.5 + 0.25 × 6.0 = 2.625, Δc = 0.065, and Δp = 1.375. Substituting gives h* ≈ 95.5%. When the hit rate is above this value, Model B is cheaper; below it, Model A is cheaper.
The break-even point shifts when the non-hit composition changes, so teams should recalculate it using their own input-to-output ratio.
4. How to Evaluate Subscription Plans
Besides pay-as-you-go billing, some platforms offer monthly subscription plans with quotas measured in tokens or request counts. To compare a plan with pay-as-you-go pricing, compute the effective unit price:
Effective unit price = monthly fee ÷ (the pay-as-you-go equivalent value of the plan’s quota)
For example, if a plan costs 300 per month and its quota is equivalent to 600 at pay-as-you-go rates, the effective unit price is 50% of list price. These are illustrative numbers, not any real platform’s plan.
Several rules matter when doing this conversion:
- Quota basis: Whether the quota is measured in tokens or request counts—the conversion results differ significantly.
- Time-period rules: Some platforms distinguish peak and off-peak hours; peak hours may be rate-limited or deduct quota at a higher multiplier. Suppose a platform defines 14:00–18:00 on weekdays as peak hours and all other times (including weekends) as off-peak. Actual rules should be confirmed in the platform’s documentation, and holiday schedules need separate verification.
- Model coverage: Whether the plan covers all models or only specific ones.
- Reset cycle: Whether the quota resets weekly or monthly, and whether unused quota rolls over.
- Overage behavior: Whether service stops, models are downgraded, or billing switches to pay-as-you-go when the quota runs out.
Also note that plan quotas are consumed at different rates depending on the hit rate. When the hit rate drops, the same requests consume more quota, and the conversion result must be adjusted accordingly.
5. Three Practices to Reduce Costs
1. Put static prefixes first, dynamic content last
Cache matching requires the prefix to match character for character. System role definitions, fixed codebase context, and lengthy business guidelines should go at the very front of the prompt, with user input and dynamic parameters at the end. If timestamps, request IDs, or random numbers appear in the prefix, the cache will be invalidated on every request. Verify prefix stability before going live.
2. Queue asynchronously and run off-peak
Tasks that do not need immediate results—such as batch document summarization, overnight code reviews, and data cleaning—can be placed in a message queue and consumed by worker processes during off-peak hours. Object storage can be used to centrally manage prompt templates and knowledge base files, while a logging service tracks input tokens, cache hits, and output tokens to compute the hit rate.
This practice has clear boundaries. Requests where users are actively waiting for a result should not be queued. Set a maximum wait time on the queue and fall back to the real-time API when it is exceeded.
3. Route dynamically based on task attributes
Core, urgent requests go through the real-time API. Deferrable tasks use plan quota first during off-peak hours, and enter the queue when quota is exhausted or during peak hours. Here is illustrative code:
import datetimePEAK_WEEKDAY_START = 14 # Illustrative value; should live in a config filePEAK_WEEKDAY_END = 18def is_off_peak_hours(now=None):"""Weekdays 14:00-18:00 are peak hours; all other times are off-peak."""now = now or datetime.datetime.now()in_peak = (now.weekday() < 5and PEAK_WEEKDAY_START <= now.hour < PEAK_WEEKDAY_END)return not in_peakdef route_api_request(task):# Core and urgent requests always use the real-time APIif task.is_urgent and task.is_core:return call_realtime_api(task.prompt)# Deferrable tasks: use the plan quota during off-peak hours when availableif task.is_deferrable and is_off_peak_hours() and subscription_quota_available():return call_subscription_plan(task.prompt)# Remaining deferrable tasks are queuedif task.is_deferrable:enqueue_to_mq(task)return "Task queued"
