KV cache explained: why LLMs cache K and V, not Q

By May 29, 2026
LLMAIMachine LearningInferenceTransformersPerformance
KV cache explained: why LLMs cache K and V, not Q

What is a KV cache?

A KV cache is the memory where a language model keeps the keys (K) and values (V) it has already computed for earlier tokens, so it does not recompute them every time it generates a new token. Each new token only needs its own fresh query (Q), which it compares against the stored keys and uses to mix the stored values. That is why inference engines cache K and V but never Q.
More precisely: in standard causal autoregressive decoding, the current query reads past keys and values. Past queries are not inputs to the new position's attention output, so retaining them across decode steps provides no reuse for this computation.
That is the scope of "never Q" here. It is not a claim about every architecture or temporary kernel buffer. Prefill computes queries for many prompt positions; this article concerns persistent state reused during decoding.
The explanation follows Hugging Face's cache documentation. It assumes fixed model parameters and an unchanged token prefix and positional configuration.

How KV caching works in LLMs

In Transformer self-attention, each position generates Query (Q), Key (K), and Value (V) projections at each layer. A KV cache retains the past keys and values. Past queries need not persist between decode steps. To see why, start with the attention math.

Attention mechanics

For one attention head, with equal key and value dimensions and an additive causal mask M:
Here Q, K, V ∈ R^(s × d): sequence length × head dimension. Batch and head axes are omitted. With grouped-query attention, several query heads read the same KV head; the stored KV head count can differ from the query head count.
During one-token-at-a-time autoregressive decoding, the model computes the output for the current position m+1 to predict the following token. Its unscaled dot-product scores are:
Expanded across the context:
The query here is only q[m+1], for the current position. Past queries Q[1:m] appear nowhere. Scale these scores by 1/sqrt(d) before softmax; all positions shown are causally allowed for this row.
So each decode step does three things: compute q, k, and v for the new token, append the new k and v to the cache, and attend from the new q over every cached key and value.

Why old queries are not reused

As K grows by one each step, should we also compute Q[1:m] @ k[m+1]^T, representing past positions attending to the new token?
No. Causal language models apply a causal mask:
Position i cannot attend to any j > i. Adding the mask makes those future-position logits -∞, giving them zero probability after softmax. The dot products themselves are not necessarily zero.
Computing those scores would be wasted work. Caching the past queries to enable that computation later would be doubly wasteful.
With an unchanged causal prefix, appending a token does not change earlier positions' representations. Their per-layer K/V remain reusable; their old Q is not needed to compute the new position's output.

The value side

After computing attention probabilities, the output at step m+1:
For full attention, this reads all retained values plus the current one. Caching them avoids recomputing the prefix's value projections. Sliding-window attention only reads the permitted window, so not every model retains all past values.

K cache vs V cache (and why not Q)

The K cache and the V cache play different roles, but both are read on every decode step:
  • Key K[1:m]: cache for future decode steps. q[m+1] @ K[1:m]^T references all past keys.
  • Value V[1:m]: cache for future decode steps. P[m+1,:] @ V[1:m+1] references all past values.
  • Query Q[1:m]: do not cache for future decode steps. The new output uses the current query, not past queries.
For the full-attention case above:
  • Keys are compared with the current query to calculate attention weights. The K cache answers "which earlier tokens matter?"
  • Values provide the vectors combined using those weights. The V cache supplies "what information do they carry?"
  • Queries select information for their own position rather than supplying reusable history to later positions.
With equal key and value dimensions, the K cache and the V cache are the same size, which is where the factor of 2 in the size formula comes from.

KV cache size formula

For uniform decoder-only full attention, equal K/V dimensions, and an unquantized cache, the raw logical payload depends on:
  • L transformer layers
  • n_kv KV heads per layer
  • d head dimension
  • s retained tokens per sequence, including processed prompt and generated tokens
  • b equal-length independent sequences without prefix sharing

Worked example: how to calculate KV cache size

Consider an explicitly hypothetical model, not a Llama checkpoint: 32 layers, 8 KV heads per layer, head dimension 128, and 8,192 retained tokens. Assume FP16 cache elements occupy 2 bytes each.
These are binary units: 1 GiB = 2^30 bytes, not one decimal GB. The calculation is derived from the tensor dimensions, not measured GPU usage or a benchmark of a named model.
Weights, activations, runtime workspaces, allocation overhead, and reserved capacity are additional costs. For unequal lengths, sum the sequence lengths. Sharding, prefix sharing, and windowed or compressed caches need separate accounting. For a deeper walkthrough of KV cache memory, including per-token costs and GQA trade-offs, see KV cache memory sizing with GQA.

MHA vs GQA vs MQA: controlling KV cache memory

The n_kv parameter varies by architecture and directly controls cache size:
  • MHA (Multi-Head Attention): n_kv = n_q. One KV head per query head.
  • GQA (Grouped Query Attention): 1 < n_kv < n_q. Query groups share KV heads.
  • MQA (Multi-Query Attention): n_kv = 1. All query heads share one KV head.
These head-count conventions are documented in HF's Qwen2 configuration reference. They describe architectural choices, not interchangeable serving flags.
Holding other dimensions fixed, reducing KV heads from 64 to 8 reduces this raw payload by a factor of 8. That arithmetic says nothing about quality or latency, and is not a verified description of a particular checkpoint.

Practical serving implications

Budget retained tokens and runtime allocation

The formula grows linearly with retained tokens for full attention. Actual allocation depends on the backend: a static cache can reserve unused capacity, while windowed layers can stop growing. KV memory does not universally dominate weights.

Measure before choosing quantization

HF's cache strategies guide describes quantization and offloading as trade-offs. Quantization can hurt latency at short contexts when GPU memory is sufficient.
Check backend support, residual full-precision storage, metadata, task quality, and end-to-end latency. Offloading trades device memory for transfers. Neither option warrants an unconditional "enable it first" recommendation.

Distinguish paging from compression

vLLM's PagedAttention design explains block-based KV storage and non-contiguous access. Paging manages allocation; it does not reduce the scalar count of a token's K/V.
That page labels itself a historical design document, not a description of today's kernel code. Use it for the concept, not a guarantee that fragmentation disappears or a measured savings percentage for a current release.

Reuse exact compatible prefixes

vLLM automatic prefix caching reuses KV for matching prefixes to skip repeated prefill work. It does not directly reduce the work of decoding new tokens.
The prefix-cache design keys blocks by tokens and preceding context, with additional identity inputs such as adapters. Similar text or the same document at a different prompt position is not enough.
Benefits depend on compatible cache entries being retained and reused. Measure hit rate, time to first token, and decode latency separately on your workload; no deployment speedup is claimed here. For how this differs from the per-request KV cache, see prefix caching vs KV cache.

FAQ

What is a KV cache in simple terms?

It is stored keys and values from tokens the model has already processed. During decoding, each new token reuses them instead of recomputing the whole prefix, and only computes its own query, key, and value.

Why doesn't the KV cache store queries?

The new position's attention output depends only on its own query, q[m+1], plus all past keys and values. Past queries would only matter for earlier positions attending to later tokens, which the causal mask forbids.

What is the difference between the K cache and the V cache?

The K cache holds keys that the current query is compared against to produce attention weights. The V cache holds the values that those weights combine into the output. With equal dimensions they take the same memory.

How do you calculate KV cache size?

Use 2 × L × n_kv × d × s × b × sizeof(dtype). For the hypothetical 32-layer model with 8 KV heads, head dimension 128, 8,192 tokens, and FP16, that is 1 GiB per sequence, or 32 GiB for 32 sequences.

Key takeaways

  • Standard causal decoding reuses past K/V, not past Q, to compute each new position's output.
  • The causal mask makes future-position attention probabilities zero; it does not make the underlying dot products zero.
  • The hypothetical FP16 example is 1 GiB per sequence, or 32 GiB for 32 independent equal-length sequences.
  • Architecture, precision, allocation, and cache sharing determine how the raw payload relates to device memory.
  • Prefix caching saves repeated prefill work for compatible prefixes; it is not a universal latency guarantee.
Next action: inspect your checkpoint's layer count, KV heads, head dimensions, attention pattern, and runtime cache dtype. Calculate the logical payload, then measure allocation and latency with representative request lengths and concurrency.
To turn this into a number for your own model, work through KV cache memory sizing with GQA. For reuse across requests, see prefix caching vs KV cache.