Why KV Cache Stores K and V but Not Q

    LLM decoding caches attention keys and values so each new token does not recompute the whole sequence. This article explains, from the attention formula and the prefill and decode stages, why past queries can be discarded, why K and V are reused, and why the current token’s query must still be computed every step.

    1. The problem

    When a large language model generates text, each step emits one new token, appends it to the input, and continues. Recomputing the entire sequence from scratch at every step makes the cost grow quickly with length. Inference frameworks commonly use a KV cache to avoid that repeated work: they store the keys (K) and values (V) from attention, but not the queries (Q).

    Two points are easy to mix up. First, if past queries are not kept, why must the new token’s query still be computed at every step? Second, why are K and V worth caching? Both answers follow from how attention is computed. The formulas come first, then the decode flow step by step.

    2. Attention recap

    In single-head attention, each position is represented by three vectors. Let the hidden state of position i in a given layer be x_i. Three weight matrices produce:

    q_i = x_i W_Q, k_i = x_i W_K, v_i = x_i W_V

    The output at that position is:

    o_i = Σ_j softmax_j( q_i · k_j / √d_k ) · v_j

    The sum is limited by the mask. A decoder-only model uses a causal mask, so position i can attend only to itself and earlier positions: j ≤ i.

    The formula shows the division of labor. q_i is used only to compute the output o_i at position i. k_j and v_j are read again by every later query. That split determines the cache policy.

    3. Two stages: prefill and decode

    Generation with a KV cache usually has two stages.

    3.1 Prefill

    The user prompt contains n tokens. The model runs one full forward pass over those n tokens. Every layer produces Q, K, and V for all positions and computes attention.

    After this stage, each layer has stored K and V for every position. That is the initial KV cache. Per layer, K has shape [n, d_k] and V has shape [n, d_v]. With multiple heads, each head has its own pair of matrices.

    Only the last position of the final layer is passed through the output layer (the language-model head) to get a distribution over the vocabulary and sample the first new token. Most of the queries computed during prefill are unused once attention is done, so they can be dropped.

    3.2 Decode

    Each decode step handles one new token. Suppose it sits at position t (counting from 0). For every layer, the steps are:

    1. From the new token’s hidden state, compute its own q_t, k_t, and v_t.
    2. Append k_t and v_t to the end of that layer’s KV cache.
    3. Take the dot product of q_t with every cached key from k_0 through k_t, producing a score vector of length t+1.
    4. Apply softmax to the scores, then take a weighted sum of v_0 through v_t to get this layer’s output.
    5. Pass the output through the feed-forward network and the rest of the block, then repeat in the next layer.

    The final layer’s output goes through the output layer to produce a distribution for the new token. The sampled token becomes the input for the next step.

    3.3 Comparing the two stages

    Item Prefill Decode
    Input tokens n, the full prompt 1 per step
    Queries computed Yes, for every position No; only the new token’s query
    What happens to K/V All positions stored at once One position appended per step
    Output Distribution at the last position; first new token Distribution for the new token
    Compute profile Heavy matrix work; compute-bound Mostly vector–matrix products; often limited by memory bandwidth

    4. Why Q is not cached

    The question is whether queries from earlier positions are used again.

    Suppose positions 0 through t−1 are already done. In layer l, the historical query q_i is used only to produce o_i. That o_i exists to form the hidden state of position i in layer l+1. The key k_i and value v_i in layer l+1 are computed from that hidden state, and they are already in the cache. In other words, every effect o_i has on later computation is already fixed inside the cached K and V.

    Later steps therefore do not need historical queries. A past query has a single use and can be discarded after that.

    Two facts should stay separate: past queries are not needed again, while past keys and values are read repeatedly. Each new token’s query must take a dot product with every historical key and a weighted sum over every historical value. Caching K and V avoids a large amount of repeated work. A query belongs only to the current token for that step. Caching it saves nothing and only consumes memory.

    5. Why the new token’s query is still computed every step

    Not needing historical queries does not mean the new token’s query can be skipped. Attention weights come from the scores q_t · k_j / √d_k. Those scores require the current query q_t, which is produced from the new token’s hidden state through W_Q. The cached keys and values do not contain that query, and no earlier query can stand in for it, because each query is tied to one position’s output. Every decode step must therefore compute q_t for the token just added, use it once against the full cached K and V, and then drop it.