Layered Encoding-Decoding for Compressing Long-Input KV Cache
This article explains how a layered encoding-decoding architecture can reduce KV cache memory and bandwidth for long-input, read-heavy LLM workloads. It covers the split encoder-decoder design, global KV sharing across layers, lower-precision storage, accuracy tradeoffs, and practical engineering considerations for developers building long-context agent systems.
Language models split text into tokens, and each position forms hidden vectors across layers. Attention uses the current query to match historical keys and then reads the corresponding values by weight to aggregate information. When generating the next token, the keys and values at historical positions do not change, so the system caches them. This is the KV cache. Cache size grows linearly with sequence length, layer count, head count, and precision, and it quickly becomes a memory and bandwidth bottleneck in long-input workloads that are extended repeatedly.
A typical coding agent loop first reads tens of thousands of words of source code, test logs, and tool output, then generates only a few hundred words for the next action. After repeated rounds, the model spends far more computation and storage on “reading” than on “writing.” If every layer in the full network stores high-precision keys and values separately for all historical positions, GPU memory is quickly exhausted, and continuation latency rises because of repeated data movement.
One targeted architecture splits a forty-layer network in half. The first twenty layers act as a causal encoder and are responsible only for compressing extremely long input into a compact representation. The last twenty layers act as a decoder and run the full network only when generating new tokens. During long-input processing (prefill), mainly the first half runs; during generation, the second half is activated. In this way, computation in the first half is spread across the entire input, while the second half executes only for newly added tokens, naturally matching a read-heavy, write-light workload.
More importantly, global KV is shared across layers. Rather than each layer independently storing a complete cache, several layers share the same global keys and values, and some layers also reuse the retrieval results. Sharing immediately reduces the number of stored copies from the number of layers to the number of sharing groups. Combined with lower storage precision, for example from sixteen bits to eight bits or lower, the size is reduced further. With both changes combined, although compression along the sequence dimension is lighter than in the previous generation, total global KV can still be reduced to about one quarter. The savings come mainly from “storing fewer copies” and “making each copy smaller,” not from more aggressive sequence compression.
The accuracy loss caused by sharing is compensated by residual and feed-forward networks in later layers. Because the encoder has already compressed long-range dependencies into a compact representation, the decoder only needs fine-grained interaction within a local window, so the information loss from sharing is limited to an acceptable range. Low-precision storage takes advantage of the fact that attention scores are relatively insensitive to quantization error, significantly reducing bandwidth with almost no impact on generation quality.
Three engineering points also matter. First, the division of sharing groups should match layer function: shallow layers are more local-pattern oriented and can share more aggressively, while deep layers are more semantic-integration oriented and should use smaller sharing groups. Second, precision selection should align with the hardware to avoid extra dequantization overhead canceling the benefits. Third, during continuation, the timestamps of the encoder output and decoder cache must remain consistent; otherwise, position misalignment occurs. In practice, ring buffers or segmented appends can be used to maintain consistency.
For developers, this approach maps directly to their own long-context systems: first measure the input-to-output ratio of real workloads, then decide encoder depth; replace per-layer independent caches with inter-layer sharing; and run small-scale ablations between precision and quality. Monitoring should cover cache hit rate, continuation latency, and generation quality at the same time, avoiding optimization of size alone while ignoring end-to-end experience. As input continues to grow, encoder output can also be made into a persistable intermediate representation and reused across sessions to further amortize the first prefill cost.
In summary, layered encoding-decoding combined with global KV sharing and low-precision storage is a systematic optimization for scenarios where reads far outnumber writes, not a single-point trick. Understanding its motivation and tradeoffs helps support longer and more frequent agent continuation inference under limited GPU memory.
