KV Cache Compression for Long-Input Workloads: A Technical Breakdown

    This article explains why long-input workloads put heavy pressure on the KV cache and how a layered architecture, sequence-direction compression, and low-precision storage work together to reduce it. It covers the prefill and decode phases, why AI coding scenarios amplify cache pressure, the division of labor between a causal encoder and a decoder, the difference between sequence compression and cache compression, and the metrics to use when evaluating these optimizations.

    1. Where the Problem Comes From: One Request Split into Two Phases

    Large language models split text into tokens, where a token may be a character, part of a word, or a symbol. As each token passes through the Transformer, it produces different hidden vectors at each layer: the attention mechanism retrieves information from the historical context, and the feed-forward network processes it further.

    Attention involves three common abbreviations: Q, K, and V. You can treat Q as the query issued from the current position, K as what determines which historical positions are relevant, and V as the content that is actually read back. For a single query and a single attention head, the read-back result can be written as:

    1. o = softmax(q · Kᵀ / √c) · V
    2. where q ∈ R^{1×c}, K, V ∈ R^{t×c}
    3. c is the number of channels, t is the visible history length

    When generating the next token, the K and V of the historical positions are still valid, so the system caches them. This is the KV cache. If one copy is stored per layer, the cache grows with sequence length and layer count; even when generating just one new token, the system must read from the historical cache.

    As a result, a request is typically split into two phases:

    • Prefill phase: all input tokens are processed at once. It is compute-intensive, with the main cost being matrix multiplications and attention computation.
    • Decode phase: only one token is added at a time, and the historical cache must be read repeatedly. The main bottleneck shifts to memory bandwidth and cache capacity.

    2. Why AI Coding Scenarios Amplify Cache Pressure

    After modifying a file, an AI coding tool may read back tens of thousands of words of source code, test logs, and tool-call output, yet produce only a few hundred words describing the next step. Across multiple rounds, the resources a model spends on “reading” can easily exceed those spent on “writing.” This kind of workload has three characteristics:

    1. Input is far larger than output, so prefill takes up a large share.
    2. Context is frequently continued, so the cache must be retained across rounds.
    3. Sessions contain many repeated fragments, such as unchanged file content.

    The conclusion is straightforward: for models optimized for long inputs, the gains come mainly from lowering the cost of “reading,” not from speeding up “writing.”

    3. Layered Architecture: Dividing Labor Between a Causal Encoder and a Decoder

    One workable design splits a 40-layer network into a 20-layer causal encoder and a 20-layer decoder:

    • When processing long inputs, mainly the first half runs, compressing the long sequence into a more compact representation.
    • When generating new tokens, the full network runs to preserve generation quality.

    The point of this asymmetric structure is to send “reading long text” and “writing new tokens” down different compute paths, confining expensive operations to the phases that truly need them.

    4. Sequence-Direction Compression Is Not the Same as Cache Compression

    Compressing along the sequence direction is lighter: it sparsifies, chunks, or summarizes historical positions to reduce the number of positions that participate in attention. Reducing the global KV cache to about one quarter, by contrast, means the actual volume of resident key-value data drops. The two can vary independently: fewer positions do not necessarily mean a proportional cache reduction if every layer and head still stores full precision.

    The cache reduction usually comes from three things stacked together:

    • Cross-layer sharing: multiple layers reuse the same global cache, avoiding duplicate per-layer storage.
    • Retrieval-result reuse: some layers also share the content they retrieve.
    • Lower storage precision: keys and values are stored in a quantized or low-bit format, trading controllable precision for capacity.

    Illustrative pseudocode:

    1. # Pseudocode: conveys the idea only, not a real API
    2. if stage == "prefill":
    3. rep = causal_encoder(input_ids) # run only the first half
    4. kv_global = quantize(rep.kv) # store at low precision
    5. else:
    6. kv = dequantize(kv_global) # retrieve during decode
    7. token = full_model(kv, last_hidden) # run the full network
    8. kv_global = update(kv_global, token)

    5. How to Evaluate This Kind of Optimization

    Four metrics are worth watching: peak cache capacity, prefill latency, decode-phase bandwidth usage, and quality regression on long-context tasks. When evaluating, be sure to fix the input-length distribution and the number of continuation rounds; otherwise, capacity and latency numbers are not comparable. In engineering practice, you can also combine general techniques such as session-level cache reuse, logging services, and monitoring alerts to locate hotspots, container platforms to host elastic inference instances, and object storage to hold long-text material.

    6. Key Takeaways

    The core tension in long-input workloads is “read-heavy, write-light.” Layered compute, cross-layer and intra-layer sharing, and low-precision storage must work together to compress the cache significantly while preserving generation quality. To judge whether any approach is effective, return to the three observable metrics—cache capacity, latency, and quality—rather than looking at a single compression ratio.