Calculating True LLM API Costs: How Cache Hit Rate Changes Your Bill

    This article explains why list prices do not equal real LLM API costs in production. It covers context caching, the cost formula, and a hypothetical comparison of two pricing plans at 98% and 95% cache hit rates to show how conclusions can reverse. It also discusses off-peak pricing constraints, how to convert subscription plans into API-equivalent value, and practical steps for cost optimization.

    Calculating True LLM API Costs: How Cache Hit Rate Changes Your Bill

    Introduction: List Price Is Not Cost

    When selecting an LLM API, many teams compare the “price per million tokens” on official pricing pages and draw conclusions from that number. In production, however, this comparison often leads to the wrong decision. Modern LLM inference services commonly use context caching. Input tokens that hit the cache are usually billed at a steep discount, while uncached tokens are billed at the standard price. In other words, your real bill depends not on the list price, but on the cache hit rate.

    This article uses a typical cost calculation case to explain the cache billing model, the cost reversal that occurs at different hit rates, and how to convert subscription plans into pay-as-you-go API equivalents. The goal is to provide developers with a reusable framework for cost evaluation.

    1. Fundamentals of the Cache Billing Model

    1.1 What Is Context Caching?

    Context caching means that when multiple requests share the same input prefix—such as system prompts, tool definitions, or long document content—the inference service reuses previously computed KV cache and avoids recomputation. Providers can therefore charge much less for cached tokens, often at 10% of the standard price or even lower.

    1.2 Cost Formula

    The total cost of one usage cycle can be abstracted as:

    1. Total cost = cached hit tokens × cache price
    2. + uncached input tokens × standard input price
    3. + output tokens × output price

    Consider a typical coding-assistant scenario: for every 100 million tokens of traffic, cache hits account for 98%, new input accounts for 1.5%, and output accounts for 0.5%. This ratio is common in multi-turn conversations and repository-level Q&A, where system prompts and context history are reused repeatedly and genuinely new content is small.

    1.3 Why Cache Strategies Differ So Much Across Providers

    Providers take very different approaches to cache pricing. Some push cache prices extremely low as a customer-acquisition strategy. Others set a relatively higher cache price but offer a lower standard input price. Some offer “off-peak discounts” with substantial reductions during specific low-traffic hours each day. This means that discussing unit prices without considering hit rate is meaningless.

    2. Worked Example: How Hit Rate Reverses the Conclusion

    The following hypothetical comparison of two plans demonstrates the calculation process. Assume:

    • Plan A: cache price of about RMB 0.115 per million tokens, with standard input and output prices at common industry levels;
    • Plan B: off-peak cache price of about RMB 0.05 per million tokens, but with relatively higher standard-price components, and the discount applies only during off-peak hours.

    2.1 High Hit Rate Scenario (98%)

    At 98% cache hits, 1.5% input, and 0.5% output, every 100 million tokens consumed costs:

    • Plan A: about RMB 12.57
    • Plan B (off-peak price): about RMB 9.40

    Here Plan B leads because of its extremely low cache price, with a gap of about 25%.

    2.2 Medium Hit Rate Scenario (95%)

    If the business scenario has lower context reuse—for example, many conversation branches or frequently changing prompts—and the hit rate drops to 95%:

    • Plan A: about RMB 14.92
    • Plan B (off-peak price): about RMB 18.25

    The conclusion completely reverses: Plan A becomes about 18% cheaper. The reason is that the uncached share rises from 2% to 5%, a 2.5x increase, and the difference in standard input/output prices now becomes the dominant factor.

    2.3 Key Takeaways

    This comparison reveals a core pattern:

    1. The higher the hit rate, the greater the weight of the cache price;
    2. The lower the hit rate, the greater the weight of standard input and output prices;
    3. There is a “critical hit rate” beyond which the relative advantages of the two plans swap.

    In engineering practice, it is better to first use one week of production logs to measure your real hit-rate distribution, then apply each provider’s prices in a weighted calculation, rather than relying on rough estimates.

    3. Off-Peak Pricing and Hidden Peak-Hour Constraints

    Many providers attach time conditions to low prices. There are two typical off-peak strategies:

    • Calendar-day based: for example, late night to early morning is the low-traffic period each day;
    • Work-schedule based: for example, only four hours on weekday afternoons are peak, and the rest of the time receives discounted pricing.

    The second form is friendlier to developers. If peak hours are limited to only four hours per workday, more than 83% of the day can enjoy non-peak pricing. Batch jobs, offline evaluations, and nightly builds can almost completely avoid peak billing.

    When calculating costs, additional factors must be considered:

    1. Time distribution of business traffic: peaks for interactive products often coincide with provider-defined peaks, so the actual discount may shrink;
    2. Task schedulability: whether offline tasks can be delayed through a task queue to run during off-peak hours;
    3. Cross-time-zone factors: for services serving global users, peak definitions may be misaligned with actual load.

    4. Subscription Plans vs. Pay-as-You-Go APIs: How to Convert

    In addition to pay-as-you-go billing, some providers offer subscription plans for coding-assistant scenarios. Consider a typical three-tier set of plans:

    Tier Monthly fee Weekly off-peak quota (at 98% cache hit rate) API-equivalent value at standard prices
    Entry approx. RMB 118/month approx. 317 million tokens approx. RMB 160
    Advanced approx. RMB 538/month approx. 1.9 billion tokens approx. RMB 955
    Flagship approx. RMB 1,078/month approx. 4.433 billion tokens approx. RMB 2,229

    Several patterns emerge from the table:

    4.1 Subscription Unit Price Is About 50%–70% of API

    The higher the tier, the deeper the discount. The flagship tier’s effective discount is close to 50%, making it suitable for heavy users.

    4.2 Quota Definitions Depend on Cache-Rate Assumptions

    Note that official token quotas are usually based on an ideal cache hit rate, such as 98%. If your actual hit rate is only 90%, the same quota will support noticeably fewer real conversation turns. When evaluating a plan, convert the quota into “equivalent new tokens” and compare it with your own usage profile.

    4.3 Who Subscription Plans Are For

    • Individual developers or small teams with stable, predictable usage: subscriptions cap costs and prevent bill overruns;
    • Teams with volatile usage or multi-model needs: pay-as-you-go APIs combined with cache optimization are more flexible;
    • Enterprise scenarios: these usually require overall cost governance that combines private deployment, object storage, logging services, monitoring and alerting, and other general cloud capabilities, rather than relying solely on subscription plans.

    5. Practical Cost Optimization Recommendations

    1. Improve cache hit rate: fix the system prompt prefix, avoid inserting timestamps or other volatile content at the beginning of prompts, and keep long contexts in a stable order;
    2. Establish usage observability: use logging services and monitoring/alerting to continuously track hit rate, input/output ratio, and time-of-day distribution, forming your own cost baseline;
    3. Schedule workloads: delay batch processing, evaluation, and data-cleaning tasks through message queues so they run during off-peak hours;
    4. Recalculate regularly: providers adjust pricing frequently, so it is advisable to rerun the critical-hit-rate calculation every quarter to verify whether the current selection remains optimal.

    Conclusion

    The real cost of LLM APIs is a dynamic equation: cache hit rate, time-of-day distribution, and billing model together determine the final bill. Comparing list prices alone easily leads to misjudgment. Only quantitative calculation based on your own traffic profile can help you make truly economical choices between “low cache price” and “low standard price,” and between “subscription” and “pay-as-you-go.” The formulas and framework in this article are intended to help your team calculate this account more clearly.