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 modelLanguage modelChat appChat appYour screenYour screen"It"
  • Language model — healthy
  • Chat app — healthy
  • Your screen — healthy — "It"
What each run reads and writes
RunReadsWrites
Run 1 (highlighted)the question"It"
Runs so far
not shown yet
Time at the lab's pace
not shown yet
  1. Run 1: "It". Run 1 reads the question and writes one token, "It", which goes straight to your screen.
  2. Run 2: "left". Run 2 reads the question plus "It", then writes "left".
  3. Run 3: "the". Run 3 reads everything so far and adds "the"; your screen now shows three tokens.
  4. Run 4: "depot". Run 4 adds "depot": four tokens took four runs, one after another.
  5. Why text streams in. At the serving lab's 28 ms a run for one request, these four runs take about 112 ms.
  6. 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

  1. Each run of the model writes one token.
  2. Each run reads the question plus every token written so far.
  3. The runs happen one after another, never all at once.
  4. 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 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 . 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.

Example. The model answers "Where is my parcel?" with "It left the depot". It writes this as four steps, adding one token each time: "It", then " left", then " the", then " depot".

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 length.

Check yourself

Why do chat apps show an answer a few words at a time?

Two harder questions

The first uses the idea on a new case. The second asks you to judge a design.

Apply it

Without speculative decoding, how many runs through the model does a 50-token answer need?

Judge a design

A server turns on speculative decoding, and a team worries that its answers will change. Are they right?

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.

Open this lesson on its own page

2. Two stages: prefill and decode

The picture

How long is the prompt?

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, NOWApplicationsends the promptprompt: 18 tokensNOWLanguage model, on a GPULanguage modelon a GPU
  • Application — sends the prompt — healthy — NOW — prompt: 18 tokens
  • Language model — on a GPU — healthy
  • The prompt
  • One answer token
Step 1 of 9

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 tokens read
0
of 18
Answer tokens written
0
of 7
KV cache rows
0
one for each token so far
Time used
0units
made-up units, not milliseconds

Prompt:

Answer so far:

Time so far, in illustrative time units (not real milliseconds):

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 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.

Example. A 1,000-token prompt is read in one prefill stage, its tokens handled side by side. Writing a 100-token answer still takes 100 separate decode steps, one new token at a time.

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

A prompt doubles in length, and the answer stays at 100 tokens. What grows?

Two harder questions

The first uses the idea on a new case. The second asks you to judge a design.

Apply it

Request A has a 500-token prompt and a 200-token answer. Request B has a 2,000-token prompt and a 50-token answer. Which needs more decode steps?

Judge a design

A team's chat feels slow, and it plans to shorten only the prompts. Answers average 600 tokens. What does that plan miss?

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.

Open this lesson on its own page

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.

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 the lab's 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. On the serving lab's small example GPU, 8,000 tokens of memory 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 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 : 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 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. The serving lab's small example GPU has room for 8,000 tokens, so only 4 requests fit at once: 8,000 divided by 2,000 is 4. A real GPU holds far more, and the lesson works one out.

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 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

What does the KV cache save the model from doing?

Two harder questions

The first uses the idea on a new case. The second asks you to judge a design.

Apply it

A GPU has room for 12,000 tokens of KV cache, and each request holds 3,000. How many requests fit at once?

Judge a design

A team says the KV cache makes every new token cost the same work, however long the text gets. What is wrong?

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.

Open this lesson on its own page

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.

One request, from sending to answer

Request

  1. Request sent at 0 s, sent
  2. 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
    1. Wait in the queue. The request waits 0.8 s in a busy queue before the GPU can start on it.
    2. Read the prompt. Prefill then reads the 3,000-token prompt in one pass, which takes about 0.12 s.
    3. The first token. One 49 ms step later, the first token arrives at 0.97 s: the time to first token.
    4. Then a steady stream. After that, a token arrives every 49 ms, using the saved work, so token 21 lands at 1.95 s.
    5. 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

    1. The first token waits for the queue, then for prefill.
    2. This wait is the time to first token.
    3. Later tokens each wait for one short step only.
    4. 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. is the wait for the first piece of the answer. Tokens per second is the speed of every token after that.

    Example. Illustrative: when the queue is busy, a long prompt's first token can take about a second, for queueing plus prefill together. After that, tokens can arrive at roughly 20 a second.

    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 , 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

    Users wait 2 s for the first word, then the text streams in fast. What is most likely slow?

    Two harder questions

    The first uses the idea on a new case. The second asks you to judge a design.

    Apply it

    A request spends 400 ms on search and reranking, 300 ms in the queue, and 500 ms in prefill. What is its time to first token?

    Judge a design

    A dashboard shows only the average time to first token, 0.6 s, yet users say starts are often slow. What should the team add?

    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.

    Open this lesson on its own page