Module 7 · 4 lessons
How a model writes, one token at a time
Why answers appear a few words at a time, why the first word is slow, and what the model keeps in memory while it writes.
Before you start
From this track: Modules 0 to 6. Each module uses only ideas from the ones before it.
Builds on: Latency, throughput and availability
Already know these? Start with the first lesson below.
Later, to go deeper: LLM serving and retrieval (RAG)
1. One token at a time
The picture
Four tokens, four runs
A model writes an answer by running once per token, each run reading everything so far and adding one piece.
Scene 1 of 5 · Run 1: "It"
Run 1 reads the question and writes one token, "It", which goes straight to your screen.
- Language model — healthy
- Chat app — healthy
- Your screen — healthy — "It"
| Run | Reads | Writes |
|---|---|---|
| Run 1 (highlighted) | the question | "It" |
- Runs so far
- not shown yet
- Time at the lab's pace
- not shown yet
- Run 1: "It". Run 1 reads the question and writes one token, "It", which goes straight to your screen.
- Run 2: "left". Run 2 reads the question plus "It", then writes "left".
- Run 3: "the". Run 3 reads everything so far and adds "the"; your screen now shows three tokens.
- Run 4: "depot". Run 4 adds "depot": four tokens took four runs, one after another.
- Why text streams in. At the serving lab's 28 ms a run for one request, these four runs take about 112 ms.
- Now you try. Set the answer length, and see how many runs it takes. For example: 10 tokens take 10 runs, about 280 ms one after another. Each run needs the token before it, so the runs cannot happen at the same time.
Scene 1 of 5: Run 1: "It". Run 1 reads the question and writes one token, "It", which goes straight to your screen.
Key points
- Each run of the model writes one token.
- Each run reads the question plus every token written so far.
- The runs happen one after another, never all at once.
- Apps show each token the moment it is written, so text streams in.
A model does not write a whole answer in one go. It picks one (A piece of text that a language model reads or writes, often part of a word. Prices and limits are counted in tokens.), a small piece of text (see AI basics), and adds it to everything written so far. Then it runs again to pick the next token.
Each run looks back over all the text so far, and produces one new token. Lesson 3 shows how it avoids redoing that earlier work. A ten-token answer still needs ten separate runs through the model, one after another, not a single long write.
This is why an answer streams in a piece at a time on screen. The app shows each token the moment the model produces it, instead of waiting for the whole answer to finish.
Many servers now speed this up with speculative (The second stage of an answer, where the model writes one new token per step, using the work it saved.). A small, fast model drafts a few tokens, and the large model checks them all in one run. It keeps the drafts it would have chosen itself, so one run can add several tokens. The method is built so that only the speed changes, not what the large model writes.
In system design: Streaming an answer means sending each token to the user as it is produced, instead of waiting for the whole answer. This is why chat apps show text appearing gradually.
Where it goes wrong
- A long answer is slow even on a fast server, because each token needs its own run. Ask for shorter answers where you can.
- A stream can stop halfway. The app must handle a partial answer and show that it ended early.
In an interview
- Explain why answers stream, and why answer length drives the wait more than (Everything sent to a language model in one request: instructions, context and the question.) length.
Check yourself
Two harder questions
The first uses the idea on a new case. The second asks you to judge a design.
Apply it
Judge a design
Sources and further reading (2)
First-hand sources to check this lesson against and to go further. They are standards, official docs, original papers, and guides from engineering and security teams. Each link was opened on 8 November 2026.
- Research paperAttention Is All You Need (2017)Vaswani et al., NeurIPSThe transformer writes its output one token at a time, each step using the tokens before it.
- Engineering guideHow to generate textHugging Face blogThe generation loop: score every token, pick one, add it, repeat.
2. Two stages: prefill and decode
The picture
18 prompt tokens: prefill takes 72 units. The first token arrives after 97 units, and each later token after 25 more.
- Application — sends the prompt — healthy — NOW — prompt: 18 tokens
- Language model — on a GPU — healthy
- The prompt
- One answer token
Step 1 of 9 · Application
Done: The application has the prompt ready
- What happens
- The prompt is 18 tokens long. A token is a word or a piece of a word.
- Why
- The model has not seen the prompt yet, so it has written nothing.
- What changes
- Nothing is saved, and no time has been used.
- Next
- Prefill: the model reads the whole prompt
Event log
8 more to go. Press a line to go back to that step.
Prompt:
Answer so far:
Time so far: prefill 4 units for each of 18 prompt tokens, then 25 units per answer token. 0 of 247 units used so far.
KV cache rows: 0
Step 1 of 9: Application. The application has the prompt ready. The prompt is 18 tokens long. A token is a word or a piece of a word.
What this simplifies. Real models do this for thousands of tokens, with huge sums run on a GPU. The rows here stand for much larger blocks of numbers, and the time units are made up. Here every decode step takes the same time; in a real model it grows a little as the KV cache grows.
The full prompt for this example: You are a support agent. Answer from the policy. Where is my parcel?
Writing an answer has two stages. The first is (The first stage of an answer, where the model reads the whole prompt in one go and saves its work for later steps.): the model reads the whole prompt, all its tokens side by side, and saves its work for the next steps.
The second stage is decode: the model writes one new token per step, using the work it saved. Prefill handles many tokens together; decode handles one token per request, per step.
This split matters for speed. A longer prompt still makes prefill take longer, since there is more to read, but it does not add one step per prompt token. Decode adds one step per answer token, run one after another.
In system design: Long prompts slow prefill; long answers add decode steps. A design that only shortens prompts misses half the cost, since decode time grows with answer length too.
Where it goes wrong
- A very long prompt makes the first token slow, so users wait before they see anything.
- On a busy server, one long prefill can delay other users' decode steps, unless the server splits long prompts into smaller pieces.
In an interview
- Name the two stages and what each costs, then say which one your feature stresses: long prompts or long answers.
Check yourself
Two harder questions
The first uses the idea on a new case. The second asks you to judge a design.
Apply it
Judge a design
Sources and further reading (2)
First-hand sources to check this lesson against and to go further. They are standards, official docs, original papers, and guides from engineering and security teams. Each link was opened on 8 November 2026.
- Research paperDistServe: Disaggregating Prefill and Decoding for Goodput-optimized LLM Serving (2024)Zhong et al., USENIX OSDIWhy reading the prompt and writing tokens use the GPU so differently that some systems split them.
- Research paperTaming Throughput-Latency Tradeoff in LLM Inference with Sarathi-Serve (2024)Agrawal et al., USENIX OSDIHow long prefills stall the decode steps of other requests, and one way to fix it.
3. The KV cache: saved work
The picture
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.
| Step | Work, nothing saved | Work, 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 the lab's 8,000 tokens
- not shown yet
- Read the prompt once. Step 1 reads the 6-token question and saves its work on each token: this is the KV cache.
- Nothing saved. With nothing saved, step 4 would redo all 9 tokens so far, to add only one more.
- With saved work. With the KV cache, each later step computes only the newest token and reads the rest from memory.
- The totals. For this 4-token answer: 30 tokens of work with nothing saved, and 9 with the cache.
- It takes memory. On the serving lab's small example GPU, 8,000 tokens of memory hold 4 requests of 2,000.
- 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
- Prefill reads the prompt once and saves its work on each token.
- With nothing saved, every step would redo all the text so far.
- With the KV cache, each later step computes one new token and reads the saved work.
- 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 token 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 (The memory where a model keeps its work on every token so far, so each new token does not redo it. It grows by one token per step.): K for key, V for value. It grows by one entry per token: one per prompt token after prefill, then one more after every decode step.
The KV (A small, fast store that keeps copies of popular data, so most reads skip the slower database.) lives in the memory of the (A graphics processing unit: a chip that does many simple sums at the same time. Language models run on GPUs because they need huge numbers of sums.), the chip that runs the model's sums. Bigger prompts and longer answers use more of that memory, one entry at a time.
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.
Where it goes wrong
- Long prompts and answers fill GPU memory, so fewer requests run together and the (A waiting line for work. One part adds messages, and another takes them off and handles them later, at its own pace.) grows.
- A request paused to free memory must read its prompt again later, which adds delay.
In an interview
- Explain what the KV cache saves, what it costs in memory, and how that limits requests per GPU.
Check yourself
Two harder questions
The first uses the idea on a new case. The second asks you to judge a design.
Apply it
Judge a design
Sources and further reading (2)
First-hand sources to check this lesson against and to go further. They are standards, official docs, original papers, and guides from engineering and security teams. Each link was opened on 8 November 2026.
- Research paperEfficient Memory Management for Large Language Model Serving with PagedAttention (2023)Kwon et al., SOSPHow big the KV cache gets, and storing it in pages so less memory is wasted.
- Official docsCachingHugging Face Transformers docsWhat the KV cache stores and why it saves recomputing earlier tokens.
4. Why the first token is slow
The picture
A slow start, then a stream
The first token waits for the queue and for the whole prompt to be read; every token after it comes at a steady pace.
Scene 1 of 4 · Wait in the queue
The request waits 0.8 s in a busy queue before the GPU can start on it.
Request
- Request sent at 0 s, sent
- Prompt read starts at 0.8 s, reading starts
Answer
- Time to first token
- not shown yet
- Tokens a second after that
- not shown yet
- Wait in the queue. The request waits 0.8 s in a busy queue before the GPU can start on it.
- Read the prompt. Prefill then reads the 3,000-token prompt in one pass, which takes about 0.12 s.
- The first token. One 49 ms step later, the first token arrives at 0.97 s: the time to first token.
- Then a steady stream. After that, a token arrives every 49 ms, using the saved work, so token 21 lands at 1.95 s.
- Now you try. Change how long the request waits in the queue. For example: First token at 1.77 s; token 21 at 2.75 s, about 20 tokens a second either way. The queue only delays the start; once writing begins, tokens arrive at the same steady pace.
Scene 1 of 4: Wait in the queue. The request waits 0.8 s in a busy queue before the GPU can start on it.
Key points
- The first token waits for the queue, then for prefill.
- This wait is the time to first token.
- Later tokens each wait for one short step only.
- A busy queue delays the start, not the pace after it.
The first token of an answer is often the slowest. It waits in the queue behind other requests, then waits for prefill, the pass that reads the whole prompt before any token is written.
Every later token is much faster. Each one only waits for a single decode step, since the prompt was already read and its saved work is already in the KV cache.
Two numbers describe this. (How long a user waits from sending a request until the first piece of the answer appears.) is the wait for the first piece of the answer. Tokens per second is the speed of every token after that.
In system design: Time to first token and tokens per second are the two numbers users feel. One is the first wait; the other is how fast the rest streams in.
Where it goes wrong
- An average hides slow starts. Track the slowest 1 in 20 or 1 in 100 first tokens, not only the average.
- Work done before the model call, such as search and (A slower, more careful model that rescores the top results of a first search, so the best few go into the prompt.), adds to the first wait as well.
In an interview
- Split time to first token into parts, your own steps, the queue and prefill, and say which one you would cut.
Check yourself
Two harder questions
The first uses the idea on a new case. The second asks you to judge a design.
Apply it
Judge a design
Sources and further reading (2)
First-hand sources to check this lesson against and to go further. They are standards, official docs, original papers, and guides from engineering and security teams. Each link was opened on 8 November 2026.
- Research paperDistServe: Disaggregating Prefill and Decoding for Goodput-optimized LLM Serving (2024)Zhong et al., USENIX OSDITime to first token and time per output token, measured as separate targets.
- Official docsReducing latencyAnthropic docs (one provider)Ways to get the first words sooner: shorter prompts, smaller models, streaming.