Module 1 — Predicting the Next Token
Fundamentally, a large language model performs a single operation iteratively: it conditions on the text generated so far and predicts the next token. A token is approximately a word (precise definitions are introduced in Module 2). To generate a sentence, the model predicts one token, appends it to the context, then predicts the next token conditioned on the extended context — and so on. This is the entire generation procedure. Every other component developed in this course serves to improve the quality of this single prediction.
The conditional-probability formulation
For students who completed Course 1, the underlying concept has already been introduced. The model estimates:
This is read as: "given the observed context, what is the probability distribution over possible next tokens?" The simplest model conditions only on the immediately preceding token — termed a bigram model. It estimates these probabilities by a method already familiar from Course 1: counting. Process a corpus, tabulate the frequency with which each word follows each other word, and normalize the counts to obtain the conditional probability \( P(\text{next} \mid \text{previous}) \).
Constructing the model by counting
Below is a small training corpus of approximately a dozen sentences. The conditional distribution of next-token frequencies has been computed for each preceding word. Select a word to display the model's learned distribution: tall bars correspond to high-probability next tokens; a flat distribution over many tokens indicates high entropy and consequently high uncertainty.
This activity needs JavaScript. The lesson below still covers everything.
Text generation via iterated sampling
The generation loop proceeds as follows: starting from a seed word, sample a next token from the model's conditional distribution, condition on that token to sample the subsequent token, and iterate. Because the bigram model conditions on only the previous token, the output exhibits local fluency but lacks coherence across longer spans. This limitation is the motivation for attention (Module 3).
This activity needs JavaScript.
# count which token follows each token, then normalize to probabilities counts = defaultdict(Counter) for prev, nxt in zip(tokens, tokens[1:]): counts[prev][nxt] += 1 # P(next | "the") — exactly the bars you saw above dist = counts["the"] total = sum(dist.values()) probs = {w: c / total for w, c in dist.items()}
A production LLM replaces the bigram counting procedure with a deep network that conditions on thousands of preceding tokens — but the model's output remains the conditional distribution P(next | context). The estimated quantity is the same; the conditioning context is substantially larger and the estimator substantially more expressive.
Check your understanding
Answer a short set of questions on next-token prediction.
This activity needs JavaScript.
Mini-game: the live tokenizer
Before tokens reach the model, text is split into pieces. Type anything — numbers, punctuation, math, a sentence — and watch exactly how an LLM sees it.
This activity needs JavaScript.