KV Cache Optimization for Long-Input Workloads: Inter-Layer Sharing and Hybrid Architectures Reduce Memory to About One Quarter
Long-input, short-output workloads such as coding agents put heavy pressure on prefill compute and decode-time KV cache. This article explains how KV cache arises from attention, how a hybrid encoder-decoder layout separates reading and writing, and how inter-layer global KV cache sharing plus low-precision storage can reduce total KV cache memory to roughly one quarter of the previous generation. It also summarizes practical implications for teams deploying long-context inference.
Why Long-Input Workloads Matter
In real-world LLM applications, one workload pattern is particularly distinctive: very long input and very short output. Consider an intelligent coding tool. After modifying code files, an Agent often needs to read back tens of thousands of words of source code, test logs, and tool execution results, but ultimately produces only a few hundred words of instructions for the next step. As multi-turn tasks continue, context accumulates, and the compute and memory resources the model spends on “reading” can easily exceed those spent on “writing.”
This type of workload poses two core challenges for inference systems:
- Heavy compute in the prefill phase: Processing tens of thousands of tokens at once makes attention compute grow quadratically with sequence length.
- High cache pressure in the decode phase: Each new token generated must read information from the historical KV cache, and the cache grows linearly with sequence length and layer count.
To understand why the second challenge is difficult, it helps to return to the attention mechanism itself.
Where KV Cache Comes From
A language model splits text into tokens. After each token enters a Transformer, it forms different hidden vectors at each layer. Attention layers read information from the preceding context, and feed-forward networks continue processing.
Attention computation involves three core tensors: Q, K, and V:
- Q (Query): the “query” issued by the current position, expressing “what I want to find”;
- K (Key): the “index” of historical positions, used to determine which positions are relevant to the current query;
- V (Value): the information content actually carried by historical positions.
For a single query and one ordinary attention head, the computation can be written as:
o = softmax(q · Kᵀ / √c) · V
Here, q is a 1 × c query vector, K and V are t × c matrices (t is the visible history length and c is the channel count), and o is the result read back. Softmax normalizes relevance scores into weighting coefficients.
The key point is that when generating a new token, the K and V of historical positions are unchanged and can be reused. The system caches them to avoid recomputation—this is the KV cache. If each layer stores its own copy, the cache grows with sequence length and layer count. In extreme cases, even when only one token is generated per step, information must be read from the full historical cache.
A request is therefore divided into two phases: the prefill phase processes the entire input and builds the initial cache; the decode phase generates token by token, appending new cache entries and reading old ones at each step.
Hybrid Architecture: Encoder Layers and Decoder Layers Each Do Their Part
A recent industry optimization idea worth attention is to divide the network by function. In one new model’s technical design, for example, a 40-layer network is divided into the first 20 “causal encoder” layers and the last 20 “decoder” layers.
- When processing long input: mainly the first half of the network runs. Encoder layers complete semantic compression and information aggregation for the long context, and their outputs settle into a compact representation.
- When generating new tokens: the full network runs. The decode phase needs the deep semantics provided by encoder layers and also needs decoder layers to complete final language modeling.
The intuition behind this design is that reading long text does not require running all layers fully; the first half of the network can already extract sufficient context information. Writing new content, however, requires information to be refined layer by layer, so the full network must participate.
Why the Cache Can Drop to About One Quarter
Architecture layering alone is not enough to substantially compress the cache. The real savings come from two coordinated techniques:
1. Inter-layer shared global KV cache
The traditional approach maintains an independent cache for each layer, so 40 layers mean 40 copies. The new scheme lets multiple layers share one global KV cache, and some layers even reuse external document representations introduced by retrieval-augmented generation (RAG). If several adjacent layers share the same cache, total cache size can fall by multiples.
2. Lower storage precision
The cache is stored in a low-precision numeric format. Attention read and write operations are relatively insensitive to precision loss. Reducing KV cache from high-precision floating point to a lower bit-width format can bring near-linear memory savings, while quantization error compensation helps maintain output quality.
Combined, the two techniques produce a result that seems counterintuitive at first but is reasonable: compression along the sequence dimension becomes lighter (more positional information is retained to maintain long-text accuracy), yet the global KV cache still drops to about one quarter of the previous generation. The saved space mainly comes from inter-layer sharing and low-precision storage, not from sacrificing representational capacity along the sequence dimension.
Implications for Developers
- Architecture is a cost factor: Inference cost is not only a matter of parameter count; attention patterns and cache organization have a major impact on actual overhead.
- Read/write asymmetry can be exploited: Long-input, short-output workloads naturally fit a division of labor between lightweight encoder reading and full decoder writing.
- Cache and precision can be decoupled: Storage precision and model weight precision can be designed independently, and low-precision KV is one of the most cost-effective engineering optimizations.
For teams that need to deploy long-context services such as coding assistants, document QA, and agent orchestration, understanding these mechanisms helps make better trade-offs between compute budget and response quality. It also helps estimate memory and throughput requirements more accurately when integrating with cloud inference services and container platforms.
Conclusion
KV cache optimization is essentially about answering a basic question again: when generating the next token, how much needs to be remembered, and at what precision. Inter-layer sharing tells us that multiple layers can reuse the same memory. Low-precision storage tells us that memory does not need to be remembered in such fine detail. Layered architecture tells us that reading and writing can be optimized separately. Together, these three ideas make high-throughput inference possible in long-input scenarios.
