Inference
Prefill and decode
What a GPU does with a prompt, and what limits it
A GPU is a small computer of its own: thousands of cores and a block of very fast memory, joined to the rest of the server by a much slower bus. This lesson follows a model into that memory and then follows one prompt through it. You will see the two phases every request has. Prefill reads the whole prompt in one pass and decides how long the user waits for the first word. Decode writes the answer one token per pass and decides how fast the words come. They are limited by different things, and neither limit is the one that decides how many users a GPU can serve. That one is memory.
- GPU
- H100 · 80 GB of HBM · about 3.35 TB/s
- Model
- Qwen3-32B in 8-bit · about 32 GB of weights
- Prefill
- many tokens in one pass · limited by compute
- Decode
- one token per pass · limited by memory bandwidth
Read the whole lesson as text
1. Fetch the weights
A model is two things: a formula, which is the same for most models, and the weights, billions of learned numbers in a file. Qwen3-32B has 32 billion of them. Stored in 8 bits each, that is about 32 GB. When the vLLM Pod on gpu-2 starts, it has none of it. It downloads the weights from object storage, as several large files. The requests go to BIG-IP, which stands in front of the storage nodes. It ends the TLS session, so the storage nodes spend their CPU on storage, and it picks a node for each request.
- The new vLLM Pod on gpu-2 asks object storage for the first file of weights.
- BIG-IP ends the TLS session and passes the request to one of the storage nodes.
- The seven files stream back through BIG-IP and land on gpu-2's local disk.
2. Into GPU memory
Zoom into gpu-2. The weights are on the local disk and the GPU can't use them there. Its cores can only work on what is in the GPU's own memory, called HBM. So the weights are read from disk into the server's memory and copied across the PCIe bus into HBM. This is the only time they cross that bus. Then vLLM does something that surprises people: it sets aside almost all the memory that is left for the KV cache and cuts it into blocks. An idle vLLM server can show a GPU that looks nearly full. That is by design.
- The weights are read from the local disk into the server's ordinary memory.
- From there they are copied across the PCIe bus into the GPU's own memory.
- vLLM claims nearly all the memory that is left and cuts it into blocks for the KV cache.
3. A prompt arrives
The worker is ready and a first request comes in: a prompt of 2,000 tokens that asks for an answer of up to 300. The API server turns the text into token numbers. The scheduler gives the request blocks from the KV cache pool, enough for the whole prompt, and puts it in the batch. Nothing has been computed yet. The blocks are reserved space, about 520 MB of it, waiting for the prefill pass to fill them.
- A request arrives at 10.244.2.22:8000. The server turns its text into 2,000 tokens and queues it.
- The scheduler takes it straight out of the queue, because the GPU is idle.
- It looks for the prompt in the cache, finds nothing, and reserves 125 empty blocks for it.
4. Prefill
Now the GPU reads the prompt. All 2,000 tokens go through the model together, in one pass. In every layer, each token looks at all the tokens before it. For each token and each layer the model works out a key and a value, the two things later tokens will need from it, and stores them in the KV cache. This is prefill. It reads the weights once and does an enormous amount of arithmetic with them, so it is limited by how fast the cores can compute. The longer the prompt, the longer it takes. Its last output is the first token of the answer, which is why prefill sets the time to first token.
- The scheduler sends all 2,000 tokens through the model in a single pass.
- The cores read all 32 GB of weights once and use each of them 2,000 times over.
- For every token and every layer, a key and a value go into the reserved blocks.
- The pass ends with the first token of the answer, and the server streams it out.
5. Decode
The rest of the answer comes one token at a time. Each pass takes the single newest token, runs it through all 64 layers, looks back at the KV cache for everything before it, and produces the next token. Then that token goes in and it starts again. A 300 token answer is 300 passes. Each pass still reads all 32 GB of weights, but now it does very little arithmetic with them: one token's worth. So the cores mostly wait for memory. Decode is limited by how fast the GPU can pull the weights out of its memory, and that sets the time between tokens.
- Now each pass carries one token: the one that was just produced.
- The pass reads all 32 GB of weights again, to do one token's worth of arithmetic.
- The new token looks back at the 2,000 cached ones, then adds its own key and value to the cache.
- The next token is ready 12 ms after the first, and the server streams it at once.
6. More users, same pass
Two more requests arrive while the first is still decoding. They don't wait for it to finish. Between two passes the scheduler adds them to the batch. Now look at what a pass costs. Its expensive part is reading 32 GB of weights, and that is the same for one request or three. So the next decode pass produces three tokens, one for each user, in almost the time it took to produce one. This is why a GPU can serve dozens of people at once. Each of them gets tokens a little slower. Together they get far more.
- Two more requests arrive while the first one is still being answered.
- At the next gap between passes, the scheduler gives both their blocks and adds them to the batch.
- Once the two newcomers are prefilled, a single decode pass makes a token for all three users.
7. A long prompt stalls the others
There is a catch. Prefill and decode share one set of cores, so they take turns. A fourth request arrives with a very long prompt: 30,000 tokens, a whole document. Its prefill needs the cores for a long stretch. While it runs, the three users who were getting a token every 13 ms see their text stop. Nothing is broken. The GPU is busy reading someone else's document. The size of the request that lands next to you decides your speed, and nobody can see that size from outside without counting tokens.
- A request with a 30,000 token prompt arrives and is added to the batch.
- Its prefill takes the cores for several seconds.
- Meanwhile the three other users wait far longer than 13 ms for their next token.
8. Memory is the ceiling
Keep adding users and something runs out. It isn't compute: during decode most cores are idle. It is the KV cache pool. Every request in the batch holds blocks for its whole prompt and for every token it has written so far, and the pool holds about 145,000 tokens in all. When it is full, the scheduler can't give the next request any blocks. That request stays in the waiting queue until someone else finishes and frees theirs. This is what at capacity means for a GPU: not busy cores, but no free memory. It is why the two numbers a router most needs from a worker are how full its KV cache is and how long its queue is.
- Many requests later, almost every block in the pool is in use.
- The next requests get no blocks. They wait in the queue and their users see nothing yet.
- The worker publishes both numbers on its metrics page, where a router can read them.
Summary
A GPU has three numbers and each one limits something else. Compute limits prefill, the one pass that reads the whole prompt, so it sets the wait for the first token. Memory bandwidth limits decode, where every pass rereads all the weights to write a single token, so it sets how fast the words come. Memory capacity limits how many requests fit at once, because each one needs its own KV cache next to the weights. Batching makes one pass serve many users, which is where throughput comes from, and a long prefill makes them all wait, which is where the trouble comes from. A worker that is full shows it in two numbers: its KV cache use and its queue.
Next lesson: Turn ten at the price of turn two