All courses

Language modeling as next-token prediction

Write p(x₁…x_T) = ∏ p(x_t | x_{<t}), an exact factorization by the chain rule of probability. Explain why a model of next-token conditionals is a complete generative model of text.

Builds on: What an LLM computes, end to end, Generating text one token at a time

▶ Watch this lesson Start the whole course

What the animation says

One question only

An LLM only ever answers one question. Given the tokens so far, how likely is each possible next token? Yet we use it to score whole sentences and to write whole replies. So here is the puzzle: does guessing one token at a time lose anything about whole texts?

Every text is a path

Nothing is lost. To see why, draw every possible text as a tree. From the root, one branch for each possible first token. From each of those, one branch for each second token. A path stops when it takes the end branch, so every text is exactly one path. The model puts a probability on every branch, and a path's probability is its branches multiplied. Your turn: finish the path for the text 'the cat sat'.

The chain rule is exact

Written as a formula, this is the chain rule. The probability of a whole text equals the product, over every position, of that token's probability given everything before it. It is just: probability of A and B equals probability of A, times B given A, applied again and again. It is exact for any distribution. No assumption that only recent tokens matter. With the end token last, even length is covered. Errors come only from imperfect guesses.

Scoring the cat sat

Let's score one full path: the cat sat, then end. The model gives 'the' a probability of point two. 'Cat' after 'the', point oh five. 'Sat' after 'the cat', point three. Multiply as we go: point two times point oh five is point oh one. Times point three, point oh oh three. The model also gives the end token, after 'the cat sat', a probability of point one. So what is the probability of the complete text?

Add logs instead

It's point oh oh oh three: every step counted, end included. For long texts, a product like this gets too small for a computer to store. So add natural logs instead: about minus one point six, minus three, minus one point two, and minus two point three. They add up to about minus eight point one. Raise e to that power, and you're back at point oh oh oh three. Same score, safe to compute.

A full distribution

Do the conditionals alone really make a full distribution? Here's a toy model with three tokens. This function gives next-token probabilities for any prefix, and they sum to one. This one is the chain rule: multiply the conditionals along a sequence. Score one sequence and you get about point oh four. Sum over all twenty-seven length-three sequences, and you get one. So a next-token predictor scores any text, and generates by drawing one token at a time. Its quality rests only on how good those guesses are.

Can next-token guesses describe whole texts?

An LLM only ever answers one question: *given the tokens so far, how likely is each possible next token?* Yet we treat it as a model of whole documents. We ask how likely an entire sentence is, and we ask it to write complete replies. Is something lost by only predicting one token at a time, or is a next-token predictor already a **complete** model of text?

Every text is a path through a tree

Picture all possible texts as a tree. From the root there is one branch per possible first token. From each of those there is one branch per second token, and so on. A path stops at the end token, and each text is exactly one path. Next-token prediction gives the branch probabilities at every node. A path's probability is the product of its branches. Walking the tree by random draws reaches each text with exactly that probability.

The chain rule: an exact factorization

$$p(x_1,\dots,x_T)=\prod_{t=1}^{T} p(x_t\mid x_{<t})$$ $x_t$ is the token at position $t$, and $x_{<t}$ is all tokens before it ($x_{<1}$ is empty). This is $P(A,B)=P(A)\,P(B\mid A)$ applied repeatedly. It is **exact** for every distribution over sequences and assumes nothing like "only recent tokens matter". If $x_T$ is the end token, text length is covered too. Errors can come only from imperfect conditionals.

Scoring "the cat sat⟨end⟩"

Suppose the model says: - $p(\text{the})=0.2$ - $p(\text{cat}\mid\text{the})=0.05$ - $p(\text{sat}\mid\text{the cat})=0.3$ - $p(\langle\text{end}\rangle\mid\text{the cat sat})=0.1$ The product is $0.2\cdot0.05=0.01$, then $0.01\cdot0.3=0.003$, then $0.003\cdot0.1=0.0003$. For long texts this product gets too small for a computer to store, so add natural logs instead: $-1.609-2.996-1.204-2.303=-8.112$, and $e^{-8.112}\approx0.0003$.

Conditionals define a full distribution

This toy model gives a next-token distribution for any prefix. Multiplying the conditionals scores one sequence. Summing over **all** 27 length-3 sequences gives 1, so the conditionals alone define a proper distribution over those sequences. With an end token, the same holds across texts of different lengths.
import numpy as np, itertools
V = 3
def cond(prefix):              # toy p(next | prefix), sums to 1
    s = np.arange(1, V + 1) + sum(prefix)
    return s / s.sum()
def joint(seq):                # chain rule: multiply conditionals
    return np.prod([cond(seq[:t])[seq[t]] for t in range(len(seq))])
print(joint([2, 0, 1]))        # 0.5 * 0.25 * 0.333 = 0.0417
print(sum(joint(s) for s in itertools.product(range(V), repeat=3)))  # ~1.0

Remember

$p(x_1\dots x_T)=\prod_t p(x_t\mid x_{<t})$ holds exactly for any distribution over text. A model that gives the next-token distribution for every prefix can therefore score any text by multiplying, and can generate from the full distribution by drawing one token at a time. Nothing is lost by the factorization. Quality depends only on how good the conditionals are.

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.