Lesson 8 of 14
Prefix caching
Thousands of requests to the same chatbot start with the same instructions, and a plain server rereads them from scratch for every one. This lesson shows when that work can be saved and reused, why the shared part must match token for token from the very first token, and how real servers find the match.
One system prompt, thousands of requests
The 3D view could not load. The three stages are described below.
01 · The rule
Only an exact start can be reused
Before Parrot writes a reply, it reads the whole prompt: every token (a token is a piece of a word, about three-quarters of one on average) runs through the model and leaves a card, the KV cache of lesson 4. This reading is called prefill. Every conversation opens with the same 2,000-token system prompt, so its cards come out the same every time. Prefix caching keeps them and hands them to the next prompt that starts the same way.
The catch is in the word “starts”. A card depends on its own token and on every token before it. So the server matches a new prompt from its first token and stops at the first difference. Nothing after that point can be reused, even words it has seen before.
The match runs from the first word
02 · The saving
About 7 times faster, not 21
With the system prompt cached, a customer’s prompt runs 100 tokens instead of 2,100. On one H100 the wait before Parrot’s first word drops from 30.8 ms to 4.59 ms.
Only one token in 21 is new, so why not 21 times faster? However few tokens it runs, the GPU still reads the whole model out of memory once (lesson 1), and that read is most of what is left. A busy server reads the model once for many requests at a time, which brings the saving close to the full ratio.
Time before the first token
Llama 3.1 8B on one NVIDIA H100, the fastest it could possibly go
03 · Chat
Every turn starts with the last one
Each new message resends the whole conversation, so turn 10 begins with turns 1 to 9, and the server already has their cards, its own answers included. Over 10 turns of 50-token messages and 250-token answers, it runs 2,500 tokens instead of 34,000.
A chat, one turn at a time
04 · Finding the match
A tree of everything cached
A server keeps its cached prompts in a tree. Prompts that start the same share a trunk and branch where they first differ, so finding the match is one walk down from the root. SGLang can stop the match at any token; vLLM stores cards in blocks of 16 tokens and reuses whole blocks only.
Sharing saves memory as well as time: in the opening scene, 16 conversations on one trunk free 3.93 GB of GPU memory for other conversations.
A server's radix tree, request by request
reused: its cards were on the shelf run for this request cached, not used
The cache is empty. Send a request.
05 · Playground
Lay out Parrot’s prompt
So Parrot needs a server with prefix caching (vLLM and SGLang both have one) and one habit: everything that is the same for every customer goes at the top, unchanged down to the character; the name, the time and the question go at the end. Move the timestamp to the top and watch the saving vanish.
Lay out Parrot's prompt
Llama 3.1 8B on one H100
Order of the parts, top first
- System prompt2,000 tokens, the same for everyone
- Timestamp10 tokens, different every request
- Customer name5 tokens, different every request
- Question95 tokens, different every request
- Reused per request
- Run per request
- Time before the first token
06 · Glossary
The vocabulary, in one place
- Token
- The unit a model reads and writes: a piece of a word, about three-quarters of one on average.
- Prefill
- Reading the prompt: running every prompt token through the model and writing its cards, before the first word of the reply.
- Cards (KV cache)
- What the model keeps for each token it has read, in every layer: a key (label) and a value (content). Lesson 4.
- System prompt
- The instructions every conversation starts with. Parrot's is 2,000 tokens.
- Prefix
- The opening of a prompt. A cached prefix is reusable only if the new prompt matches it token for token from the first token.
- Prefix caching
- Keeping the cards of prompts already read, and reusing them for any new prompt that starts the same way.
- Time to first token
- How long a user waits before the reply starts. Prefill takes most of it; the times here are the least it could be.
- Radix tree
- How a server stores its cached prompts: prompts that start the same share a trunk and branch where they first differ.
- Block
- A fixed group of tokens whose cards are stored together (16 in vLLM). A block cache reuses only whole blocks.
You can now
- Compute the prefill time a prefix-cache hit saves for given cached and new token counts, and explain why a single request with a short new suffix cannot go below one read of the model's weights while a batched server gets close to the token ratio.
- Explain why reusable KV must be a prefix that matches token for token from the first token, and why the same text in a different position or after a different start cannot be reused.
- Decide how to lay out a prompt template (stable content first; names, dates and questions last) so a radix-tree or block-hash cache actually hits, and predict how many tokens a 16-token block cache can reuse.
Open the workbook: the formulas, the exact numbers, graded exercises and the sources behind this page.