Why Autoregressive Decoding Caches Only the KV, Not Q

    This article explains why the KV cache in autoregressive LLM inference stores only keys and values, not queries. It breaks inference into a parallel prefill phase and sequential single-token decode steps, shows why caching turns quadratic recomputation into linear work, and argues from causal masking, last-position output, and reuse patterns that queries are single-use data. It also covers the memory cost of the cache, common misconceptions, and the bandwidth bottlenecks of long-context serving.

    Why Autoregressive Decoding Caches Only the KV, Not Q

    Where the Question Starts: The Illusion of “Generating One Token”

    Many people have the same intuition the first time they look at LLM inference: the model takes in a prompt, emits a new word, and that looks like a single forward pass. So why would anything need to be cached?

    The key point is that autoregressive generation is not one forward pass. It is one prefill plus many single-step decodes. The prompt is processed in parallel all at once, and after that every new token requires another full forward pass, repeated until a stopping condition is met. Caching exists to eliminate that repeated computation.

    One common misunderstanding is worth clearing up. A “single inference step that generates the next token” really does process only one new position during decoding, but the attention computation inside the model still has to cover the entire history. If the key and value tensors for that history were recomputed at every step, the cost would accumulate linearly with sequence length. That is exactly what the cache solves, and it is also why inference hardware feels pressure on memory bandwidth and memory capacity.

    Two Phases: Prefill and Decode

    Autoregressive inference is usually split into two phases.

    Prefill phase: the full prompt sequence is fed in, and every layer computes the hidden representations, queries, keys, and values for all positions in parallel, then produces the first new token. This phase is a compute-bound workload dominated by matrix-matrix multiplication, with high parallelism and generally good hardware utilization. Once it finishes, the keys and values for every attention head in every layer are written to the cache.

    Decode phase: the new token from the previous step becomes the input. Only that position’s query, key, and value are computed, the new key and value are appended to the cache, and then a single query “looks at” all historical keys and values to produce the new representation for that position. The output layer then samples the next token. Each step handles only one position, so this is a matrix-vector workload: small in compute, but it must read the entire history of keys and values, which makes it a classic memory-bound workload.

    Here is illustrative pseudocode for the core loop:

    1. cache = prefill(prompt) # write per-layer K/V for the history
    2. token = cache.last_logits.argmax()
    3. while not finished(token):
    4. q, k, v = project(token) # Q/K/V for the current position only
    5. cache.append(k, v) # append, never overwrite history
    6. rep = attention(q, cache.k, cache.v)
    7. token = sample(head(rep))

    Nowhere in this code is a query vector for a historical position needed. That is the setup for the conclusion below.

    Why a Cache Is Necessary: The Scale of Repeated Work

    Without a cache, step t would have to recompute the keys and values for all t preceding positions. Generating a sequence of length n would cost roughly quadratic work. With caching enabled, each historical position’s key and value are computed exactly once, and the total drops to linear. For long-context scenarios the gap is not a constant factor but an order of magnitude.

    Note also that caching does nothing to reduce the read burden. Every decode step still walks over all historical keys and values, so single-step latency climbs as the context grows. This is why the bottleneck in long-context inference usually lands on memory bandwidth and capacity rather than floating-point throughput.

    Why Only K and V Are Cached, Not Q

    This is the central question, and it can be argued on three levels.

    First, the causal structure means historical representations are already fixed. Decoding uses a causal mask, so a position’s new representation depends only on itself and the content to its left. A newly sampled token never reaches back to change the representation of any earlier position, which means historical keys and values can be safely reused with no recomputation.

    Second, only the last position is used for output. At each step, the only thing actually consumed is the row for the newest token in the final layer’s hidden states. Query vectors for all earlier positions would never enter this step’s output path, so caching them brings no benefit and only takes up space. The only thing that genuinely needs computing is the newest token’s query attending over all historical keys and values to produce its representation in the current context.

    Third, the read/write patterns are asymmetric. Keys and values are “written once, read many times” at every step, which gives them reuse value. A query is used exactly once after it is produced, which makes it single-use data. Caching pays off through reuse, so single-use data is naturally excluded. In one sentence: what gets cached is the context that will be consulted repeatedly, not the question being asked right now.

    Formally, let the context be X′ = X ⊕ [x′]. This step only needs:

    1. q = x′W^Q, k = x′W^K, v = x′W^V
    2. rep = softmax(q (K ⊕ k)ᵀ / √d_k) (V ⊕ v)

    Here K and V come from the cache, while k and v are computed at this step and appended. The query q is used and discarded.

    Cost Assessment: A Quantitative Intuition for Memory Usage

    The cache is not free. Its memory footprint can be approximated as: number of layers × number of heads × per-head dimension × sequence length × 2 (one copy each for keys and values) × bytes per value at the chosen numerical precision × batch size. It scales with sequence length and batch size, so under long contexts and high concurrency it can easily become the dominant consumer of memory.

    Common engineering mitigations include storing the cache at lower precision, sharing or compressing keys and values across adjacent positions, using paged management to reduce fragmentation, and capping the context length per request in high-concurrency serving. These are all general technical directions; the actual implementation has to be evaluated against the capabilities and limits of the inference framework in use.

    Clearing Up Common Misconceptions

    • “Caching changes the output.” It does not. The cache only stores a batch of intermediate tensors for reuse, and it is mathematically equivalent to recomputing them without a cache.
    • “The decode phase saturates compute.” In most cases it does not. Utilization is typically low for small-batch single-step decoding, and the bottleneck is bandwidth and scheduling overhead.
    • “Longer context makes each step faster.” The opposite is true: each step has more keys and values to read, so latency rises.

    Summary

    Caching appears in single-token generation because autoregressive inference is fundamentally one parallel prefill plus many sequential decodes, and a large amount of intermediate results can be reused across those sequential steps. Caching keys and values but not queries comes from three factors working together: causal masking fixes historical representations, output takes only the newest position, and keys and values carry reuse value. Understanding this mechanism explains where the memory and bandwidth pressure in inference serving comes from, and it helps developers make better engineering tradeoffs in batching strategy, context length control, and precision selection.