Papa Programmer
Lesson 05 of 20

How a model writes a reply

8 min read

Optional

The generation loop

Writing a reply is a loop around Next-token predictionThe one task an LLM is trained on: given the text so far, give a probability to every token that could come next. A reply is written by repeating this prediction one token at a time.. The program runs it, and the model takes part in one step of each pass.

  1. ReadThe model reads the prompt and every token written so far.
  2. ScoreIt gives every token in its vocabulary a probability.
  3. PickThe program chooses one token from those probabilities.
  4. AppendThe program adds the token to the text and starts the next pass.

Then the cycle starts again at Read

One pass of the loop adds one token to the reply

The program below has the same shape. A small table stands in for the model, so you can run it and read every step.

python
import random

next_tokens = {
	"Once": {"upon": 1.0},
	"upon": {"a": 1.0},
	"a": {"time": 0.7, "hill": 0.3},
	"time": {"<end>": 1.0},
	"hill": {"<end>": 1.0},
}

def generate(prompt, max_new_tokens=10, seed=None):
	rng = random.Random(seed)
	tokens = prompt.split()
	for _ in range(max_new_tokens):
		options = next_tokens[tokens[-1]]
		token = rng.choices(list(options), weights=list(options.values()))[0]
		if token == "<end>":
			break
		tokens.append(token)
	return " ".join(tokens)

print(generate("Once", seed=1))
print(generate("Once", seed=2))

It prints Once upon a hill and then Once upon a time. After "a", the table gives "time" a probability of 0.7 and "hill" 0.3. The program draws at random in proportion to those numbers, so different seeds give different stories.

A real model differs in one important way. The table looks only at the last token. A model reads the whole text on every pass, so its choice after "a" depends on everything before it.

Three ways the reply ends

A reply needs an end, or the loop would run until the program runs out of memory. Generation stops at the first of these three conditions.

  • A stop token. The model learned a special token that means "the reply is finished". In the sketch, that token is <end>.
  • A length limit. You set the most tokens the reply may have. The sketch calls it max_new_tokens, and most libraries and APIs use a similar name.
  • A stop sequence. You can give text such as a closing tag. The program stops as soon as the reply contains it.

When a reply stops in the middle of a sentence, the length limit is usually the cause. Raise the limit, or ask for a shorter answer.

Why replies appear word by word

Each token needs its own pass through the model. A chat app shows each token as soon as it arrives, instead of waiting for the whole reply. This display is called streaming. The model is not slower than it looks. It is producing the reply in the same order you read it.

The prompt is read faster than the reply is written

The model reads the whole prompt in one pass, because all of the prompt's tokens are already known. The reply has to be written one pass per token, because each token depends on the one before. For this reason, a long reply takes more time than a long prompt of the same length. Lesson 10 explains the two phases in detail.

The reply shares the context window

The Context windowThe maximum number of tokens a model can process in its context. Depending on the model, the limit can include the answer as well as the input. is the most tokens a model can handle at once, and the prompt and the reply share it. Suppose a model has a window of 128,000 tokens and your prompt uses 120,000. The reply can then be at most 8,000 tokens long, whatever length limit you set.

One token per passThe model predicts a token, the program adds it, and the loop runs again.
Always set a limitA stop token, a length limit or a stop sequence ends the reply.
One shared windowThe prompt and the reply must fit in the context window together.

Your reading position is saved

0%

0 of 20 complete

Contents

Learning tools