Why LLM Prompt Cache Hit Rates Reach 96% or Higher

    This article explains why prompt cache hit rates of 96% to 99% are normal for long-context coding workloads with stable prefixes, and why the same number can be misleading for highly random request patterns. It breaks down how prompt caching reduces inference cost, compares three different hit-rate definitions, lists common factors that cause cache misses, and outlines engineering practices for stabilizing prefixes, aligning lengths, splitting sessions, adding observability, and running load tests. It also provides four checks for judging whether a reported hit rate is credible, along with practical deployment notes for Baidu AI Cloud.

    1. The Short Answer: 96%–99% Is Not Abnormal

    For long-context, fixed-prefix coding workloads, a prompt cache hit rate of 96%–99% falls within a normal, explainable range. But if a workload is highly random, changes system instructions and code context every turn, and still reports the same number, the figure is more likely a measurement artifact than an optimization result.

    To understand this, you need to unpack what “hit rate” actually measures. It is not a mysterious number determined by model capability. It is a deterministic outcome determined by request structure.

    2. Why Cache Hits Cut Inference Costs So Much

    LLM inference has two stages: first, a forward pass over the input that produces reusable intermediate state (commonly understood as the attention key-value cache); second, autoregressive token-by-token output based on that state. The first stage scales with input length and layer count, and it is the dominant cost in long-context scenarios.

    The core idea of prompt caching is simple: if the current request’s prefix exactly matches a previous request, reuse that intermediate state and skip the repeated forward computation. As a result, the cached portion is significantly cheaper and faster than the uncached portion. Providers typically reflect this with discounted pricing or a separate low-price tier, with the exact ratio defined by each provider.

    One constraint must be emphasized: most implementations require the prefix to match token by token, using a longest-common-prefix match. Once a difference appears in the middle of the prefix, everything after it degrades to a cache miss.

    A typical way to organize requests:

    1. [Stable prefix] System instructions + project conventions + dependency list + directory structure + fixed examples
    2. [Semi-stable section] Relevant file contents (sorted by path and version, then concatenated)
    3. [Volatile tail] Current question, latest changes, temporary debugging info

    3. How Hit Rate Is Calculated: Three Definitions, Very Different Results

    Most disagreements come from inconsistent definitions. The three common ones are:

    1. # Definition 1: Token-weighted hit rate (closest to the bill)
    2. hit_rate_token = cached_tokens / total_input_tokens
    3. # Definition 2: Request-level hit rate (any hit counts)
    4. hit_rate_request = hit_requests / total_requests
    5. # Definition 3: Savings rate (relative to no-cache baseline)
    6. saving = (baseline_cost - actual_cost) / baseline_cost

    In multi-turn coding sessions where each turn adds a small change and prior context is carried repeatedly, the vast majority of cumulative input tokens are repeated prefixes. The token-weighted hit rate naturally approaches or exceeds 96%. The request-level definition is usually lower, because there is always a first request and cold-start requests. When you see 99%, confirm which definition it uses.

    4. Typical Factors That Collapse the Hit Rate

    • Volatile fields in the prefix, such as timestamps, random request IDs, session numbers, or usernames.
    • Dynamic ordering: file lists, dependency lists, or retrieval results change order between turns.
    • Unstable templates: variable-length debug info or truncation logic inserted into the prompt template.
    • Premature truncation: trimming from the beginning to control length, which directly breaks the common prefix.
    • Multiple tenants sharing the same prefix space, causing the cache to be evicted frequently.
    • Cache lifetime mismatched with the workload: after a long idle period the cache has expired, so the first turn necessarily misses.
    • Tool calls and structured output change the actual assembly order, so the prefix is no longer stable.

    5. Engineering Practice: Make Hit Rate Observable and Optimizable

    The first step is to stabilize the prefix. Put invariant content first and version it; put volatile content last. For retrieval-augmented scenarios, sort by a stable key before concatenation instead of dynamically inserting by relevance into the middle of the prefix.

    The second step is length alignment. Keep the prefix length near the block boundaries commonly used by cache implementations, so small tail changes each turn do not trigger a full recompute.

    The third step is session separation. Use independent prefixes for different projects and branches to reduce mutual eviction.

    The fourth step is observability. Use a logging service to record input tokens, cached tokens, uncached tokens, and latency for each request, and set alert thresholds for sudden hit-rate drops. When needed, sample requests into object storage for offline review.

    The fifth step is load-test validation. Build two contrasting workloads, “stable prefix + incremental tail” and “fully random prefix,” and compare measured data against claimed values.

    1. Suggested observability fields:
    2. request_id / prefix_version / input_tokens
    3. cached_tokens / uncached_tokens / hit_rate
    4. latency_ms / cache_state(warm|cold)

    6. How to Judge Whether a Hit Rate Number Is Credible

    Cross-check four points: first, whether the metric is explicitly labeled as token-weighted; second, whether cold-start requests are handled and disclosed; third, whether the billed savings are mathematically consistent with the hit rate and discount rules; fourth, whether the metric changes when the workload pattern changes. If the number stays constant, it is almost certainly a statistical artifact rather than real optimization.

    7. Deployment Notes from a Baidu AI Cloud Perspective

    With Baidu AI Cloud’s LLM services and general cloud products, developers can apply these methods directly: use a container platform to run inference and load tests, use object storage to archive request samples, use logging and monitoring alerts to build a hit-rate dashboard, and attribute costs against cache-tier pricing. Note that cache granularity, lifetime, and billing rules differ across platforms, so always rely on the definitions in your own console and the official documentation rather than copying someone else’s percentages.

    8. Summary

    A 96%–99% hit rate is not the problem. The problem is whether it can be explained by its definition, reproduced by the workload, and verified by the bill. Structuring the prefix, making metrics observable, and making costs attributable is the truly controllable optimization path for long-context applications.