KV cache and prefix caching
medium 40 minWhile a model writes, it saves the key and value vectors of every token it has already seen, so each new token costs one small step instead of a pass over the whole text.
See it run
Watch one chat fill its cache, then watch a second chat reuse the start of the first.
Operations
Without a cache
Every step recomputes keys and values for every earlier token.
Step 1 of 4
Step 6
Tokens
- You
- help
- a
- bike
- shop
To write after five tokens, the model computes keys and values for all five.
Step 1: Step 6
for t in range(len(prompt), len(prompt) + new): K = [Wk @ x for x in xs] # all of them, again V = [Wv @ x for x in xs] out = attend(Wq @ xs[-1], K, V) xs.append(next_input(out))- Work per step
- grows with t each step
- Memory
- nothing saved
- Projections, 32 + 224 tokens
- about 32,000
The waste is the recomputing, not the reading. Old keys and values come out exactly the same every time.
In the real world
The same kv cache and prefix caching doing real jobs. Pick one and step through it.
Max adds the time as the first line of the system prompt.
Step 1 of 4
Before
Blocks of 16 tokens
- b0
- b1
- ...
- b374
- ask
Every request starts with the same 375 blocks. Hit rate is high.
Step 1: Before
Common mistakes
The bugs interviewers watch for, with the fix beside each.
Using query heads in the size formula
criticalWith grouped-query attention the cache has far fewer heads than the queries. Qwen3-8B would come out 4 times too big.
Wrong2 * layers * num_attention_heads * head * 2Right2 * layers * num_key_value_heads * head * 2Something volatile at the start of the prompt
criticalOne different token in block 0 changes every block hash after it, so the hit rate drops to zero.
Wrongprompt = f"Time: {now}\n" + SYSTEM + userRightprompt = SYSTEM + user + f"\nTime: {now}"Planning memory for the weights only
majorThe cache of all running requests often needs more memory than the weights. The server runs out under load.
Wrongfits = weights_gb < gpu_gbRightfits = weights_gb + work_gb + users * kv_per_request_gb < gpu_gbRewriting earlier history in an agent
majorEditing or reordering old turns breaks the prefix, so every turn pays full price again.
Wronghistory = summarise(history) + [new]Righthistory.append(new)One shared cache across tenants
majorHits are faster than misses, and that timing can reveal what another tenant sent.
Wronghashes = block_hashes(tokens)Righthashes = block_hashes(tokens, salt=tenant_id)
Interview tips
How to explain it
- Old keys and values never change.
- Prefill writes, decode reads.
- Blocks and a block table.
- Same start, same blocks.
Memory trick
- Same prefix, same blocks, free prefill.
- Two for K and V, then layers, heads, size, bytes.
- Stable first, volatile last.
Always say
- Cache per request times concurrency.
- Prefix caching does not speed up decode.
- Route to the replica with the prefix.
- Isolate tenants.
Numbers to know
- Qwen3-8B: 144 KiB per token.
- Qwen2.5-72B: 2.5 GiB per 8k chat.
- H100 SXM: 80 GB, 3.35 TB/s.
- Provider reads: often 0.1x input (27 Nov 2026).