Cache Hit Rate and Real Input Cost for LLM APIs: A Practical Calculation Guide

    This article explains why standard LLM API input pricing does not reflect actual billing when cache hits are high, and provides a token-weighted formula for estimating the effective input cost. It includes a worked example at 90% cache hit rate, a sensitivity table, engineering factors that affect cache hit rates, and reusable pseudocode for cost measurement and optimization.

    Why Standard Pricing Does Not Equal Your Real Bill

    When selecting or budgeting for large language model (LLM) APIs, a common mistake is treating the standard input price multiplied by total input tokens as the true cost. That approach works reasonably for workloads with near‑zero cache reuse, but for production scenarios with long system prompts, frequent multi‑turn conversations, fixed retrieval‑augmented generation (RAG) pipelines, or consistent batch templates, a large portion of input tokens can be reused. In those cases, the cache hit rate becomes a key driver of the final bill.

    Cache hit rate should be understood as the fraction of input tokens that match cached content and are billed at the cache price. It is not a request‑level hit ratio, but a token‑weighted ratio. This distinction matters: a long‑context request with 80% of its tokens hitting the cache affects costs very differently from many short requests with no hits. Therefore, cost models must be built on token granularity, not on request counts.

    Let P be the standard input price per token and P_c the cached input price per token. With a token‑weighted hit rate h, the effective input price per token P_eff is:

    P_eff = h × P_c + (1 − h) × P

    If we define the cache discount ratio d = P_c / P, the relative effective price becomes:

    P_eff / P = h × d + (1 − h) = 1 − h × (1 − d)

    This simple formula shows that higher hit rates and deeper cache discounts directly lower the actual input cost. Relying solely on the listed standard price will systematically over‑ or under‑estimate costs, making accurate model selection and capacity planning impossible.

    Example: 90% Cache Hit Rate

    For illustration, assume a cache price ratio d = 0.175 (i.e., cached input tokens are billed at 17.5% of the standard price). This ratio is a sample parameter, not a commitment from any specific provider. With a hit rate h = 90%:

    P_eff / P = 0.9 × 0.175 + 0.1 × 1.0
    = 0.1575 + 0.1
    = 0.2575

    The effective input price is about 25.75% of the standard price, roughly a 2.6× discount (or “2.5 折” in Chinese retail jargon). For 1M input tokens, the portion that hits the cache is billed at the cache price, while the remaining 10% is billed at the standard price, yielding a total cost equal to 25.75% of the standard price.

    This explains why “90% cache hit rate, effective input price at about 2.5× discount” is not an exaggeration—it is the natural result of token‑weighted billing. Hits enjoy the discounted rate, misses still pay the standard rate, and the weighted blend becomes the real unit cost.

    Sensitivity: Joint Impact of Hit Rate and Cache Discount

    With the cache price ratio fixed at d = 0.175, the effective input discount for various hit rates is:

    Cache Hit Rate (h) Effective Input Price / Standard Approximate Discount
    0% 100% 10.0× (no discount)
    50% 58.75% 5.9×
    80% 34% 3.4×
    90% 25.75% 2.6×
    95% 21.625% 2.2×
    100% 17.5% 1.75×

    The table shows a dramatic drop in input cost when the hit rate goes from 0% to 90%. From 90% to 95% the marginal benefit still exists but is smaller. Engineering effort should first target inefficient patterns that suppress hit rates below 80%—unstable prefixes, randomized templates, unstable retrieval ordering, and inconsistent request routing—then tackle the long tail.

    Important caveat: caching does not change output token billing. If your workload has a high proportion of output tokens, the absolute bill reduction from lower input costs will be diluted. Therefore, cost governance should simultaneously track input hit rate, input token share, output token share, and average context length per request, rather than focusing on a single metric.

    Common Engineering Factors That Affect Cache Hit Rate

    1. System prompt stability – Putting timestamps, random IDs, user nicknames, or dynamic experiment parameters at the beginning of the prompt breaks prefix reuse. Stable instructions should be placed early, and dynamic variables should be positioned later.

    2. Multi‑turn context management – Keeping full conversation history improves reuse, but overly long contexts increase the absolute amount of uncached tokens. Consider summarization, trimming, or segment‑level caching to balance hit rate against total token volume.

    3. RAG pipeline consistency – Unstable retrieval ranking, random concatenation formats, or changing citation numbers reduce cache hits. Fix the template, stabilize ordering, and normalize null values and delimiters.

    4. Batch processing and template uniformity – If every request in a batch inserts a random field, the hit rate will be very low. Common instructions, formatting constraints, and example sets should be placed in a fixed shared prefix.

    5. Request routing and session affinity – If the same business traffic is randomly distributed across multiple instances, regions, or model versions, cache reuse may degrade. Design routing strategies that balance cost and availability.

    6. Measurement methodology – Always compute hit rate on a token‑weighted basis, and break it down by model, tenant, scenario, and prompt version. A global average can hide expensive long‑tail requests.

    Reusable Cost Calculation Code

    The following pseudocode is provider‑agnostic and computes the effective input discount and a sensitivity table:

    1. def effective_input_discount(hit_rate, cache_price_ratio):
    2. """
    3. hit_rate: fraction of input tokens that hit the cache (0~1)
    4. cache_price_ratio: cached price / standard input price
    5. Returns: effective input price / standard input price
    6. """
    7. return hit_rate * cache_price_ratio + (1 - hit_rate)
    8. cache_ratio = 0.175 # sample parameter for illustration
    9. for h in [0, 0.5, 0.8, 0.9, 0.95, 1.0]:
    10. ratio = effective_input_discount(h, cache_ratio)
    11. print(f"Hit rate={h:.0%}, effective input price={ratio:.2%}, approx {ratio*10:.2f}x discount")

    For production, add a total input cost function:

    1. def input_cost(total_input_tokens, hit_rate, standard_price, cache_ratio):
    2. cached = total_input_tokens * hit_rate
    3. uncached = total_input_tokens - cached
    4. return cached * standard_price * cache_ratio + uncached * standard_price

    Integrate this function into bill reconciliation, budget alerts, and cost regression tests. It quickly validates whether changes to prompt templates, retrieval chains, or routing policies actually reduce the true input cost.

    Monitoring and Cost Governance Recommendations

    • Establish token‑level metrics: total input tokens, cached tokens, uncached tokens, hit rate, effective input price ratio, and per‑scenario cost breakdown.
    • Set up budget and alerting: configure thresholds for abnormal hit‑rate drops, abnormal context length growth, or cost spikes for a specific tenant.
    • Adopt version‑based evaluation: after every change to system prompts, retrieval templates, or context‑trimming strategies, compare hit rates and costs on a fixed evaluation set.
    • Distinguish exploratory traffic from stable production traffic: low hit rates in exploratory traffic are normal and should not be evaluated with a global hit‑rate target.

    When building with cloud providers, generic observability services (log management, monitoring, alerting) can be used to collect invocation counts, input tokens, and cache hit ratios, embedding cost visibility into daily operations. Keep in mind that cost optimization must not degrade quality; also monitor answer accuracy, latency, and error/retry rates to avoid trimming context so aggressively that it breaks business semantics.

    Conclusion

    When evaluating LLM API costs, the standard list price is only a starting point. The cache hit rate is the key variable that determines the actual input price. The token‑weighted effective input price equals:

    effective price = hit rate × cache price ratio + (1 − hit rate) × standard price

    Under the sample parameters used here, a 90% hit rate brings the effective input cost down to about 25.75% of the standard price (roughly a 2.6× discount). Developers should incorporate cache hit rate into architecture design, prompt engineering, retrieval chains, routing strategies, and observability practices, relying on data‑driven cost governance instead of being misled by standard pricing.