KV Cache Compression and Sharing for Long-Input Workloads

    This article explains why read-heavy, write-light workloads put the most pressure on the KV cache, and how a layered design, cross-layer sharing, and lower storage precision reduce cache size. It covers the workload characteristics that drive optimization, the division of labor between a causal encoder and a decoder, a rough model for estimating cache size, and a practical engineering checklist for capacity planning, hit rate, precision tiers, staged execution, observability, and quality regression. Readers will gain a clear cost model for making trade-offs among capacity, latency, and quality in real applications.

    1. Background: Why Read-Heavy, Write-Light Workloads Strain the Cache

    In scenarios such as AI coding assistants, log analysis, and long-document question answering, a typical request looks like this: the model reads back tens of thousands of words of source code, test logs, and tool output, and ultimately produces only a few hundred words describing the next action. The input and output lengths are severely asymmetric, which inverts the resource consumption structure—the compute and GPU memory spent on “reading” clearly exceed what is spent on “writing.”

    To understand this, start with how a Transformer basically works. Text is split into tokens, and each token forms different hidden vectors at every layer as it passes through the network. The attention mechanism reads information from the historical context, and the feed-forward network continues to process it. In attention, the common Q, K, and V can be understood as follows: Q is the query issued by the current position, K is used to decide which historical positions are relevant, and V carries the information that is actually read back. For a single query and single head:

    1. o = softmax(q * K^T / sqrt(c)) * V

    Here q is a 1×c query vector, K and V are the t×c historical key and value matrices, c is the number of channels, t is the visible history length, and o is the read-back result. Softmax normalizes the relevance scores into weighting coefficients.

    When generating a new token, the K and V of historical positions do not need to be recomputed and can be reused directly, so the system caches them. This is the KV cache. If one copy is stored per layer, the cache size grows linearly with sequence length and number of layers; even to generate a single token, the entire historical cache must be read. This is exactly the pain point of long-input workloads.

    2. Workload Characteristics: Long Inputs, Frequent Continuations, and Asymmetric Generation

    Long-input workloads have three key characteristics that determine the optimization direction.

    First, the input length is far greater than the output length. The write cost of the cache is incurred once, but the read cost recurs at every generation step, forming a “write once, read many times” structure.

    Second, the context is frequently continued. Multi-turn tool calls keep accumulating history, and the cache must remain usable across turns, so the hit rate directly determines the actual cost.

    Third, attention access has locality. During generation, not all historical positions are attended to uniformly, which leaves room for “layered processing” and “shared reuse.”

    3. Layered Design: Dividing Labor Between a Causal Encoder and a Decoder

    One effective structure is to split the deep network into two segments: the front segment acts as a causal encoder that handles the long input, and the back segment acts as a decoder that generates new tokens. Processing the long input mainly runs the front half, and the generation stage then runs the full network.

    The benefit of this division is that the encoding stage does not need to activate all layers, which lowers the compute peak of long-sequence prefilling; the decoding stage runs the full network, but each step processes only a single position, so it is still dominated by cache reads overall. A sketch:

    1. input sequence -> [causal encoder: first N layers] -> global KV cache
    2. |
    3. new token -> [encoder reuse] -> [decoder: last N layers] -> output

    Note that this is only a structural abstraction. The specific layer split and whether retrieval results are shared should be based on what public technical materials disclose, and undisclosed parameters should not be guessed.

    4. Why the Cache Can Drop to One Quarter: Two Main Lines, Sharing and Precision

    One phenomenon worth explaining is that compression along the sequence direction is lighter, yet the global cache drops substantially. The reasons come mainly from two places.

    The first is cross-layer sharing. Multiple layers no longer each store a copy of the global K and V; instead they share the same cache, and some layers even reuse the retrieval results as well, avoiding repeated computation and repeated storage. Sharing significantly weakens the multiplier from the “number of layers” dimension.

    The second is lower storage precision. The cache is stored at lower numerical precision, trading capacity for an acceptable quality loss. Only when the two are combined does the result appear in which sequence compression is not aggressive yet the total cache is lower.

    A rough model can build intuition:

    1. cache size ≈ layers × sequence length × channels × bytes per element × number of copies

    Sharing reduces “layers × copies,” low precision reduces “bytes per element,” and sequence compression affects only one of these terms, so the total reduction can far exceed what any single technique achieves.

    5. Engineering Practice Checklist

    • Estimate capacity first: use the formula above to estimate peak cache, reserve room for continuation growth, and avoid mid-session overflow in long conversations.
    • Prioritize hit rate: reusing the cache across turns is more worthwhile than compression, so prioritize keeping the prefix stable and reducing invalid rewrites.
    • Tier the precision: keep higher precision for layers with high stability requirements and use low precision for redundant layers, applying quality tiers.
    • Execute in stages: schedule prefilling and decoding separately, route long inputs through the encoding segment, and lower peak usage.
    • Observability: build monitoring and alerts for cache hit rate, read/write volume, and the per-turn input-to-output ratio, and tune parameters with data.
    • Quality regression: before any compression or sharing goes live, run regression on long-document and multi-turn tool-call scenarios to prevent “saving GPU memory while losing quality.”

    6. Conclusion

    The essential contradiction of long-input workloads is that read cost is paid repeatedly. Layered execution, cross-layer sharing, and low-precision storage break down this contradiction from three levels: structure, reuse, and representation. Understanding the cost model of the cache matters more than memorizing a specific number—it determines how you trade off capacity, latency, and quality in real applications.