Cache Hit Rate and the Real Cost of LLM APIs

    This article explains why the listed price of an LLM API can differ sharply from the actual cost you pay. It introduces the concept of effective unit price, shows how cache hit rate and input-to-output token ratio affect blended cost, and explains why prefix caching and prefill/decode separation matter. It also provides practical guidance for reducing cost through stable prefixes, better prompt structure, and workload selection.

    Cache Hit Rate and the Real Cost of LLM APIs

    1. A counterintuitive phenomenon

    Open the pricing page of almost any large language model API, and it is easy to conclude that prices have risen across the board. Output token prices may be several times higher than before, and input token prices may also be higher.

    However, treating the listed price as the real cost can lead to the wrong conclusion.

    The number on a pricing page is only the nominal unit price. What actually determines the cost you pay is the effective unit price, which is shaped by cache hit rate, the input-to-output token ratio, and your calling pattern. This article uses a unified calculation model to quantify the gap between nominal unit price and real cost.

    2. Three-tier pricing structure

    Mainstream large language model APIs often use three pricing tiers:

    • Uncached input: the full prompt is sent for the first time and must be computed completely.
    • Cached input: the request hits a prefix cache and can be served at a lower cost.
    • Output: the tokens generated by the model, usually the most expensive tier.

    One notable structural change is that newer model generations often widen the price gaps between uncached and cached input, and between output and cached input.

    For illustration, suppose the pricing per million tokens is as follows. These are demo numbers only and do not represent any specific product:

    Tier Early example New-generation example
    Uncached input 6.5 20
    Cached input 1.1 2
    Output 27 100

    Unit: CNY per million tokens. The prices are illustrative only and are not actual prices of any specific product.

    From these numbers, three conclusions stand out:

    • The multiple between uncached and cached input rises from about 6x to about 10x.
    • The multiple between output and cached input rises from about 25x to about 50x.
    • The absolute increase is smallest for the cached-input tier and largest for the output tier.

    This pattern sends a clear signal: vendors are using price structure to encourage usage patterns with higher cache hit rates.

    3. The leverage effect of cache hit rate

    Two formulas are useful here.

    Let:

    • h = cache hit rate
    • p_cache = cached-input unit price
    • p_miss = uncached-input unit price
    • p_out = output unit price

    The effective input unit price is:

    1. effective input unit price = p_cache * h + p_miss * (1 - h)

    If the input-to-output token ratio is R:1, the blended cost per million tokens becomes:

    1. blended unit price = (R * effective input unit price + p_out) / (R + 1)

    Long-context coding-agent workloads can easily reach an input-to-output ratio of 20:1.

    Here is a reusable Python function:

    1. def effective_cost(p_miss, p_cache, p_out, hit_rate, in_out_ratio):
    2. eff_input = p_cache * hit_rate + p_miss * (1 - hit_rate)
    3. blended = (in_out_ratio * eff_input + p_out) / (in_out_ratio + 1)
    4. return eff_input, blended
    5. # Illustrative pricing
    6. p_miss, p_cache, p_out = 20, 2, 100
    7. # Scenario A: enterprise coding agent, highly reused prefixes, 90% hit rate
    8. eff_in_a, blended_a = effective_cost(
    9. p_miss, p_cache, p_out,
    10. hit_rate=0.9,
    11. in_out_ratio=20
    12. )
    13. print(f"Scenario A: effective input≈{eff_in_a:.1f}, blended≈{blended_a:.1f}") # 3.8, 8.4
    14. # Scenario B: intermittent developer, different prompt each time, 20% hit rate
    15. eff_in_b, blended_b = effective_cost(
    16. p_miss, p_cache, p_out,
    17. hit_rate=0.2,
    18. in_out_ratio=5
    19. )
    20. print(f"Scenario B: effective input≈{eff_in_b:.1f}, blended≈{blended_b:.1f}") # 16.4, 30.3
    21. print(f"Blended cost gap: {blended_b / blended_a:.1f}x") # about 3.6x

    The key point is this: the higher the cache hit rate, the more effectively the high uncached-input price is diluted, and the closer the effective input unit price becomes to the lower cached-input tier.

    4. Scenario comparison: same model, two cost outcomes

    Scenario A: Enterprise-level coding agent

    Assumptions:

    • Input-to-output ratio: R = 20
    • Cache hit rate: h = 0.9

    Calculation:

    • Effective input unit price ≈ 2 × 0.9 + 20 × 0.1 = 3.8 CNY
    • Blended unit price ≈ (20 × 3.8 + 100) / 21 ≈ 8.4 CNY per million tokens

    In this scenario, the system prompt, tool definitions, and repository context are highly reusable. About 90% of input tokens hit the cache, so the workload stays protected in the lower-price tier by the large price gap.

    Scenario B: Intermittent developer

    Assumptions:

    • Input-to-output ratio: R = 5
    • Cache hit rate: h = 0.2

    Calculation:

    • Effective input unit price ≈ 2 × 0.2 + 20 × 0.8 = 16.4 CNY
    • Blended unit price ≈ (5 × 16.4 + 100) / 6 ≈ 30.3 CNY per million tokens

    In this scenario, each prompt is assembled differently. About 80% of input tokens miss the cache, so the workload directly absorbs the high uncached-input price.

    For the same model, the blended unit cost in Scenario A is roughly one-third of that in Scenario B. What looks like a general price increase may actually be a discounted bill for enterprise users with stable prompts, while developers who frequently change prompts may see prices close to the nominal list price or even higher.

    5. Architecture and cache hit rate

    Cache hit rate is not mysterious. It is the result of both inference architecture and calling habits. Two widely used techniques are worth understanding.

    Prefix caching

    Prefix caching stores the KV cache corresponding to previously computed prompt prefixes. If later requests share the same prefix, the system can reuse it directly and avoid repeated computation.

    This requires the caller to keep the prefix byte-for-byte identical. Even changing one space in the system prompt can invalidate the cache.

    Prefill / decode separation

    In a prefill/decode disaggregated inference architecture, prefill and decode are scheduled on separate resource pools:

    • Prefill processes the input and generates the KV cache.
    • Decode generates output tokens one by one.

    Separating these stages allows cache reuse and throughput optimization to happen independently. In long-context, high-reuse scenarios, this can significantly improve the cache hit rate.

    This also explains why coding-agent workloads, such as a long system prompt, fixed tool definitions, and a progressively appended conversation, are naturally suited to high cache hit rates. In contrast, one-off question-answering calls with little reuse are unlikely to benefit from caching.

    6. The essence of cost divergence

    The broader conclusion is simple: what determines your cost outcome is not whether the listed price has increased, but which side of the cache-hit divide you are on.

    • Long context, stable prefixes, and frequent consecutive calls → you are on the high-cache-hit side and benefit from a structural discount.
    • Short requests, changing prefixes, and scattered calls → you are on the low-cache-hit side and pay close to the nominal price.

    In other words, the pricing structure rewards usage patterns that are cacheable.

    7. Practical cost-reduction advice

    1. Keep prefixes stable
      Put fixed content such as the system prompt, tool or function definitions, and few-shot examples at the beginning of the prompt. Keep them completely unchanged to avoid unnecessarily breaking the cache.

    2. Place dynamic content later
      Put time-sensitive content, such as the current user input or timestamps, after the stable prefix. This maximizes the reusable portion of the prompt.

    3. Improve the effective input-to-output ratio
      Batch workloads and long-context tasks can dilute the expensive output cost. For pure short-question and short-answer workloads, reconsider whether the usage pattern is worth keeping.

    4. Quantify your cache hit rate
      Use the calculation method in this article. Plug in the actual pricing and measured cache hit rate, then calculate your own effective blended unit price before deciding on model selection or optimization direction.

    5. Use platform capabilities where available
      Prefix caching, request routing, and usage monitoring are often provided by mature cloud platforms or inference services as general capabilities. Check the relevant API documentation or observability tools offered by your provider, so you can track cache-hit rate, request volume, and other relevant metrics continuously.

    In short: understanding the billing structure is more important than watching the listed price. What you need to optimize is not the nominal unit price, but your own cache hit rate.