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.
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.
Read, predict, pick, append, repeat
The generation loop, precisely
Two steps, and what temperature does
A toy generation loop
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
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.
Bytewise
All courses