Sub topic 1
How a Model Produces Text One Token at a Time
Prerequisites
This chapter assumes you know basic HTTP request and response concepts, can read short Python snippets, and know that an LLM API takes text in and returns text out. You should also know that a token is a small chunk of text, often a word or part of a word. No machine learning background is needed.
Generation happens in a loop
A language model does not write a whole answer in one step. It predicts one token at a time, and each new token depends on the prompt and on every token produced so far. This is called autoregressive generation.
Each pass through the loop works like this:
- The model reads the prompt plus all tokens generated so far.
- It produces a probability for every token in its vocabulary.
- A sampling rule picks one token, for example the most likely one or a random choice weighted by probability.
- That token is appended to the text, and the loop repeats.
- The loop stops when the model emits a special end-of-sequence token or reaches a length limit.
A toy version of the loop
The sketch below replaces the model with a fixed lookup table. The loop has the same shape as real generation: choose a token, append it, and continue until an end token appears.
import time
NEXT_TOKEN = {
"<start>": "Streaming",
"Streaming": " sends",
" sends": " tokens",
" tokens": " early",
" early": ".",
".": "<end>",
}
def generate():
token = "<start>"
while True:
token = NEXT_TOKEN[token]
if token == "<end>":
return
time.sleep(0.2) # stands in for the time one model step takes
yield token
for piece in generate():
print(piece, end="", flush=True)
print()The function is a generator, so each token is available the moment it is chosen. Streaming APIs rely on the same property. The server can send each token as soon as the loop produces it.
What this means for latency
Each step takes a roughly fixed amount of time, so total generation time grows with the length of the answer. A 500-token answer takes about ten times as long as a 50-token answer.
The loop also explains why the first token is not instant. Before step one, the model must process the whole prompt. Long prompts make that first step slower. Once generation starts, tokens arrive at a steady rate.
Without streaming, the server waits for the loop to finish and then sends everything at once. The user stares at a blank screen for the full generation time. With streaming, the server sends each token as it appears. The total time is the same, but the user sees text after only the first step.
| Approach | When the user first sees text | When the answer is complete |
|---|---|---|
| No streaming | After the last token | After the last token |
| Streaming | After the first token | After the last token |
Common misunderstandings
- Streaming does not make the model faster. It only exposes the tokens earlier.
- Tokens are not always whole words. A single word may arrive in several pieces, and a piece may end mid-word or mid-character, which matters when you parse and display output later.
- You cannot know the final text until the loop ends. A stream is a partial view of an answer that is still being written.
Exercise
A model generates 200 tokens at 50 tokens per second, and processing the prompt takes 0.5 seconds. Roughly when does the user see the first token and the last token with and without streaming?
Show answer
With streaming, the first token appears at about 0.5 seconds plus one token step (0.02 s), so around 0.52 s, and the last token at about 0.5 + 4 = 4.5 s. Without streaming, the user sees nothing until about 4.5 s, when the whole answer arrives at once. The total time is the same. Streaming only moves the first visible text earlier.