Real-World Cost Modeling for LLM API Caching and Subscription Plans
Estimating LLM costs by looking only at the per-million-token price is misleading. This article presents a reusable cost model that accounts for cache hit rates, uncached input/output ratios, peak-hour limits, periodic quotas, and retries, helping developers convert list prices into effective business costs. It includes a crossover analysis showing when a cheaper cache price stops being the cheaper option, a method for converting subscription plans into effective unit costs, and runnable Python code for your own calculations.
Real-World Cost Modeling for LLM API Caching and Subscription Plans
When deploying large language model applications, cost evaluation is often reduced to “check the price per million tokens.” But under real production traffic, both pay-as-you-go API pricing and subscription plans are influenced by multiple variables: cache hit rate, the share of uncached input tokens, output token share, peak-hour restrictions, periodic quotas, concurrency limits, failed retries, and cache TTL. Comparing cache prices alone easily leads to one-sided conclusions. This article provides a reusable cost model that helps developers convert “list prices” into “effective business costs.”
1. Why the Cache Price Alone Is Not Enough
Tokens in an LLM request typically fall into three categories:
- Cache-hit tokens: the portion that hits a prefix cache or context cache, usually billed at the lowest rate;
- Uncached input tokens: the input portion that must be recomputed, billed higher than cache hits;
- Output tokens: the generated results, usually billed at the highest rate.
Let the total token count be (T), the cache-hit ratio be (h), the uncached input ratio be (i), and the output ratio be (o), satisfying:
[
h+i+o=1
]
Let the three unit prices be (p_c), (p_i), and (p_o) (per million tokens). The total cost is:
[
C(T,h,i,o)=\frac{T}{10^6}\left(h p_c+i p_i+o p_o\right)
]
When (T) is 100 million tokens:
[
C_{100M}=100\left(h p_c+i p_i+o p_o\right)
]
The cache price only affects the first term. With a high hit rate, the cache price dominates; as the hit rate drops, uncached input and output prices quickly take over. Therefore, a plan with a lower cache price is not always cheaper — a plan with a higher cache price can win at moderate hit rates thanks to lower uncached input/output prices.
2. Cost Rankings Can Flip with the Cache Hit Rate
To illustrate this reversal, assume two anonymized plans:
- Plan A: higher cache price, but lower uncached input and output prices;
- Plan B: lower cache price, but higher uncached input and output prices.
Assume that within the non-cached portion, uncached input and output tokens keep a roughly 3:1 ratio. With non-cached ratio (1-h):
[
i=0.75(1-h),\quad o=0.25(1-h)
]
The cost difference between Plan A and Plan B is:
[
\Delta = h(p^A_c-p^B_c)+0.75(1-h)(p^A_i-p^B_i)+0.25(1-h)(p^A_o-p^B_o)
]
Setting (\Delta=0) yields the crossover point:
[
h^*=\frac{K}{K-d_c}
]
where:
[
d_c=p^A_c-p^B_c
]
[
K=0.75(p^A_i-p^B_i)+0.25(p^A_o-p^B_o)
]
When the actual hit rate is above (h^), Plan B (lower cache price) wins; below (h^), Plan A (lower combined uncached input/output cost) wins. This is the quantitative explanation behind “at high hit rates, compare cache prices; at low hit rates, compare blended prices.”
3. Anonymized Example Calculation
The following parameters are for demonstration only and do not represent any actual pricing. Assume:
| Parameter | Plan A | Plan B |
|---|---|---|
| Cache price (per 1M tokens) | 0.12 | 0.05 |
| Uncached input price (per 1M tokens) | 1.2 | 2.0 |
| Output price (per 1M tokens) | 4.8 | 8.0 |
At 98% cache hit, 1.5% uncached input, and 0.5% output:
- Plan A cost per 1M tokens:
(0.98\times0.12+0.015\times1.2+0.005\times4.8=0.1596) - Plan B cost per 1M tokens:
(0.98\times0.05+0.015\times2.0+0.005\times8.0=0.1190)
Scaled to 100 million tokens:
- Plan A: about 15.96;
- Plan B: about 11.90.
Plan B is cheaper here, matching the intuition that a low cache price wins at high hit rates.
Now drop the hit rate to 95%, splitting the non-cached portion 3:1 (3.75% uncached input, 1.25% output):
- Plan A:
(0.95\times0.12+0.0375\times1.2+0.0125\times4.8=0.2190) per 1M tokens, or about 21.90 per 100M tokens; - Plan B:
(0.95\times0.05+0.0375\times2.0+0.0125\times8.0=0.2225) per 1M tokens, or about 22.25 per 100M tokens.
Plan A now wins slightly. The crossover point is about 95.24%. The takeaway: cost comparisons must be based on your real hit-rate range, not a single quoted price.
4. Converting Subscription Plans into Effective Unit Prices
Subscription plans with periodic quotas often come in tiers — basic, standard, premium — billed monthly with weekly token quotas. The effective unit price is:
[
p_{plan}=\frac{F}{Q_w \times W \times 10^6}
]
where (F) is the monthly fee, (Q_w) is the weekly token quota, and (W) is the number of billing weeks per month, typically 4.
Assume an anonymized example with three tiers at monthly fees of 120, 540, and 1080, and weekly quotas of 320M, 1.9B, and 4.4B tokens respectively. Using 4 weeks:
- Basic: total quota 1.28B tokens, effective unit price (120/1280=0.09375) per 1M tokens;
- Standard: total quota 7.6B tokens, effective unit price (540/7600=0.0711) per 1M tokens;
- Premium: total quota 17.6B tokens, effective unit price (1080/17600=0.0614) per 1M tokens.
Compared against pay-as-you-go, the discount depends on the baseline. With a baseline of 0.1596 per 1M tokens, the three tiers cost roughly 58.7%, 44.5%, and 38.4% of baseline; with a baseline of 0.1190, they are roughly 78.8%, 59.7%, and 51.6%. So “plans are roughly 30–50% off API pricing” is only valid under specific hit rates and baselines — it cannot be applied unconditionally.
Also consider peak-hour restrictions. If peak hours are Monday to Friday, 14:00–18:00 (4 hours per day), the weekly peak window is 20 hours — about 11.9% of 168 hours. If your traffic concentrates heavily in that window, peak-hour rate limits, multipliers, and queuing policies will significantly affect effective capacity; conversely, workloads that can shift off-peak will achieve higher plan utilization.
5. Practical Calculation Code
def api_cost_per_million(h, i, o, p_cache, p_input, p_output):assert abs(h + i + o - 1.0) < 1e-9return h * p_cache + i * p_input + o * p_outputdef api_cost_per_100m(h, i, o, p_cache, p_input, p_output):return api_cost_per_million(h, i, o, p_cache, p_input, p_output) * 100def crossover(pA_cache, pA_in, pA_out,pB_cache, pB_in, pB_out,input_output_ratio=3.0):dc = pA_cache - pB_cachedi = pA_in - pB_indo = pA_out - pB_outq_in = input_output_ratio / (input_output_ratio + 1)q_out = 1 / (input_output_ratio + 1)K = q_in * di + q_out * doif abs(K - dc) < 1e-12:return Nonereturn K / (K - dc)def plan_unit_cost(monthly_fee, weekly_quota_tokens, weeks=4):total_million = weekly_quota_tokens * weeks / 1e6return monthly_fee / total_million# Example: 98% cache hit, 1.5% uncached input, 0.5% outputprint(api_cost_per_100m(0.98, 0.015, 0.005, 0.12, 1.2, 4.8))print(api_cost_per_100m(0.98, 0.015, 0.005, 0.05, 2.0, 8.0))# Example crossover pointprint(crossover(0.12, 1.2, 4.8, 0.05, 2.0, 8.0))# Example effective plan unit priceprint(plan_unit_cost(120, 3.2e8, 4))
6. Recommendations for Production
First, build request-level cost logging. At minimum, record timestamps, cache-hit tokens, uncached input tokens, output tokens, status codes, retry counts, whether the request occurred during peak hours, and whether it went through a subscription plan. Aggregate hit-rate distributions hourly and weekly, focusing on P50 and P95 rather than averages alone.
Second, run sensitivity analysis. Calculate costs across cache hit rates from 99% down to 85%, varying input/output ratios, to identify the crossover ranges between plans. If your hit rate fluctuates significantly, consider hybrid routing: high-hit requests to the low-cache-price plan, and low-hit or long-output requests to the plan with lower blended pricing.
Third, evaluate the truly usable capacity of subscription plans. Beyond monthly fees and weekly quotas, confirm peak-hour throttling rules, concurrency caps, context length limits, whether quotas roll over, whether failed retries count against the quota, and whether cache hits count against it. You can use general-purpose logging and monitoring/alerting services — for example, those offered by Baidu AI Cloud — to build a cost dashboard, but the final selection should be driven by your traffic distribution and SLA requirements.
Fourth, continuously optimize your caching strategy. Stable system prompts, prefix reuse, minimizing dynamic variables at the front of prompts, and controlling temperature and irrelevant context all improve hit rates. At scale, every percentage point of hit-rate improvement translates into a meaningful cost difference.
Conclusion
The real cost of LLM APIs and subscription plans is not a single unit price — it is the joint result of price structure × cache hit rate × input/output ratio × peak hours × quota availability. A low cache price is compelling at high hit rates but can be overtaken by uncached input and output costs as hit rates fall; plan discounts also depend on the pay-as-you-go baseline and cannot be judged in isolation from your traffic profile. Engineering teams should turn cost estimation into continuous monitoring and dynamic routing capabilities, driven by real logs, to avoid being misled by single-point pricing.
