Inference
Turn ten at the price of turn two
Prefix caching, and how a load balancer throws it away
A model keeps nothing between two messages. So a chat application sends the whole conversation again on every turn: the instructions, every question, every answer, and the new message at the end. The GPU has to read all of it before it can write a word, and that reading is prefill, the expensive pass that sets the wait for the first token. Left alone, a conversation gets slower with every turn. This lesson follows one chat through ten turns, shows the trick that keeps the wait flat, and then shows how an ordinary load balancer undoes it.
- The chat
- 1,200 tokens of instructions, then turns of about 240 tokens
- The model
- qwen3-32b on three identical workers
- The cost
- prefill: every prompt token is read before the first word
- The trick
- keep the work, find it again by the text that made it
Read the whole lesson as text
1. A chat API remembers nothing
The user asks a first question. The request holds the assistant's instructions, 1,200 tokens of them, and the question. The data plane sends it to gpu-2. Nothing about this is special yet. The point to hold on to is what the request contains, because the next one will contain all of it again. The API is stateless. The server does not know this user, and the application proves it by resending everything each time.
- The first message of a chat arrives. It carries the assistant's instructions and one question.
- The request goes to gpu-2.
2. Prefill once, then write
Go inside gpu-2. Prefill reads all 1,248 tokens in one pass and leaves its work in GPU memory: a key and a value for every token at every layer. That is the KV cache. Decode then writes the answer one token at a time, and for each new token it reads the cache and adds one entry. Without the cache every word would need the whole prompt read again, with a pause before each word. With it the pause is paid once. The answer here is 192 tokens long, so when it ends the cache covers 1,440 tokens.
- The GPU reads all 1,248 tokens of the prompt in one pass.
- Prefill's result is written into KV cache blocks so decode never has to read the prompt again.
- Each token of the answer added its own entry. The cache now covers the prompt and the answer.
3. Throw it away and pay again
Suppose the server frees those blocks the moment the answer ends. That is the simple thing to do, and the request is over. Now the user sends a second message. The application resends the thread: instructions, first question, first answer, new question. That is 1,488 tokens, and the GPU has never seen any of them as far as it knows. It reads them all. Turn three brings 1,728. By turn ten the prompt is 3,408 tokens, and the user waits for every one of them before the first word appears. Over ten turns the GPU prefills 23,280 tokens to answer ten short questions.
- With nothing set to keep them, the blocks are freed when the answer ends.
- The second turn carries the whole thread, and the GPU reads all of it from the start.
4. Keep the blocks, label them by their text
Now keep the blocks when the request ends, and give each one a label worked out from the exact tokens it covers and everything before them. When a request arrives the scheduler walks its prompt block by block and looks the labels up. For as long as they match, the work is already done. Turn two arrives with 1,488 tokens. The first 1,440 are the thread so far, and their blocks are still there. Only the new question has to be read. This is prefix caching. It works only from the start of a prompt, because a token's key and value depend on every token before it.
- The scheduler hashes the prompt in blocks of 16 tokens and finds the first 90 already in the cache.
- Prefill reads the 48 new tokens and nothing else.
5. Turn ten costs what turn two cost
Follow the chat to its tenth turn. The prompt is now 3,408 tokens. With the blocks kept, 3,360 of them are found and 48 are read. The wait before the first word is the same as on turn two. Over ten turns the GPU prefilled 1,680 tokens where it would have prefilled 23,280. The price is memory. The blocks of an idle conversation sit in the KV cache pool, and that pool is the same one that limits how many requests the GPU can run at once. Kept blocks are given up first when room is needed. So the saving lasts as long as the memory does.
- On the tenth turn the scheduler finds 210 blocks and has 3 new ones to fill.
6. One set of instructions, every user
Something else comes with this for free. The saved work for a stretch of text depends only on that text and what came before it. It has no idea whose conversation it belongs to. A second user opens the same assistant. Their request starts with the same 1,200 tokens of instructions, so the first 75 blocks are found, and only their own question is read. An assistant with a long system prompt, or an agent with a long list of tools, pays for that text once per worker and then never again. This is also why the same cache is a question for tenancy: it is shared by anything that sends the same text.
- Another user's first message arrives. It begins with the same instructions.
- The first 75 blocks match what the first user left behind, so they are used as they are.
7. Round robin sends turn three elsewhere
Back out to the factory, and put an ordinary load balancer in front of the workers. Its job is to spread requests evenly, one to each worker in turn. That has balanced web traffic for decades, because web servers are interchangeable. These workers are not. The first user's next turn comes in and the rotation points at gpu-3. Nothing of this conversation is there. gpu-3 reads the whole thread from the first token, while the blocks that would have saved the work sit unused on gpu-2. No error is raised and every health check passes.
- With round robin the next turn goes to the next worker, which has never seen this conversation.
8. Two requests that look the same
There is a second reason, and it has nothing to do with caches. Spreading requests evenly spreads work evenly only if requests are about the same size. A short greeting and a request to summarize fifty pages are both one POST to the same path. The balancer can't tell them apart. So the fifty pages can land on a worker that is streaming answers to thirty people, and its long prefill makes all thirty wait. Everything the balancer would need is inside the workers: how full each one's memory is, whose saved work sits where, and how long each queue is.
- The rotation sends a very long prompt to the worker that is already streaming to the most people.
9. A router that can see inside
The fix is a router that knows what the workers know. In the factory that is the endpoint picker. For each request it weighs three things: which worker already holds this request's opening text, how much memory each has free, and how long each queue is. The first user's next turn goes back to gpu-2 and prefill reads 48 tokens. The fifty pages go to the worker with the most room. The course this lesson is built from quotes about three times the throughput and a first response twice as fast for the open source llm-d project routing this way on the same hardware. The AI gateway picks the model. BNK picks the GPU.
- The data plane asks the endpoint picker before it sends the request anywhere.
- The turn goes to gpu-2, where all but the new question is already done.
Summary
A chat resends its whole thread on every turn, and the GPU must read a prompt before it can answer. If the work of reading is thrown away, a conversation gets slower with every message. Prefix caching keeps the KV cache blocks and finds them again by the text that produced them, so only new text is read: turn ten costs what turn two cost, and a shared system prompt is paid for once. All of that lives in one worker's memory. A load balancer that spreads requests evenly sends the next turn somewhere else and the saving is gone. It also can't tell a greeting from fifty pages. Serving a model well needs a router that sees prefixes, memory and queues.
Next lesson: Three numbers on a GPU card