Why Context Caching Achieves a 90%+ Hit Rate

    This article explains why a 90%+ cache hit rate is normal in AI coding assistants. It covers what prefix caching actually reuses (KV cache rather than text), why coding sessions naturally produce long stable prefixes with short increments, the two physical bottlenecks that lower hit rates (KV cache size and single-layer caching), and practical steps to check and improve caching, along with the cost implications.

    1. The Short Answer: A 90%+ Cache Hit Rate Is Reasonable

    When you write code in a continuous conversation with an AI coding tool and see a 96%–99% cache hit rate on your bill or monitoring dashboard, your first reaction may be that the metric is being measured incorrectly. In fact, this number matches the technical characteristics of this scenario and is close to what you should expect. To understand why, you first need to know what the cache actually stores and why it is so easy to hit while writing code.

    1. The Cache Stores Intermediate Computation, Not Text

    When a large model processes input, it first converts the token sequence into the intermediate states required for attention computation, commonly known as the KV cache (Key/Value Cache). It stores the key and value tensors for every layer and every attention head over the already-processed prefix. These are intermediate products of inference, held in the GPU memory of the inference node.

    When the prefix of a new request is exactly the same as before, these intermediate states do not need to be recomputed and can be reused directly. The cache hit rate measures the proportion of input tokens that can be reused directly without recomputation, not similarity at the character level.

    2. Coding Sessions Are Naturally “Long Prefix + Short Increment”

    The interaction pattern of writing code has one distinct feature: the context only grows and is never modified. A session often contains the requirement description, project structure, several file contents, error logs, and the complete code the model has already generated. Each time the user requests a change, the genuinely new content may be only a few dozen to a few hundred words, while the rest is all repeated prefix.

    1. Input at turn N = [system prompt + conversation history + generated code + current instruction]
    2. └────────── stable prefix (reusable) ──────────┘ └ new ┘

    With this structure, a hit rate above 90% is simply an arithmetic result, not an anomaly.

    2. Why Some Platforms Cannot Do It: Two Physical Bottlenecks

    1. The Cache Is Too Large to Fit in GPU Memory

    Under a standard attention mechanism, the KV cache grows linearly with sequence length, and long-context scenarios can occupy tens of GB of GPU memory. GPU memory is a scarce resource, so even a modest amount of caching gets evicted by other requests, and the hit rate naturally drops. Common industry approaches are compression and sparsification, such as low-rank compression along the sequence dimension, grouped key/value sharing, and mixing sliding windows with global tokens, which reduce cache usage for the same context by an order of magnitude.

    2. There Is Only One Cache Layer, So It Is Cleared When the Session Breaks

    If the cache exists only in the GPU memory of the inference node, then concurrency peaks, session idle time, and node scaling will all invalidate the cache, and users can only recompute everything when they return to continue asking questions. A more robust engineering approach is to layer it: keep hot data in GPU memory, move warm data down to host memory, and store cold data in persistent storage, combined with a prefix hash index for fast lookup and migration. This way, even after a 30-minute interruption, the stable prefix can still be reused.

    3. How to Check and Optimize

    • Check the monitoring metric: confirm that the hit rate is measured by input tokens and distinguishes the system prompt, conversation history, and file contents.
    • Keep the prefix stable: put unchanging content first and place volatile timestamps, random IDs, and retrieval results at the end, so the prefix is not “polluted” and does not trigger a cascade of misses.
    • Control context growth: trim history by task, keep only relevant files and necessary logs, and summarize long documents before including them as input.
    • Make good use of session continuation: advance the same task within the same session to reduce the cold starts caused by creating new sessions.
    • Pay attention to cache lifetime: understand the cache retention policy of the platform you use, especially when you switch to other work in the middle of a long task and then return to continue.

    4. A Rational View from the Cost Perspective

    A high hit rate means most of the input is billed at the cache price, so actual spending is far lower than an estimate based on full input. Therefore, when evaluating cost, you should look not only at the unit price but also at the hit rate and cache retention duration. If a platform’s hit rate stays persistently low in typical coding sessions, it is more likely a problem with the cache implementation or the way it is measured, and is worth verifying carefully during selection. For enterprises that want to build their own inference capabilities, they can deploy inference services on a container platform, use object storage for prefix persistence, and track changes in the hit rate with a monitoring and alerting system. The relevant general capabilities of Baidu AI Cloud can serve as one reference path.