Module 7 · Lesson 3 of 4

The KV cache: saved work

Saved work, step by step

The KV cache saves the model's work on every token, so each new step computes only the newest token and reads the rest.

Scene 1 of 5 · Read the prompt once

Step 1 reads the 6-token question and saves its work on each token: this is the KV cache.

Writing "It left the depot", in tokens of work
StepWork, nothing savedWork, with the KV cache
Step 1: "It" (highlighted)??
Step 2: "left"??
Step 3: "the"??
Step 4: "depot"??
Total work, nothing saved
not shown yet
Total work, with the cache
not shown yet
Requests that fit in 8,000 tokens
not shown yet
  1. Read the prompt once. Step 1 reads the 6-token question and saves its work on each token: this is the KV cache.
  2. Nothing saved. With nothing saved, step 4 would redo all 9 tokens so far, to add only one more.
  3. With saved work. With the KV cache, each later step computes only the newest token and reads the rest from memory.
  4. The totals. For this 4-token answer: 30 tokens of work with nothing saved, and 9 with the cache.
  5. It takes memory. It sits in the memory of the GPU, the model's chip: 8,000 tokens hold 4 requests of 2,000.
  6. Now you try. Set the answer length, and compare the total work with and without saved work. For example: A 20-token answer: 310 tokens of work with nothing saved, 25 with the KV cache. Without saved work, each step redoes all the text so far, so the gap grows with every token.

Scene 1 of 5: Read the prompt once. Step 1 reads the 6-token question and saves its work on each token: this is the KV cache.

Key points

  1. Prefill reads the prompt once and saves its work on each token.
  2. With nothing saved, every step would redo all the text so far.
  3. With the KV cache, each later step computes one new token and reads the saved work.
  4. The saved work takes memory on the chip running the model, limiting how many requests fit.

Redoing all the earlier work at every step would be slow. Instead, the model saves its work on each as it reads or writes it. So a later step computes only the one new token. It still reads the saved work for every earlier token, and that reading grows as the text grows.

For each token, the model works out two lists of numbers, called a key and a value. A new token compares itself with the keys of earlier tokens to decide which ones matter, then mixes in their values. This step is called attention.

The keys and values are the saved work, so it is called the : K for key, V for value. It grows by one entry per token: one per token after , then one more after every step.

The KV lives in the memory of the , the chip that runs the model's sums. Bigger prompts and longer answers use more of that memory, one entry at a time.

Example. Illustrative: each request's KV cache holds 2,000 tokens. So 8,000 tokens of GPU memory fit only 4 requests at once, since 8,000 divided by 2,000 is 4.

In system design: Memory for the KV cache limits how many requests run together. With fixed GPU memory, longer prompts and answers mean fewer requests can run at once.

Check yourself

What does the KV cache save the model from doing?