KV Cache Optimization for Long-Input Workloads: How Causal Encoder-Decoder Separation Works

    Agent workloads read far more tokens than they generate, which makes the KV cache a major memory bottleneck. This article explains an architecture that splits a model into a causal encoder for input tokens and a full decoder for generation, then combines the split with cross-layer KV sharing and low-precision KV storage. It covers why the design suits input-heavy workloads, how the savings reduce the KV cache to about a quarter of a conventional design, how it compares with other compression approaches, and what to check before adopting it.

    1. Background: Agent Workloads Read Far More Than They Write

    Consider a typical coding agent loop. After the model modifies a file, it reads tens of thousands of words of source code, test logs, and tool outputs back into its context. It then generates only a few hundred words as the next action. Across repeated cycles, a clear asymmetry emerges:

    • Input side: The token count is large, but each input token needs to be processed only once. After that, it exists only as history that attention reads.
    • Output side: The token count is small, but each new token runs a full computation that reads the history and writes new content.

    Under this load, the resources spent on input, especially KV cache storage and memory access, can easily exceed those spent on generation. A traditional Transformer is symmetric throughout. Every token, whether input or output, passes through all layers and leaves K and V vectors at each layer. The cache size grows linearly with the number of layers and the sequence length, which makes it the main bottleneck in long-context inference.

    2. Core Idea: Input Takes Half the Network, Generation Uses the Full Network

    To address this asymmetry, a recent class of architectures splits the network along its depth into two parts. This example assumes 40 layers in total:

    1. First 20 layers: causal encoder. When processing long input, tokens pass only through this first half. Its outputs serve as compressed context representations that the second half reads.
    2. Last 20 layers: decoder. When generating new tokens, the full 40-layer network still runs, which keeps generation quality intact.

    The reasoning behind this design is that the job of an input token is to be understood and to leave retrievable memory, not to produce output. For input tokens, the deep processing in the second half is largely redundant. The K and V vectors it produces mostly serve attention reads by later tokens. If input tokens take only the first half, both the input-side computation and KV storage drop by nearly half. On the output side, generated tokens make up a very small share, so the cost of full computation is negligible.

    One constraint is essential: the encoder stage must remain causal. Each position can see only the history to its left. This keeps the information flow of prefill and the later generation stage strictly aligned, so prefill and decoding never work from inconsistent context.

    3. How the Cache Drops to About a Quarter

    Based on the analysis above, limiting input to 20 layers should roughly halve the KV cache. The observed result is more aggressive: the global KV cache falls to about a quarter of the conventional approach. Two more compression methods contribute.

    1. Cross-layer global KV sharing. Adjacent layers or groups of layers no longer each keep an independent cache. Instead, they share one global KV. The rationale is that in deep networks, neighboring layers attend to history in highly similar patterns, so storing them separately adds limited information. The design can choose which layers share and which keep independent caches. Typically, retrieval-oriented layers close to the input keep independent KV, while layers near the output reuse the shared cache. This lets teams balance quality against capacity.

    2. Lower storage precision. The KV cache is memory-access-intensive data. Storing it at lower bit widths, such as reducing 16-bit values to 8-bit or even lower, directly and linearly reduces memory usage. Because K and V are objects that attention reads rather than final outputs, they generally tolerate quantization error better than weights and activations. In practice, low-precision KV storage is a cost-effective optimization.

    Combining the two methods explains the drop to about a quarter. Halving the layers (×1/2) contributes the first reduction. Sharing across layers saves a further portion, and compressing precision (roughly ×1/2) contributes the last. This compression happens along the layer and precision dimensions. Compression along the sequence dimension, such as dropping, merging, or sparsifying historical tokens, can stay lighter. That restraint is the key to preserving long-context recall.

    4. Comparison with Other Compression Approaches

    To see where this approach fits, compare it with the mainstream methods:

    • Sequence compression (sliding windows, token eviction, sparse attention): These methods directly reduce the number of cached history positions and are simple to implement. However, they risk losing information from distant context.
    • Attention architecture changes (linear attention, cross-layer KV sharing, and similar): These methods change how attention computes and stores state. The gains can be large, but they involve training and deeper modifications.
    • The encoder/decoder split with cross-layer sharing and low-precision storage (discussed in this article): Starting from the asymmetric workload, this approach concentrates compression on the layer and numerical dimensions and keeps the sequence dimension intact.

    The third approach fits agent and long-document question answering best, where inputs are long and outputs are short. For conversational scenarios with short inputs and long multi-turn generation, the gains are more limited. Architecture choices should always match the shape of the workload.

    5. Pseudocode Sketch

    The following sketch describes the differences between the prefill and decoding stages. It illustrates the logic only and is not a real interface:

    1. # Prefill: input tokens pass only through the causal encoder segment
    2. for layer in encoder_layers: # first 1/2 of the layers
    3. hidden = layer(hidden, kv_cache.shared)
    4. # Decoding: new tokens run through the full network
    5. for i, layer in enumerate(all_layers):
    6. if i in shared_kv_layers:
    7. kv = kv_cache.shared # reuse the shared cache (low-precision storage)
    8. else:
    9. kv = layer.make_kv(hidden) # a few retrieval layers keep independent caches
    10. hidden = layer(hidden, kv)

    6. Practical Takeaways for Engineering

    1. Measure the workload shape before choosing a design. Monitor production requests to measure the input-to-output token ratio. If reads far outnumber writes, cross-layer sharing approaches will deliver the largest gains.
    2. Prioritize precision compression for the KV cache. The KV cache tolerates quantization well, so compressing it is usually safer and faster to show results than compressing weights.
    3. Ablate the sharing groups. Sharing too aggressively can remove attention patterns unique to deep layers. Determine the group granularity through perplexity and regression tests on downstream tasks.
    4. Keep prefill and decoding consistent. Any segmented architecture must keep the information flow of both stages aligned. Otherwise, the model may fail to see history or may misread it during generation.

    In short, KV cache optimization is moving from “subtracting along the sequence” toward “reallocating computation according to workload asymmetry.” The combination of segmented networks, cross-layer sharing, and low-precision storage offers a reference pattern for long-input, short-output agent workloads. It balances recall quality and resource efficiency.