KV Cache Optimization for Long Inputs: Half-Depth Encoding and Cross-Layer Sharing

    Long-input, short-output workloads can spend more inference resources processing context than generating responses. This article explains how half-depth causal encoding, cross-layer KV cache sharing, and lower-precision storage can reduce prefill computation and cache memory, and what these architectural choices mean for long-context deployment.

    1. Why Processing Input Can Cost More Than Generating Output

    Coding agents, long-document analysis, and multi-turn retrieval-based question answering often have a pronounced imbalance between input and output lengths.

    Consider a coding agent. After each operation, the system adds updated source code, test logs, and tool results to its context. The input can quickly reach tens of thousands of tokens, while the model may generate only a short instruction or code patch. Across repeated iterations, the compute and GPU memory required to process input can easily exceed the resources spent generating output.

    These workloads have two defining characteristics:

    • Inputs are very long and frequently extended.
    • Generation phases are relatively short.

    Traditional decoder architectures are not particularly efficient for this pattern. Every token, whether part of the input or generated output, passes through the full network and leaves a KV cache entry at every layer.

    An architectural alternative divides the network into an encoding section and a decoding section. Most input tokens pass only through the first section, while generated tokens use the full network. Cross-layer sharing of a global KV cache and lower-precision storage can further reduce memory consumption.

    2. Where the KV Cache Comes From

    Understanding these optimizations starts with the cache itself.

    A language model splits text into tokens. As each token passes through a Transformer, it produces hidden representations at successive layers. Attention reads information from the available context, and feed-forward networks further process that information.

    Attention uses three core tensors: Q, K, and V.

    • Q, or query: The current position’s request for information—what it needs to find.
    • K, or key: An index-like representation associated with a context position, used to assess relevance to the query.
    • V, or value: The information carried by that position and retrieved through attention.

    For a single attention head, the output can be expressed as:

    1. o = softmax(q · Kᵀ / √c) · V

    Here, q is the current query vector, K and V contain the keys and values for t visible context positions, and c is the key/query dimension. The softmax operation converts relevance scores into weights, and o is the resulting weighted context representation.

    During autoregressive generation, the keys and values for earlier positions do not change when the model generates the next token. Rather than recomputing them, the inference system stores and reuses them. This stored state is the KV cache.

    For a fixed cache layout, its size grows roughly in proportion to:

    1. sequence length × number of cached layers × bytes per stored value

    With a separate cache at every layer, memory usage grows linearly with context length. Even when generating just one new token, attention still needs to read the relevant historical cache.

    An inference request therefore has two distinct stages:

    • Prefill: Processing the input context.
    • Decode: Generating new tokens one at a time.

    Their resource profiles differ substantially. This distinction is the starting point for half-depth encoding.

    3. Half-Depth Encoding: A Shorter Input Path, a Full Generation Path

    One representative design splits a 40-layer network into two sections:

    • First 20 layers: a causal encoder. Most tokens in a long input complete their contextual processing in this section without passing through the entire network.
    • Last 20 layers: a decoder. When generating new tokens, the model runs both sections, using the full network.

    This arrangement offers two main benefits.

    First, it substantially reduces prefill computation, improving time to first token for long inputs.

    Second, the encoding section can use more aggressive cache-retention strategies—for example, retaining only summary information useful to decoding—rather than keeping a separate, full KV record at every layer.

    The causal constraint is essential. Each encoder position may attend only to the context available up to that position, not future tokens. This preserves compatibility with the causal training objective and avoids information leakage.

    The key distinction is therefore not simply “encoder versus decoder.” It is a shorter computation path for most input tokens, combined with a full-depth path for generated tokens.

    4. Two Cache-Reduction Levers: Cross-Layer Sharing and Lower Precision

    In this family of designs, compression along the sequence dimension may be less aggressive, yet the global cache can still shrink to roughly one-quarter of the previous generation’s size. The main savings come from reducing storage across layers and lowering the precision of stored values.

    Cross-Layer Sharing of a Global KV Cache

    Traditional implementations maintain a separate cache for each layer. Although layers learn different attention patterns, their stored context representations can contain substantial overlapping information.

    A shared-cache architecture allows multiple attention layers to read the same global KV store. Some layers may reuse the same set of keys and values, including representations associated with retrieval-augmented information.

    This reduces duplication along the layer dimension. Instead of storing one complete cache per layer, a group of layers shares one cache.

    If 20 layers share a single cache, the theoretical reduction for those cache copies can be substantial—on the order of an order of magnitude. The overall reduction still depends on how much of the architecture uses sharing and which cache components remain separate.

    Lower-Precision Storage

    Cache memory also scales with the number of bytes used for each stored value.

    Replacing higher-precision floating-point storage with a lower-bit representation, such as a 4-bit quantization approach, can reduce the relevant storage footprint by more than half. The objective is to achieve this reduction with little loss in attention quality.

    The shared representation is also intended to make quantization error more manageable, alongside the reduction in repeated storage.

    Together, these mechanisms explain why lighter sequence compression can coexist with a much smaller total cache. The primary optimization is not necessarily to retain fewer sequence positions. It is to reduce duplication across layers and reduce the number of bits used per value.

    The roughly fourfold reduction describes the combined architectural result, rather than a universal outcome for every model that uses cache sharing or quantization.

    5. What This Means for Deployment

    KV cache memory is a major component of inference-time GPU memory consumption. It directly affects how many requests can remain resident on a GPU and how much context each request can carry.

    Reducing the cache to approximately one-quarter of its former size can provide several practical benefits:

    • Higher concurrent serving capacity: More long-context sessions can fit within the same GPU memory budget, supporting greater throughput.
    • Longer usable context: Coding agents and retrieval-intensive question-answering systems can retain more conversation history and supporting material.
    • A lower deployment barrier: GPUs with more limited memory become more practical for long-context services. Standard container platforms, monitoring, and alerting can support elastic deployment.

    These are capacity benefits, not a guarantee that overall throughput or supported context length will improve by exactly four times.

    For developers building agent applications, model quality should not be the only selection criterion. Architecture disclosures in technical reports can reveal important differences in operating cost.

    Useful questions include:

    • Does the model use different computation paths for input processing and generation?
    • Is the KV cache shared across layers?
    • Which cache storage precisions are supported?

    These choices directly influence the cost of serving long-running, context-heavy applications.

    6. Conclusion

    Long-input, short-output workloads are increasingly common in LLM applications. They benefit from architectural optimizations that target input processing and cached state, rather than focusing only on token generation.

    The approach described here combines three ideas: process most input tokens through a half-depth causal encoder, use the full network for generation, and reduce KV storage through cross-layer sharing and lower precision.

    Understanding the roles of Q, K, and V—and how cache memory scales with sequence length, cached layer count, and storage precision—helps developers evaluate the real benefits and limits of inference optimizations without relying on marketing terminology alone.