All courses

Generating text one token at a time

To answer a prompt, the model reads all tokens so far and outputs a probability for each possible next token. It then picks one, either the most likely or a random draw whose randomness is set by settings like temperature. That token is appended and the whole step repeats until a stop token. Every chat reply is produced this way, strictly one token at a time, so each token costs a model pass and a token, once chosen, is never revised.

▶ Watch this lesson Start the whole course

What the animation says

One narrow question

At heart, an LLM answers one narrow question: given this text, what token comes next? Yet it writes whole paragraphs, code, and chat replies. Long replies also take longer and cost more. And a model can state something wrong, then correct itself a sentence later, instead of fixing the original. One mechanism explains all three. Here the prompt is 'the cat', and the model has just picked 'sat'. So what happens next?

Read, predict, pick, append

It's appended. 'The cat sat' goes back in, and the model runs again. Each pass reads all the text so far: the prompt, plus everything it has already written. It outputs a probability for every token in its vocabulary. Here that's 'on' at point seven, stop at point three, and everything else zero. A rule picks one token. The simplest rule takes the most likely one. That token is appended, and the loop repeats until the model picks a special stop token. Your turn: drag the token this rule picks to where it goes.

Temperature shapes the draw

Instead of always taking the favourite, the rule can make a random draw, shaped by a setting called temperature. Back to the first step: sat at point six, ran at point three, stop at point one. At temperature one half, square each one: point three six, point oh nine, point oh one. Divide by their sum, and you get point seven eight, point two, and point oh two. Low temperature sharpens. The favourite gets even likelier, and near zero it's just the most-likely rule. At temperature two, take square roots instead. That gives point four seven, point three three, and point one nine. Flatter, so surprises happen more often.

One pass per new token

Whatever the rule, every new token needs its own pass through the model. The prompt is 'the cat'. Pass one picks 'sat'. Pass two reads 'the cat sat' and picks 'on'. Pass three reads 'the cat sat on' and picks stop. The loop ends. The reply was 'sat on', then stop. How many model passes did generating it take?

Chosen tokens are permanent

Three. One pass per generated token, counting the stop token, and each pass reads all the text so far. So a reply twice as long takes about twice as many passes. That means more time and more cost. Now look at this line in the loop: the text is only ever appended to. A chosen token is never revised. So if the model writes something wrong, it can't go back. It can only add words after it, like 'actually, I meant…' Read everything, predict, pick, append, repeat. That's how a next-token predictor writes.

How does a next-token predictor write essays?

At heart, an LLM answers one narrow question: *given this text, what token comes next?* Yet it writes whole paragraphs, code and chat replies. How does a one-token answer become a long reply? Why does a long reply take longer and cost more? And why can a model state something wrong and then correct itself in the next sentence instead of fixing the original? One mechanism explains all three.

Read, predict, pick, append, repeat

The model reads **all tokens so far**, meaning the prompt plus everything it has written. It outputs a probability for **every** token in its vocabulary. A rule picks one token. The rule can take the most likely one, or make a random draw that a setting like temperature shapes. That token is appended to the text and the loop runs again. It stops when the model picks a special **stop token**. A chosen token is never revised. Later tokens can only follow it.

The generation loop, precisely

Write $x_{\le t}$ for all tokens up to position $t$, and $\theta$ for the model's learned numbers. At step $t$ the model gives $p_\theta(\cdot\mid x_{\le t})$: a probability $p_i$ for each of the $V$ tokens $i$. Pick $x_{t+1}$ in one of two ways: - most likely: $x_{t+1}=\arg\max_i p_i$ - random draw from $q_i = \dfrac{p_i^{1/\tau}}{\sum_j p_j^{1/\tau}}$ $\tau=1$ uses the model's own probabilities. $\tau<1$ sharpens them, and as $\tau\to0$ the draw becomes the most-likely rule. $\tau>1$ flattens them. Append the token, then repeat until the stop token or a length limit. Each new token costs one model pass.

Two steps, and what temperature does

Toy vocabulary: the, cat, sat, ran, on, ⟨stop⟩. Any token not listed below gets probability 0. Prompt: "the cat". The model gives sat 0.6, ran 0.3, ⟨stop⟩ 0.1. The most-likely rule picks **sat**. The next pass reads "the cat sat" and gives on 0.7, ⟨stop⟩ 0.3, so it picks **on**. Random draw at $\tau=0.5$ on step 1: square each probability to get $0.36, 0.09, 0.01$, sum $0.46$, so $q = 0.78, 0.20, 0.02$. At $\tau=2$: take square roots to get $0.775, 0.548, 0.316$, sum $1.639$, so $q = 0.47, 0.33, 0.19$.

A toy generation loop

In this toy, a fixed table stands in for the model and looks only at the last token. A real LLM's pass reads **all** of `ids` every step. The loop structure is otherwise identical. Note that `ids` only ever grows: nothing already chosen is changed.
import numpy as np
vocab = ["the", "cat", "sat", "<stop>"]
P = np.array([[.05,.8,.1,.05],[.05,.05,.8,.1],[.3,.1,.1,.5],[.25,.25,.25,.25]])
def generate(ids, tau=1.0, rng=np.random.default_rng(0)):
    while vocab[ids[-1]] != "<stop>" and len(ids) < 20:
        p = P[ids[-1]]                 # real model: one pass over all ids
        q = p ** (1 / tau); q /= q.sum()
        ids = ids + [rng.choice(len(vocab), p=q)]  # append, never revise
    return [vocab[i] for i in ids]
print(generate([0], tau=0.5))

Remember this

Every reply is built **one token at a time**. The model reads all text so far, gives a probability for each possible next token, and one token is picked, either the most likely or a temperature-shaped random draw. That token is appended, and the loop repeats until a stop token. So each token costs one model pass, and a chosen token is permanent.

On Bytewise this lesson comes in small steps with a check at the end, and the app brings it back right before you would forget it.