KV Cache Compression for Long Inputs and High Concurrency

    This article explains why KV cache grows so large in long-input, high-concurrency LLM workloads and why reading context often costs more than generating output. It covers the root cause of cache expansion, three orthogonal compression dimensions (sequence-level compression, cross-layer sharing, and lower storage precision), an encoder-decoder split that separates long-input processing from autoregressive generation, and a capacity estimation formula with practical rollout recommendations.

    1. Background: Why Reading Costs More Than Writing

    In applications such as AI coding assistants, document Q&A, and log analysis, a single task often has a highly asymmetric workload: the model must read back tens of thousands of words of source code, test logs, and tool output, yet produces only a few hundred words of next actions. Across multiple turns, the resources consumed by processing input can significantly exceed those consumed by generating output.

    These workloads share three characteristics:

    • Input length far exceeds output length, so computation and memory usage during the prefill phase dominate;
    • Context must carry across turns, so historical results cannot be discarded and must stay resident;
    • Concurrency is high, so total cache size scales linearly with the number of concurrent requests.

    As a result, the focus of inference optimization shifts from “compute faster” to “store more efficiently.”

    2. Root Cause of Cache Expansion: One Copy per Layer

    A language model splits text into tokens, and each token forms an independent hidden vector at every layer. In the attention mechanism, Q is the query issued from the current position, K determines the relevance of historical positions, and V carries the information actually read back. The read operation of single-head attention can be written as:

    1. o = softmax(q * K^T / sqrt(c)) * V
    2. q ∈ R^{1×c}, K,V ∈ R^{t×c}, o ∈ R^{1×c}

    Here t is the visible history length and c is the number of channels. When generating a new token, the K and V of historical positions remain valid, so they are cached for reuse. This is the KV cache.

    The key problem is that if every layer stores its own copy, cache size is roughly proportional to “number of layers × sequence length × number of channels × bytes per element × concurrency.” Multiply the sequence length by ten and the number of layers by two, and memory usage grows accordingly, eventually squeezing out the headroom available for batching and causing throughput to drop.

    3. General Optimization Paths: Three Orthogonal Compression Dimensions

    Widely accepted optimization techniques fall into three categories that are orthogonal to each other and can be stacked:

    1. Sequence-direction compression: sparsify, window, or apply low-rank projection to historical K and V, reducing the number of tokens or channels per layer;
    2. Cross-layer sharing: let multiple layers reuse the same global cache, turning the “number of layers” factor from a multiplicative term into a constant;
    3. Lower storage precision: store the cache at a lower bit width and control quality loss with quantization error compensation.

    An easily overlooked conclusion is that lighter sequence-direction compression does not necessarily mean a smaller total cache. What really determines the total is often the number of layers and the precision.

    4. Layered Encoder-Decoder Execution: Separating Long Inputs from Autoregressive Generation

    One practical engineering design is to split a deep network into two segments: the front segment acts as a causal encoder that digests the long input in a single pass, while the back segment acts as a decoder that runs in full during generation. Long inputs mainly go through the first half, and the full network is activated only when generating new tokens.

    Illustrative pseudocode:

    1. def forward(request):
    2. if request.phase == "prefill":
    3. h = encoder_layers(request.input_ids) # front half only
    4. cache = h.kv # write to global cache
    5. else:
    6. h = decoder_layers(last_token, cache) # full network
    7. cache = update_global_cache(cache, h.kv)
    8. return h

    The benefit of this structure is that the expensive computation for long inputs is confined to shallow layers, while deep layers run only for a small number of generation steps. At the same time, the global cache becomes the single source of truth for context, making cross-turn continuation and reuse easier.

    5. Capacity Estimation and Deployment Recommendations

    Before going live, it is advisable to do a capacity calculation first and move uncontrollable risks forward:

    1. cache_bytes ≈ layers_effective × seq_len × channels × bytes_per_elem × concurrency

    Here layers_effective is determined by the sharing strategy and bytes_per_elem is determined by the storage precision. Deployment can proceed in four steps:

    • Define the workload profile: measure the distribution of input/output lengths and the number of continuation turns;
    • Choose a compression combination: prioritize cross-layer sharing and low-precision storage, then evaluate the strength of sequence-direction compression;
    • Establish a quality baseline: use a fixed evaluation set to monitor output drift caused by cache compression;
    • Add observability: use logging services and monitoring alerts to track cache hit rate, memory watermark, and queueing latency.

    For cloud deployment, you can combine container platforms for elastic scaling and use object storage to hold long-context snapshots, balancing cost and user experience.