AlgoViz

KV cache and prefix caching

medium 40 min

While 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

  1. You
  2. help
  3. a
  4. bike
  5. shop
computed now = 5

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

  1. b0
  2. b1
  3. ...
  4. b374
  5. ask
hit rate = high

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

    critical

    With grouped-query attention the cache has far fewer heads than the queries. Qwen3-8B would come out 4 times too big.

    Wrong
    2 * layers * num_attention_heads * head * 2
    Right
    2 * layers * num_key_value_heads * head * 2
  • Something volatile at the start of the prompt

    critical

    One different token in block 0 changes every block hash after it, so the hit rate drops to zero.

    Wrong
    prompt = f"Time: {now}\n" + SYSTEM + user
    Right
    prompt = SYSTEM + user + f"\nTime: {now}"
  • Planning memory for the weights only

    major

    The cache of all running requests often needs more memory than the weights. The server runs out under load.

    Wrong
    fits = weights_gb < gpu_gb
    Right
    fits = weights_gb + work_gb + users * kv_per_request_gb < gpu_gb
  • Rewriting earlier history in an agent

    major

    Editing or reordering old turns breaks the prefix, so every turn pays full price again.

    Wrong
    history = summarise(history) + [new]
    Right
    history.append(new)
  • One shared cache across tenants

    major

    Hits are faster than misses, and that timing can reveal what another tenant sent.

    Wrong
    hashes = block_hashes(tokens)
    Right
    hashes = 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).