Short answer. The KV cache keeps the key and value vectors of all previous tokens, in every attention layer. With the KV cache, each decode step runs the model on the new token only. Without it, each decode step runs the model again on all previous tokens. The KV cache trades GPU memory for this saved compute.
Diagram file: kv-cache.svg (open it with open kv-cache.svg on macOS, xdg-open kv-cache.svg on Linux). Orange = computed in this step. Blue = read from the KV cache.
A decoder-only transformer generates one token per step. In each attention layer, the model computes three vectors for each token $i$:
\[q_i = x_i W_Q, \qquad k_i = x_i W_K, \qquad v_i = x_i W_V\]Here $x_i$ is the vector of token $i$ at the input of that layer. $W_Q$, $W_K$, and $W_V$ are the weights of that layer.
The causal mask lets token $i$ attend only to tokens $1, \dots, i$. Thus $x_i$, $k_i$, and $v_i$ depend only on tokens $1, \dots, i$. A new token at a later position cannot change them. So the model can compute $k_i$ and $v_i$ one time and keep them.
The model does not keep the other vectors of old tokens:
Generation has two phases:
In decode step $t$, each attention layer does these operations (diagram, part B):
$K$ and $V$ are the cached matrices, with one row per token. $d_h$ is the head dimension. Each attention head does steps 3 and 4 separately, with its own part of $K$ and $V$.
The KV cache changes the work per step from all tokens to one token (diagram, part A). A forward pass costs about $2P$ FLOPs per token for the weight products. Here $P$ is the number of model parameters. For $n$ generated tokens (prompt not counted):
\[\text{without KV cache: } \sum_{t=1}^{n} 2P\,t \;\approx\; P n^2 \qquad\qquad \text{with KV cache: } 2Pn\]Thus the KV cache cuts this compute by a factor of about $n/2$.
Two limits apply:
Count the numbers in the KV cache. Then multiply by the bytes per number. Use these symbols:
| Symbol | Meaning |
|---|---|
| $L$ | number of layers |
| $n_{kv}$ | number of key/value heads (equal to the number of query heads in standard multi-head attention) |
| $d_h$ | head dimension |
| $T$ | tokens in one sequence (prompt + generated) |
| $B$ | number of sequences in the batch |
| $b$ | bytes per number (2 for fp16 or bf16, 1 for fp8) |
Do these steps:
The result is:
\[M_{\text{KV}} \;=\; \underbrace{2}_{K,\ V} \cdot \underbrace{L}_{\text{layers}} \cdot \underbrace{n_{kv}\, d_h}_{\text{width per layer}} \cdot \underbrace{T}_{\text{tokens}} \cdot \underbrace{B}_{\text{sequences}} \cdot \underbrace{b}_{\text{bytes}}\]In standard multi-head attention, $n_{kv} d_h = d_{\text{model}}$. Then each token needs $2\, L\, d_{\text{model}}\, b$ bytes.
Worked example: Llama 2 7B. Use $L = 32$, $n_{kv} = 32$, $d_h = 128$, and fp16 ($b = 2$).
Check yourself: Llama 3 8B. This model uses grouped-query attention: 32 query heads share $n_{kv} = 8$ KV heads. The other values are $L = 32$, $d_h = 128$, and bf16. Compute the memory per token. Then compute the memory for one sequence of 8192 tokens.
If you want, I can make an interactive page for this formula. It has one slider per symbol and compares the KV cache size with the weight size.