Understanding KV Cache Compression in Long-Context Inference

    This article breaks down why reading long inputs costs more than generating output in long-context LLM inference, and how a layered causal encoder plus decoder design, cross-layer global KV cache sharing, and lower-precision storage work together to reduce cache usage to roughly one quarter. It covers how the KV cache originates in attention, the asymmetric Prefill and Decode stages, the cache size estimation formula, and practical engineering considerations for capacity planning, precision trade-offs, chunked scheduling, and observability.

    1. Background: Why Reading Costs More Than Writing

    In applications such as AI coding assistants, document question answering, and log analysis, a single interaction often looks like this: a tool modifies a few files, the model then reads back tens of thousands of words of source code, test output, and stack traces, and finally produces only a few hundred words describing the next action. Input length and output length are heavily asymmetric, so inference cost concentrates on the “read” side.

    A model family designed for this kind of workload (referred to here as the “new-generation long-context model”) addresses exactly this pattern of long inputs and frequently continued contexts in its technical report. For comparison, this article calls the previous generation of similar designs the “prior-generation approach.” The comparison uses only the general configuration figures disclosed in public reports and does not involve any specific product parameters.

    2. Where the Cache Comes From: Attention and State Reuse

    A language model first splits text into tokens, where a token may be a character, a word fragment, or a symbol. As each token passes through the Transformer, it forms different hidden vectors at each layer: attention pulls information from the historical context, and the feed-forward network continues to process it.

    Within attention, Q, K, and V can be understood as follows: Q is the query issued by the current position, K is used to determine which historical positions are relevant, and V carries the content that is actually read back. The computation for a single query and a single attention head can be written as:

    1. o = softmax(q * K^T) * V
    2. q ∈ R^(1×c), K,V ∈ R^(t×c), o ∈ R^(1×c)

    Here c is the number of channels and t is the length of the visible history. When generating a new token, the K and V of historical positions can be reused directly, so they are cached. This is the KV cache. If one copy is stored per layer, the cache size grows linearly with the number of layers and the sequence length.

    A request therefore splits into two phases: Prefill, which processes the existing input and builds the state; and Decode, which generates tokens one by one while repeatedly reading the historical cache. With long inputs, Prefill places far greater demands on compute and GPU memory than Decode.

    3. Layered Structure: Causal Encoder Plus Decoder

    The new-generation approach splits a 40-layer network into a 20-layer causal encoder and a 20-layer decoder:

    • When processing long inputs, it mainly runs the first half of the encoder, performing causal encoding block by block.
    • When generating new tokens, it runs the full network, reusing the encoder states rather than recomputing them.

    This asymmetric execution brings two benefits: the Prefill phase can be parallelized and chunked, making it easier to pipeline; and the Decode phase does not need to repeatedly scan the entire raw input.

    4. Sharing and Precision: How the Cache Drops to One Quarter

    Multiple layers share the same global KV cache, and some layers even reuse the retrieval results as well; combined with lower storage precision, the global cache can be reduced to about one quarter of the prior-generation approach. Notably, compression along the sequence direction is lighter instead, which indicates the savings come mainly from cross-layer sharing and storage bit width rather than more aggressive sequence trimming.

    A rough estimate can be written as:

    1. Cache ≈ L_eff × t × c × bytes_per_value × 2(K,V)

    Sharing reduces L_eff from 40 to close to 20 or even lower, and precision drops from 16-bit to 8-bit and then halves again; multiplying the two gets you close to one quarter.

    5. Engineering Implementation

    1. Capacity planning: Reserve GPU memory based on peak context length and concurrency, and bring the cache reuse hit rate into monitoring and alerting.
    2. Precision trade-offs: Low-precision storage requires evaluating numerical stability under long contexts, keeping critical layers at high precision.
    3. Chunking and scheduling: Combine Prefill chunking with request queuing to avoid long inputs blocking short requests.
    4. Observability: Build logging services and metric dashboards for cache hit rate, prefill latency, and the read/write byte ratio per request.

    6. Conclusion

    The bottleneck in long-context inference is “reading.” By combining a layered causal encoder and decoder, cross-layer global cache sharing, and low-precision storage, cache usage can be squeezed to around one quarter without aggressively trimming the sequence. The takeaway for developers is that optimization should shift from simply shortening inputs toward system design focused on state reuse and storage precision.