KV Cache Compression for Long-Input LLMs: Cutting Memory to One Quarter
Long-input, short-output workloads such as coding agents make the KV cache a dominant inference cost. This article explains why the cache grows, then examines a representative approach that combines encoder-decoder split execution, cross-layer global KV cache sharing, and lower-precision storage to reduce cache memory to roughly one quarter. It also analyzes the cost structure behind the savings and outlines practical evaluation criteria for long-context models.
1. Background: The Hidden Cost of Long-Input Inference
In large-scale text generation, one workload pattern stands out: very long input and relatively short output. A typical example is a coding agent. It may need to read back tens of thousands of tokens of source code, test logs, and tool outputs, and finally generate only a few hundred tokens of next-step instructions. After multiple rounds of this loop, the compute and memory resources the model spends on “reading” can easily exceed those spent on “writing.”
Traditional autoregressive models generate token by token. For each new token, the attention mechanism must look back over the full historical context. To avoid recomputation, the system caches the Key (K) and Value (V) at historical positions across layers. This is commonly called the KV cache. This creates a direct tension: the longer the sequence, the larger the cache; the more layers, the larger the cache. When the input reaches tens or even hundreds of thousands of tokens, the cache itself becomes a dominant term in inference cost.
So a technical question worth analyzing is: can the cache footprint for long-input inference be greatly reduced while minimizing quality loss? A recent technical report describes a representative approach whose core can be summarized in three ideas: segmented execution, cross-layer sharing, and low-precision storage.
2. First, Understand Why the Cache Expands
A language model splits text into tokens. After each token enters a Transformer, it forms hidden vectors at each layer; attention layers read information from context, and feed-forward networks continue processing. Three common abbreviations in attention are Q, K, and V:
- Q (Query): the “query” issued by the current position, representing what I want to find;
- K (Key): the “index” of historical positions, used to judge relevance;
- V (Value): the “content” of historical positions, carrying the information to be read back.
Taking a single query and one attention head as an example, the computation can be written as:
o = softmax(q · Kᵀ) · V
Here q ∈ R^{1×c} is the current query vector, K, V ∈ R^{t×c} contain the keys and values of t visible historical positions, and o ∈ R^{1×c} is the read-back result. Softmax turns relevance scores into weighting coefficients. This formula is only for building intuition; actual compression and sharing implementations change the specific storage method.
The key point is that when generating the next token, the K and V of historical positions do not need to be recomputed and can be reused directly, so the system stores them. If each layer stores its own copy, the cache scale is roughly:
Cache size ≈ number of layers × sequence length × heads per layer × head dimension × 2 (K and V) × bytes per precision
When the number of layers reaches dozens and the sequence length reaches tens of thousands, the cache alone may occupy tens of GB of GPU memory. This is why long-context inference is expensive.
3. Breaking Down the Approach: Segmented Execution + Layer Sharing + Low Precision
Encoder-decoder split execution
The approach splits the originally unified multi-layer network into two segments: the first half acts as a causal encoder, and the second half acts as a decoder. When processing long input, mainly the first half runs; when generating new tokens, the full network still runs.
The insight behind this design is that the “understanding” task in the long-input phase and the “generation” task in the decoding phase have different computational needs. On the input side, the encoder mainly performs context compression and representation; on the output side, full generation capability is still required. As a result, the cache growth pattern changes: during the input phase, only the cache states related to the encoder part need to be maintained, rather than the full cache of all layers in the entire network.
Multi-layer shared global KV cache
The more critical savings come from cross-layer sharing: multiple layers reuse the same global KV cache, and some layers even reuse retrieval results as well. In traditional architectures, each layer maintains independent K and V, so doubling the number of layers nearly doubles the cache; a sharing design breaks this linear relationship.
An intuitive way to understand it: if 20 decoder layers share one global cache instead of storing one copy per layer, then decoder-side cache pressure can ideally drop by an order of magnitude. Of course, full sharing would sacrifice differences in representation across layers. Practical schemes usually adopt compromises such as partial-layer sharing, grouped sharing, or on-demand retrieval to balance memory and quality.
Lower storage precision
The third idea is to reduce cache storage precision, for example by storing K and V in low-bit floating point or quantized formats. Cache memory usage is proportional to the number of bytes per element: moving from high precision to low precision can directly compress space proportionally without changing the number of cache entries. Combined with cross-layer sharing, the multiplicative effect brings total cache size down to roughly one quarter of the original. In other words, compression along the sequence dimension is relatively modest; most of the savings come from the sharing structure and lower precision.
4. Why “Lighter Sequence Compression” Can Be More Cost-Effective
One easily confusing phenomenon is that this type of approach is not aggressive in sequence-dimension compression, yet the cache drops sharply. The reason lies in the cost structure:
- The layer dimension is an amplifier. Sequence length contributes only linear growth; multiplying by the number of layers is the main overhead. Sharing directly reduces the layer dimension.
- The input phase runs only half the network. The encoder segment handles fewer layers, naturally reducing the intermediate states that must be persisted.
- Lower precision is a universal multiplier. Regardless of how the cache is organized, reducing the number of bytes is always effective.
- Long-input, multi-turn continuation scenarios amplify the benefits. Agent-style workloads repeatedly read long context, and the cache lives for a long time, so any compression is amplified.
5. Practical Implications
For developers, the value of understanding this approach lies in evaluating and selecting long-context models. You should not only look at the surface number of “how many tokens are supported,” but also focus on:
- Cache layering and sharing strategy: whether all layers are independent, and whether grouped sharing is supported;
- Cache quantization capability: storage precision and whether on-demand dequantization is supported;
- Segmented execution mechanism: differences in compute paths between the input and generation phases;
- Measured memory footprint and throughput under your own workload, rather than only theoretical context length.
For deployment, you can combine general cloud infrastructure (such as GPU container platforms, elastic inference services, object storage, and logging systems) to deploy this type of long-context inference service, and choose an appropriate cache strategy based on the input/output ratio. For agent workloads where input is much larger than output, an encoder-decoder split execution architecture plus shared cache is often more cost-effective than simply adding more GPU memory.
6. Summary
The essential tension of long-input inference is that attention needs to remember history, and the cost of remembering history grows linearly with sequence length and layer count. Splitting the network into a causal encoder and a decoder, letting multiple layers share a global KV cache, and adding low-precision storage reduce overhead along different dimensions. Ultimately, with relatively modest sequence compression, the cache footprint drops to about one quarter of the original. This provides a clear and practical engineering pattern for serving high-intensity agent workloads with long-context models under limited GPU memory.
