Why KV Cache Can Shrink to One-Quarter: A Deep Dive into Long-Input Inference Optimization
KV cache memory is often the biggest bottleneck in long-context LLM inference. This article breaks down how a newly disclosed inference architecture for agent-style workloads reduces global KV cache size to roughly one-quarter of the previous generation, even with lighter sequence-level compression. It covers three core techniques: splitting the network into an encoder-decoder structure so long inputs only run through half the layers, sharing a global KV cache across multiple layers including retrieval results, and lowering KV storage precision. The article also walks through the memory math, engineering trade-offs, and practical lessons for building long-context agent applications.
Why KV Cache Can Shrink to One-Quarter: A Deep Dive into Long-Input Inference Optimization
1. Background: The “Read-Heavy, Write-Light” Pattern in Agent Workloads
Take a coding agent as an example. A typical workflow round works like this: after the model modifies a few files, it needs to re-read tens of thousands of characters of source code, test logs, and tool outputs, then generates only a few hundred characters of instructions for the next step. This loop may repeat dozens or even hundreds of times, meaning the model spends far more compute and memory on “reading input” than on “generating output.”
A recent technical report from a next-generation model describes an inference architecture designed precisely for this kind of “long input, frequent continuation” workload. Several key figures from the report are worth examining:
- The network has 40 layers, split into 20 causal encoder layers plus 20 decoder layers;
- When processing long inputs, mostly the first half of the network runs; when generating new tokens, the full network runs;
- Multiple layers share a global KV cache, and some layers even reuse retrieval results as part of it;
- Despite lighter compression along the sequence dimension, the global KV cache shrinks to roughly one-quarter of the previous generation’s design, mainly due to cross-layer sharing and lower storage precision.
This article does not focus on a specific product. Instead, it abstracts the general technical logic behind this design and explains three things: why the KV cache is the core bottleneck in long-context inference; how layered scheduling reduces wasted computation; and how sharing and quantization compress memory usage.
2. Foundations: Attention and the KV Cache Memory Ledger
Language models split text into tokens. As each token passes through the Transformer, it produces hidden vectors at every layer. The attention mechanism is responsible for “reading information from prior context,” and its core can be simplified as:
o = softmax(q · Kᵀ) · V
Here q is the query vector at the current position, K and V are the keys and values of the t historical positions, and o is the result read back. Intuitively: q issues a request for “what am I looking for,” K determines which historical positions are relevant, and V carries the content being read back.
The key point is the autoregressive nature of generation: as each new token is produced, the K and V values of historical positions never change and can be reused. To avoid recomputing them, the inference engine caches the K and V of every historical token at every layer — this is the KV cache.
Its memory footprint can be estimated as:
KV cache size ≈ layers × 2 (K and V) × sequence length × hidden dimension × bytes per element
Each of the four factors scales the footprint linearly: the longer the context, the more layers, the larger the hidden dimension, and the higher the precision, the bigger the cache. When context reaches tens or hundreds of thousands of tokens, the KV cache often occupies more memory than the model weights themselves, making it the first bottleneck of long-context inference.
3. Path One: Encoder-Decoder Splitting to Avoid Running the Full Network on Long Inputs
In a conventional Decoder-only architecture, every layer participates in processing each input segment, and every layer writes its own KV cache. The architecture described above splits the network into two segments:
- Causal encoder segment (first 20 layers): processes the input text, producing compressed intermediate representations and a global KV cache;
- Decoder segment (last 20 layers): generates new tokens.
The scheduling strategy is “inputs run half the network, generation runs the whole thing”:
- Prefill phase: only the encoder segment runs, encoding the long input and producing the shared cache;
- Decode phase: the full network runs to generate tokens, but the historical context for new tokens relies mainly on the shared cache already produced by the encoder segment.
This design yields a double benefit:
- Compute: the encoding cost of long inputs is roughly halved, since the second half of the network does not participate in input processing;
- Cache: the middle layers of the decoder can read from the shared cache instead of independently maintaining a full KV copy per layer.
This explains a counterintuitive phenomenon: even though this design is “lighter” along the sequence dimension (compressing less aggressively per token, so each cached token retains more information), the global KV cache ends up smaller. The savings come from structural cross-layer reuse, not from more aggressively discarding token information.
4. Path Two: Multi-Layer Cache Sharing and Reusing Retrieval Results
The second key optimization is cross-layer sharing. In Transformers, the key-value representations of adjacent layers are usually highly similar, which provides a statistical basis for reuse across layers. The design allows multiple layers to reference the same global KV cache rather than each holding a private copy.
Furthermore, some layers even fold retrieval-augmented results (e.g., external knowledge recall) into the shared cache: once a retrieval result is written, multiple layers can reuse it, avoiding repeated retrieval, repeated encoding, and repeated caching.
A conceptual sketch of this structure:
# Conceptual illustration, not an actual implementationglobal_kv = KVStore(precision="low") # globally shared, low precisionfor layer_id in decoder_layers:if layer_id in shared_layers:kv = global_kv.get(...) # reuse the shared cache directlyelse:kv = compute_kv(...) # compute independentlyglobal_kv.put(kv)
Note that sharing a cache means these layers read the same K/V during attention, which can subtly affect representational capacity. The technical report chooses to share the cache only for some layers while others keep independent copies — a deliberate trade-off between memory savings and model quality. This is a generalizable design pattern: evaluate similarity layer by layer, then decide the sharing scope.
5. Path Three: Lower Storage Precision
The third optimization is lowering the storage precision of the KV cache (for example, from high-precision floating point to a low-precision numeric format). Quantization has a relatively mild impact on attention because the softmax-weighted sum is insensitive to local numerical errors. In practice, several points deserve attention:
- Handle K and V separately: K participates in dot products with
q, so quantization error directly affects relevance ranking and often requires finer quantization granularity; V only participates in weighted sums and tolerates more error; - Per-channel scaling: maintaining a scaling factor per channel significantly mitigates precision loss caused by outliers;
- Per-layer configuration: shared vs. independent layers, and shallow vs. deep layers, differ in quantization sensitivity, so precision can be chosen per layer.
Combining the three paths, the cache footprint can be formalized as:
new size ≈ old size × (fraction of independent layers) × (precision ratio)
When the fraction of independent layers drops to about one-half and precision is also halved, the overall cache shrinking to one-quarter of the original is a natural outcome — even with lighter sequence compression.
6. Engineering Lessons and Limitations
This architecture offers several direct lessons for developers building long-context applications:
- The optimization space for long-context inference lies in structure, not just algorithms: system-level designs such as cross-layer sharing and segmented scheduling can stack with algorithmic optimizations like sparse attention.
- Agent applications should care about context lifecycle: when continuing generation over the same long context repeatedly, separating prefill from decoding maximizes the value of cache reuse; design tool-call chains to reuse sessions wherever possible and avoid needless re-encoding.
- Higher precision is not always better: KV cache precision is a three-way trade-off among memory, bandwidth, and quality; low-precision caches also reduce memory-access bandwidth, which indirectly improves decode throughput.
- Evaluate on real workloads: compression ratios must be evaluated alongside actual accuracy on tasks such as long-document QA and code continuation — don’t fixate on memory numbers alone.
Of course, the design has costs: encoding inputs through only “half the network” limits the information capacity of the intermediate representations; cross-layer sharing may lose layer-specific fine-grained information; and low-precision caches may affect precision-sensitive tasks (such as exact numeric extraction or strict format adherence). Deployment should be accompanied by regression testing tailored to your own workloads.
7. Conclusion
Shrinking the KV cache to one-quarter is not the magic of a single trick, but the product of three factors: structural sharing + segmented scheduling + low-precision storage. The encoder/decoder split reduces the number of layers that must maintain caches; multi-layer sharing and retrieval-result reuse eliminate redundant copies; and low-precision storage directly compresses the bytes of each cached element. Understanding this memory ledger not only explains a model’s performance figures, but also guides reasonable memory and cost trade-offs in your own LLM inference and agent system designs.
