LLM API Cache Hit Rate and Effective Input Cost Calculation

    When estimating LLM API costs, list prices for input tokens often reflect only cache-miss traffic. This article explains how cache hit rate changes the effective input price, provides a calculation model and worked examples, shows how input cost relates to total request cost, and outlines engineering, monitoring, and procurement practices for evaluating cache reuse and real billing outcomes.

    When evaluating LLM API costs, the standard input price is usually only the price for cache-miss input. The input cost that actually appears on the bill depends on how much of the input is served from cache and how much is not. For systems with frequent reuse, cache hit rate can reduce the effective input price by several times. For workloads with highly random request contexts and frequently changing prefixes, the hit rate may be low, and the standard price may be closer to the actual settlement price. Technical and procurement teams therefore should not rely only on a price list for budgeting. They should build a calculation model that links cache hit rate, effective input price, and total invocation cost.

    1. Distinguish Standard, Cached, and Effective Input Prices

    The input side of an LLM API can usually be divided into two token categories:

    1. Input tokens that miss the cache, billed at the standard input price;
    2. Input tokens that hit the cache, billed at the cached input price. Whether such a price exists, how a hit is determined, whether there is a minimum billing block, and how long the cache remains valid depend on the provider’s rules.

    In addition, output tokens, tool calls, multimodal input, long-context surcharges, and retry requests may be billed separately. Different providers do not implement caching in exactly the same way. A common approach is to reuse a stable prefix: when the beginning of a request exactly matches an existing cache and the cache is still valid, the matching part may be billed at the cached price. This is usually not a case where “any similar content automatically receives a discount,” and it is not result-level caching. Prefix order, template format, the position of dynamic fields, cache validity, and isolation policy all affect the hit rate.

    A more accurate cost concept is therefore the “effective input price”: add the actual cost of cache-hit and cache-miss tokens, then convert the result into an input cost per million tokens. This price is suitable for comparison with the standard price and for monthly budgeting.

    2. A Unified Input Cost Formula

    Let:

    • N be the total number of input tokens in the measurement period;
    • h be the cache hit rate, weighted by token rather than simply averaged by request;
    • p_s be the standard input price, in CNY per million tokens;
    • p_c be the cached input price, in CNY per million tokens.

    The total input cost is:

    C_in = N / 1,000,000 × [h × p_c + (1 - h) × p_s]

    The effective input price is:

    p_eff = h × p_c + (1 - h) × p_s

    The effective cost ratio is:

    r = p_eff / p_s = h × (p_c / p_s) + (1 - h)

    The savings relative to the standard price is:

    s = 1 - r = h × (1 - p_c / p_s)

    The formula shows that the savings depend on two variables: the higher the cache hit rate, and the lower the cached price relative to the standard price, the greater the savings. If the cached price is only slightly lower than the standard price, even a 90% hit rate may produce limited total cost reduction. Conversely, if the cached price is significantly below the standard price and the business prefix is highly stable, input costs can fall meaningfully.

    Note that if cache writes, cache storage, cache validity, minimum billing units, or similar items generate additional charges, they should be added to the formula separately, for example as W_cache. If a provider only publishes a standard price without committing to a hit rate, the budget model should use a conservative hit rate and set upper and lower bounds rather than assuming that all traffic will hit the cache.

    3. Worked Example at a 90% Hit Rate

    For illustration, assume a generic billing model with:

    • Standard input price p_s = CNY 4 per million tokens;
    • Cached input price p_c = CNY 0.7 per million tokens;
    • Input volume of 1,000,000 tokens;
    • Cache hit rate h = 90%.

    The figures below are arithmetic examples only. They do not represent any actual provider’s quote or commitment.

    Cache-hit portion:

    900,000 tokens = 0.9 million tokens
    Cost = 0.9 × 0.7 = CNY 0.63

    Cache-miss portion:

    100,000 tokens = 0.1 million tokens
    Cost = 0.1 × 4 = CNY 0.4

    Total input cost = 0.63 + 0.4 = CNY 1.03.

    Compared with the standard price of CNY 4:

    1.03 ÷ 4 = 25.75%

    In other words, the actual input cost is about 25.75% of the standard price, often summarized as roughly one quarter of the standard price. This “quarter price” wording is only a convenient way to communicate the equivalent discount. Precise budgeting should still use the formula and actual billing data.

    Sensitivity at different hit rates:

    Cache hit rate Input cost per 1M tokens Relative to standard price
    0% CNY 4.000 100.00%
    50% CNY 2.350 58.75%
    80% CNY 1.360 34.00%
    90% CNY 1.030 25.75%
    95% CNY 0.865 21.63%
    99% CNY 0.733 18.33%

    The table shows that moving the cache hit rate from 0% to 80% already produces a very clear cost reduction; moving from 80% to 90% still has value; but at higher levels, further improvement requires more careful engineering investment. Teams should not blindly pursue a 100% hit rate. They should balance invocation volume, the cached price gap, engineering complexity, and business latency targets.

    4. Input Cost Is Not the Same as Total Invocation Cost

    Many cost misjudgments come from looking only at the input price. Actual total cost also needs to account for output tokens, tool calls, multimodal content, retries, failed requests, and other factors. This can be written as:

    C_total = C_input_effective + C_output + C_other

    Here, C_input_effective is calculated using the formula above; C_output = output token volume × output price; and C_other includes general engineering costs such as external retrieval, function calls, log storage, and content moderation.

    If the business output is very long, for example long-form writing, report generation, or complex code generation, total cost may still be dominated by the output side even if input cost receives a large discount. Conversely, if the workload consists mainly of long-context question answering, fixed knowledge-base Q&A, or batch classification, input tokens account for a high share, and the benefits of a cache strategy are more pronounced.

    Unit economics should also be calculated per request:

    Cost per request =
    input tokens × effective input price + output tokens × output price + other per-request costs

    Then divide by business value, such as cost per conversion, per report, or per ticket, to judge whether cache optimization is worthwhile. Comparing only “CNY per million tokens” can easily ignore differences in request structure.

    5. How to Improve and Stabilize Cache Hit Rate

    The core of cache hits is reuse of a stable prefix. Engineering teams can work on the following areas.

    1. Stabilize system prompts and role definitions. Put system instructions, output formats, and tool descriptions that do not change from request to request at the very beginning, and keep their versions stable. Do not place timestamps, random numbers, or user nicknames at the start of the prefix.

    2. Fix template order and format. Content with the same meaning but different whitespace, line breaks, JSON key order, or punctuation may cause the prefix to no longer match exactly. Normalize templates, use template IDs and hashes to identify versions, and review changes.

    3. Place dynamic content later. Put user questions, real-time retrieval snippets, and dynamic session fields after the stable prefix. In this way, even if the end of each request differs, the common beginning may still be reused.

    4. Use an append-style structure for multi-turn sessions. In multi-turn conversations, preserve the existing context order as much as possible and append new messages rather than rewriting or reordering the entire history every turn. When history needs to be compressed, manage stable background and dynamic summaries in separate layers.

    5. Group batch jobs by shared context. For large offline jobs based on “the same set of instructions + different samples,” schedule them into similar time windows to avoid repeated cold starts of the context. Whether caches can be shared across tenants or sessions depends on the provider’s isolation policy and compliance requirements. Cross-tenant sharing should not be assumed by default.

    6. In retrieval-augmented scenarios, put static content first and dynamic content last. Place long-lived knowledge, rules, and tool descriptions in the prefix, and place small request-specific retrieved snippets at the end. If knowledge snippets themselves are highly repetitive, reusable prefixes can also be designed by business group, but data isolation and sensitive information protection must be considered.

    7. Control the number of prefixes and the frequency of changes. Excessive fragmentation of prefix templates reduces the probability of reuse and increases observability difficulty. It is advisable to govern high-frequency templates centrally and regularly clean up low-hit, low-benefit templates.

    8. Complete batch calls within the validity period. Caches usually have a validity period and an eviction mechanism. Batch jobs should run continuously within the time window where possible. For important jobs, a small number of warm-up requests can be sent first, but warm-up should not be assumed to succeed, and a cache-miss budget should still be retained.

    6. Monitoring Metrics and Budget Alerts

    Cache hit rate is not a fixed parameter. It is an operating metric that fluctuates with traffic structure, template versions, time windows, and server-side policies. At a minimum, monitor:

    • Cache hit rate weighted by token, not only by request count;
    • Cache-hit tokens, cache-miss tokens, and total input tokens;
    • Effective input price and the discount ratio relative to the standard price;
    • Input-side savings, total invocation cost, and output cost share;
    • Hit rate distribution by feature, tenant, and template version;
    • Number of template changes, number of cold starts, and retry rate;
    • p50/p95 latency and error rate, so that stability is not sacrificed for hit rate.

    The following pseudocode can be used for unified calculation:

    1. function calcInputCost(totalPromptTokens, hitTokens, standardPrice, cachedPrice):
    2. missTokens = totalPromptTokens - hitTokens
    3. cost = hitTokens / 1000000 * cachedPrice
    4. + missTokens / 1000000 * standardPrice
    5. hitRate = totalPromptTokens > 0
    6. ? hitTokens / totalPromptTokens
    7. : 0
    8. effectivePrice = totalPromptTokens > 0
    9. ? cost * 1000000 / totalPromptTokens
    10. : 0
    11. return {cost, hitRate, effectivePrice}

    For budgeting, use three scenarios: a pessimistic scenario based on a lower hit rate, a baseline scenario based on the recent actual hit rate, and an optimistic scenario based on the target hit rate after optimization. If the actual hit rate remains below the threshold for an extended period, trigger a cost alert and check whether template changes, traffic structure changes, cache validity, or request routing have changed.

    7. Procurement and Technical Review Checklist

    When reviewing cache billing capabilities, do not ask only whether caching exists. Confirm the following general questions:

    1. How is a cache hit determined? Does it require an exact prefix match? Are request parameters distinguished?
    2. What are the cache unit, minimum billing block, validity period, and eviction policy?
    3. Are cache writes, storage, and reads billed separately?
    4. How is the cache scope isolated? Is it isolated by account, region, tenant, or session?
    5. Can metering data be exported for hit tokens and miss tokens?
    6. Does caching affect data retention and compliance boundaries?
    7. How are price adjustments or policy changes communicated?
    8. How can reproducible benchmark tests be run under real business traffic?

    Benchmark tests should use representative samples and cover typical prefixes, long contexts, concurrency, latency, and retry scenarios. Tests need to separate cold start from steady state, record input tokens, hit tokens, output tokens, and cost for each request, and then calculate the weighted hit rate. A single demo or a single invocation result cannot be used to infer long-term effective cost.

    8. Conclusion

    Cache hit rate is a key variable in input cost calculation, but it is not a single switch. It is an operating metric determined jointly by business prefix stability, engineering template governance, scheduling strategy, and provider billing rules. The standard price is useful as an upper-bound reference, while the real budget should use an effective cost model: “hit portion × cached price + miss portion × standard price.” With reasonable design, a 90% hit rate combined with a significant cached price gap can reduce input cost to roughly a quarter of the standard price. However, this conclusion must be based on observable, reproducible, and auditable actual data. Engineering, operations, and procurement should jointly establish monitoring and scenario-based budgets, balancing cost, latency, quality, and compliance.