KV Cache Compression for Long-Input, High-Concurrency LLM Inference

    This article examines why KV cache compression matters in long-input, high-concurrency LLM serving, where reading cached keys and values often costs more than writing them. It explains what the KV cache stores, then walks through three optimization routes: splitting the network into encoder and decoder stages, sharing cache across layers and reusing retrieval results, and combining lighter sequence compression with lower storage precision. It also provides a capacity estimation formula, a tuning checklist, and common pitfalls to avoid.

    1. The Problem: Why Reading Costs More Than Writing

    In applications like AI coding assistants, document Q&A, and log analysis, a typical turn looks like this: the model first reads back tens of thousands of words of source code, test logs, and tool-call outputs, and then produces only a few hundred words describing the next action. The input-to-output ratio often reaches dozens to one, and after each tool call, the context has to be reconnected once more.

    This leads to two direct consequences. First, the computation in the prefill stage grows with sequence length, stretching time-to-first-token. Second, the keys and values of historical tokens must be read repeatedly, putting pressure on both memory capacity and memory bandwidth at the same time. A widely observed pattern in the industry is that in long-context inference, the bottleneck is often not compute, but the act of moving the cache itself.

    Because of this, the optimization goals for long-input workloads can be broken into three mutually constraining routes:

    • Compress along the sequence dimension: make each token occupy less cache;
    • Share across the layer dimension: let multiple layers reuse the same cache;
    • Reduce storage precision and sparsify: make each value occupy fewer bytes and be read fewer times.

    2. The Basics: What the KV Cache Actually Stores

    A language model splits text into tokens. Each token passes through multiple Transformer layers, forming hidden vectors layer by layer; attention is responsible for pulling information from historical positions, and the feed-forward network continues the processing. Three common abbreviations in attention are Q, K, and V: Q is the query issued by the current position, K is used to judge which historical positions are relevant, and V carries the content that is actually read back.

    For a single query and a single attention head, the computation can be written as:

    1. o = softmax( q * K^T / sqrt(c) ) * 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 visible history length. When generating a new token, the historical K and V can be reused directly without recomputation, so the system keeps them around. This is the KV cache. If each layer stores its own copy, the cache size is roughly proportional to “number of layers × sequence length × number of channels × bytes per element,” and under long sequences it quickly becomes the main consumer of memory.

    There is also an often-overlooked engineering detail: the cache must not only fit, but be readable at every step. During decoding, every generated token requires scanning the entire historical cache, and bandwidth pressure rises linearly with length.

    3. Route One: Splitting the Network into Encoder and Decoder Stages

    One typical design splits a deep network by function: the first several layers form a causal encoder that mainly runs when processing long inputs; the last several layers form a decoder that runs together with the encoder when generating new tokens. With forty layers, for example, this can be divided into a twenty-layer encoder and a twenty-layer decoder.

    The benefits of this approach are a clear division of labor:

    • Long inputs only need to pass through the first half to complete representation building, lowering prefill cost;
    • The decoding stage still runs the full network, so generation quality is not compromised by cutting corners;
    • The encoder output can serve as a conditioning input to the decoder, forming a clean interface boundary.

    The engineering cost to note is that there is a representation-alignment problem between the two parts; if the interface dimensions or normalization strategies are inconsistent, quality is directly affected. At the same time, the two-stage structure places higher demands on scheduling: during batching, requests that “only run the first half” must be orchestrated separately from requests that “run the full network,” otherwise the gains will be eaten up by scheduling fragmentation.

    4. Route Two: Cross-Layer Sharing and Retrieval Result Reuse

    The second route works along the layer dimension. In traditional implementations, each layer holds an independent cache; a sharing scheme lets multiple layers point to the same global cache, and some layers even reuse retrieved results as well, avoiding duplicate writes and duplicate reads.

    You can think of it as “one copy of data, referenced in many places”:

    1. layer_0 ─┐
    2. layer_1 ─┤
    3. layer_2 ─┼──> Global KV cache (shared read/write)
    4. layer_3 ─┤
    5. ... ─┘

    Sharing brings not only reduced capacity but also bandwidth gains: when the same cache is referenced by multiple layers, the number of physical reads drops. The cost is that the expressive power between layers is constrained, and the model must be trained to adapt to this sharing structure; otherwise, forcing sharing at inference time will degrade quality. In addition, after sharing, cache eviction and reclamation must be managed centrally, and any incorrect lifecycle assumption in any layer can cause data races.

    5. Route Three: Lighter Sequence Compression and Lower Storage Precision

    The third route contains two orthogonal techniques. The first is compression along the sequence dimension, for example letting adjacent positions or local windows share representations and merging unimportant positions, keeping the compression ratio relatively restrained so that quality loss stays within an acceptable range. The second is reducing storage precision, using fewer bytes to carry keys and values, combined with block-wise or channel-wise scaling factors to suppress the impact of outliers.

    Worth emphasizing is the combination of “lighter sequence compression + more aggressive cross-layer sharing”: the former is gentle and preserves quality; the latter is aggressive and responsible for bringing the total down. In the end, the global cache is reduced to about one quarter of the previous generation, with the main contributions coming from cross-layer sharing and lower storage precision, rather than pushing sequence compression to the limit. This explains a seemingly contradictory result—compressing more lightly along the sequence dimension actually reduces the total more.

    6. Capacity Estimation and a Tuning Checklist

    Before going live, it is recommended to do a quantitative estimate first, then decide which combination to adopt:

    1. Cache bytes ≈ layers × sequence length × channels × bytes per element × concurrency × (1 - sharing reduction)

    Tuning can proceed in the following order:

    1. Measure the baseline first: record prefill time, single-step decode time, and peak cache usage;
    2. Then try sharing: confirm whether quality remains stable after multi-layer reuse, and observe the bandwidth reduction;
    3. Then tune precision: step down from higher precision gradually, monitoring outliers with scaling factors;
    4. Finally tune compression: prioritize gentle methods like local merging and avoid aggressive pruning;
    5. Supporting governance: combine logging services and monitoring alerts to observe cache hit rate and eviction frequency, use a container platform for elastic scaling, and use object storage to accumulate long-context samples.

    7. Common Pitfalls

    • Looking only at capacity and not bandwidth: the cache fits but cannot be read fast enough, so latency remains;
    • Treating compression ratio as the only metric: ignoring quality regression makes the online experience worse instead;
    • Neglecting scheduling: if the two-stage structure does not match the batching strategy, the gains drop to zero;
    • Lacking a regression baseline: without a fixed evaluation set, you cannot tell whether an optimization is actually effective.

    8. Conclusion

    The essential tension of long-input workloads is that “the cost of reading grows linearly with context.” A viable solution is not a single breakthrough, but the coordination of layered design, cross-layer sharing, and precision and sparsity compression, supported by quantitative estimation and continuous monitoring. Applying this approach to your own inference pipeline is what lets you achieve a controllable balance among memory, bandwidth, and quality.