KV Cache Compression for Long-Input Workloads: How It Works

    This article explains why reading long inputs costs more than generating output in LLM applications, how KV caches expand based on the attention mechanism, and the three-stage optimization path behind recent long-input models: sequence-level compression, cross-layer sharing, and lower precision. It also covers capacity estimation and a practical rollout order, with cache hit rate, time to first token, and peak memory per request as core metrics.

    1. The Problem: Why Reading Costs More Than Writing

    In applications like AI coding assistants, document Q&A, and log analysis, a single request is often highly asymmetric: the model has to read back tens of thousands of words of source code, test logs, and tool output, yet produces only a few hundred words describing the next action. Across multi-turn sessions, the history is carried forward repeatedly, and the cost on the reading side quickly overtakes the generation side.

    The technical report for a certain new-generation lightweight inference model focuses precisely on this kind of workload—long inputs with frequent continuation. It splits a 40-layer network into 20 causal encoder layers and 20 decoder layers. Long inputs mainly pass through the first half, and the full network runs only when new tokens are generated. Meanwhile, multiple layers share a global KV cache, and some layers even reuse retrieval results along with it. The result is counterintuitive: compression along the sequence dimension is lighter, yet the global KV cache drops to roughly one quarter of the previous generation, with the gains coming mainly from cross-layer sharing and lower storage precision.

    2. Why the Cache Balloons: Starting from the Attention Formula

    Once a token enters the Transformer, it forms hidden vectors layer by layer. Attention reads from the history, and the feed-forward network continues to process them. Q is the query at the current position, K determines the relevance of historical positions, and V carries the information being read back. For a single query and single head, this can be written as:

    1. o = softmax(q · Kᵀ / √c) · V
    2. q ∈ R^{1×c}, K,V ∈ R^{t×c}, o ∈ R^{1×c}

    When generating a new token, the historical K and V can be reused, so they are cached. If each layer stores its own copy, the cache size is approximately num_layers × t × c × 2 × bytes_per_value. The longer the sequence and the more layers there are, the more pronounced the expansion.

    3. A Three-Stage Optimization Path

    1. Sequence-dimension compression: sparse or windowed attention and retrieval-based reading reduce t per layer.
    2. Cross-layer sharing: multiple layers reuse the same global cache, directly shrinking the num_layers factor. This is the main driver of the order-of-magnitude drop here.
    3. Precision and layout: KV uses lower storage precision, paging, and quantized packing to reduce bytes per value.

    The encoder–decoder layered execution further decouples reading from writing: per-layer encoding of long inputs runs only through the first half, and the decoding stage reuses the already-compressed global cache, avoiding redundant computation.

    4. Capacity Estimation and Engineering Rollout

    Estimate the upper bound using peak concurrency × average context length × layer factor × bytes per channel, then use monitoring and alerts to observe the actual hit rate and eviction rate. Rollout order: start with paged caching and lower precision (low risk, fast results), then introduce cross-layer sharing, and finally evaluate sparse retrieval. Cache hit rate, time to first token, and peak memory per request should be tracked continuously as core metrics, so that optimization does not fix one thing while breaking another.