LLM Cache Hit Rate and Its Impact on Inference Cost

    This article analyzes how LLM prompt cache hit rate affects inference cost at scale. It presents a simple cost model based on hit rate, explains why a few percentage points of degradation can multiply the bill several times, and identifies the workload conditions under which hit rate matters most. It also covers cache-friendly prompt construction, prefix consistency, tenant isolation and invalidation, observability metrics, a practical optimization checklist, and common misconceptions.

    1. Background: Why a Few Percentage Points Multiply the Bill

    In long-context, multi-turn conversation, code completion, and document Q&A scenarios, input tokens often make up the majority of inference cost. One of the mainstream inference optimization approaches is prefix caching (Prompt Cache / KV Cache reuse): when multiple requests share an identical prompt prefix, the system can reuse the already-computed intermediate state and only compute the new portion incrementally, significantly reducing input-side billing and latency.

    The original observation describes a platform under a daily load of roughly 435 million input tokens with a cache hit rate of about 99.82%, where the actual cost was around 12 USD. If the hit rate degraded to about 95%, the same request volume would cost more than 60 USD, a gap of over 5x. This is not a comparison of any specific product capabilities, but a general conclusion: under high-cardinality workloads, hit rate is a strongly nonlinear factor in the cost curve.

    2. An Abstract Model: Expressing the Bill as a Function of Hit Rate

    Let the total input tokens be T and the cache hit rate be h. The missed portion is billed at the full price and the hit portion at a discounted price. A minimal model can describe the total cost:

    1. Cost(h) = T * [ h * p_hit + (1 - h) * p_miss ]

    Here p_hit is the unit price of the hit portion, p_miss is the unit price of the missed portion, and typically p_hit << p_miss.

    Define the “relative cost multiplier” as:

    1. R(h1, h2) = Cost(h2) / Cost(h1)

    Substituting h1 = 0.9982 and h2 = 0.95, and assuming the hit portion is nearly free (p_hit ≈ 0):

    1. R ≈ (1 - 0.95) / (1 - 0.9982) = 0.05 / 0.0018 ≈ 27.8

    In practice, the hit portion is not zero cost, and there are fixed overheads and output token costs, so the observed multiplier falls back to around 5x. This derivation shows a key fact: what determines the bill is not the hit rate itself, but the absolute size of the “miss rate.” A 95% hit rate means 5% of tokens must be recomputed at full price, while 99.82% means only 0.18% must be recomputed. The two differ by about 27x in “recompute volume.”

    3. Cost Sensitivity: When Does Hit Rate Really Matter

    The impact of hit rate is strongly correlated with three variables:

    1. Input token volume T: The larger the volume, the more a tiny difference in miss rate is amplified. Under small loads, the gap between 95% and 99% may be just a few cents.
    2. Input-to-output ratio: Scenarios where input far exceeds output (long document review, code base Q&A, batch summarization) are the most sensitive to hit rate.
    3. Discount depth p_hit / p_miss: The deeper the discount, the more valuable the hit rate.

    A sensitivity table can support engineering judgment (illustrative, not a specific vendor’s pricing):

    Daily input tokens 95% hit rate 99.8% hit rate Miss volume difference
    10 million Low Low ~480K tokens
    100 million Medium Low ~4.8M tokens
    435 million High (tens of USD) Very low (around ten USD) ~20.9M tokens

    Conclusion: if your daily input volume is under 10 million tokens, optimizing hit rate is not a high priority. Once you reach the hundreds-of-millions scale, hit rate should be treated as a first-class cost metric, on par with latency and availability.

    4. Engineering the Hit Rate: Prefix Consistency and Cache Keys

    The prerequisite for cache reuse is that the prefix is byte-for-byte identical. Common causes of hit rate collapse include:

    • Injecting volatile fields such as timestamps, random IDs, or usernames at the very front of the prompt;
    • Frequently rolling out system prompt updates via canary releases, causing full invalidation;
    • Unstable ordering of tool definitions (function schema), producing a different serialization each time;
    • Insufficient prefix isolation in a shared cache space across multiple tenants, or, conversely, over-isolation that prevents reuse.

    A recommended “cache-friendly” request construction order:

    1. [Stable layer] System instructions + tool definitions + domain knowledge (long-term unchanged)
    2. [Semi-stable layer] Conversation summary / fixed retrieved snippets (versioned incrementally)
    3. [Volatile layer] Current user input, timestamps, random values (placed last)

    Illustrative code (pseudocode, expressing only the layering idea):

    1. def build_prompt(system_v, tools_v, knowledge_v, user_input, nonce):
    2. stable = f"{system_v}|{tools_v}|{knowledge_v}" # main body for hits
    3. volatile = f"{user_input}|{nonce}" # the part that breaks hits
    4. return stable + "\n<<<VOLATILE>>>\n" + volatile

    The key point is to place volatile content last and give the stable layer an explicit version number, so that changes cause a controlled “invalidate once” event rather than scattered, continuous invalidation.

    5. Isolation, Invalidation, and Multi-Tenant Governance

    In multi-business, multi-tenant environments, two goals must be addressed at once: maximizing reuse and preventing data from mixing across tenants:

    • Namespace isolation: Partition the cache key space by business line/tenant to avoid compliance risks from cross-tenant hits;
    • Versioned invalidation: When the knowledge base or system prompt is updated, increment the version number to achieve a one-time, predictable invalidation;
    • TTL and capacity eviction: Set reasonable expiration and eviction policies to avoid hot prefixes being pushed out;
    • Minimizing sensitive information: Do not write user private data into the stable layer, reducing the complexity of isolation and auditing.

    6. Observability: Turning Hit Rate into an Operable Metric

    Without measurement there is no optimization. It is recommended to collect at least the following metrics and connect them to the cost dashboard:

    1. Request level: input tokens, hit tokens, missed tokens, hit flag;
    2. Aggregate level: hit rate by hour/tenant/business line, and the absolute value of missed tokens;
    3. Cost level: cost derived from missed tokens, cost per request, savings from the cache discount;
    4. Alert level: a sharp period-over-period drop in hit rate (a common trigger is template changes or unstable serialization).

    A minimal viable statistical example:

    1. hit_rate = cached_input_tokens / total_input_tokens
    2. miss_tokens = total_input_tokens - cached_input_tokens
    3. est_cost = cached_input_tokens * p_hit + miss_tokens * p_miss + output_tokens * p_out

    7. Optimization Checklist (Ready to Apply)

    • Move stable content such as system prompts, tool definitions, and long documents to the front and version it;
    • Prohibit placing timestamps, random values, or request IDs in the prefix;
    • For retrieval-augmented scenarios, chunk by document version and reuse a fixed block order;
    • For batch tasks, aggregate requests by “identical prefix” to reduce prefix jitter;
    • Establish a daily report of hit rate and missed tokens, and set alerts for sharp drops;
    • Conduct a cost sensitivity review quarterly to assess whether continued optimization investment is worthwhile.

    8. Common Misconceptions

    1. Looking only at the percentage: The gap between 95% and 99% should be converted to the “absolute amount of missed tokens” before evaluation;
    2. Treating the cache as free: The hit portion still has a discounted price and fixed overhead, so p_hit should be retained in the model;
    3. Ignoring output cost: Output tokens usually do not participate in prefix reuse, so long-output scenarios need separate optimization;
    4. Over-pursuing hit rate: If the stable layer changes frequently, forced reuse brings correctness risks, so ensuring semantic stability of the prefix should take priority.

    9. Conclusion

    Under large-scale inference workloads, cache hit rate is not a nice-to-have optimization but a lever that determines the order of magnitude of the bill. By restructuring requests into a “stable prefix + trailing volatile content,” combined with versioned invalidation, tenant isolation, and hit rate observability, a 4-percentage-point gap can be turned into a several-fold cost advantage. The methods above are general engineering practices; specific discounts and billing rules should be based on the official documentation of the chosen cloud platform. In a compliant cloud environment such as Baidu AI Cloud, the cost dashboard described above can be built using general capabilities like log services and monitoring alerts.