Sub topic 1
What a language model does: predicting the next token
Prerequisites
This chapter assumes you can read basic Python, know what a probability distribution is (a set of non-negative numbers that sum to 1), and have a general idea of what a neural network is. No matrix math is needed yet.
The core task
A language model is a function that takes a sequence of tokens and returns a probability for every possible next token. A token is a small piece of text, such as a word or part of a word. Chapter 2 covers how text is split into tokens. For now, treat each token as a word.
Given the prefix "the cat sat on the", a model assigns a probability to every token in its vocabulary, which is the fixed list of tokens it knows. A good model gives "mat" or "floor" a high probability and "democracy" a very low one.
Formally, the model estimates P(next token | all previous tokens). Everything an LLM does, including answering questions and writing code, comes from applying this one step repeatedly.
From one step to whole text
Any sequence has a probability that can be split into a product of next-token steps. This is the chain rule of probability:
P(t1, t2, ..., tn) = P(t1) × P(t2 | t1) × P(t3 | t1, t2) × ... × P(tn | t1..tn-1)
This is why a model that only predicts one token at a time can still score or produce long passages. To generate text, you run a loop: predict a distribution, pick a token from it, append the token to the input, and repeat.
A tiny worked example
The simplest language model is a bigram model. It conditions only on the previous token and estimates probabilities by counting. The code below builds one from a toy corpus and prints the distribution after "the".
from collections import Counter, defaultdict
corpus = "the cat sat on the mat . the cat ate the fish .".split()
counts = defaultdict(Counter)
for prev, nxt in zip(corpus, corpus[1:]):
counts[prev][nxt] += 1
def next_distribution(prev):
total = sum(counts[prev].values())
return {tok: n / total for tok, n in counts[prev].items()}
print(next_distribution("the"))The word "the" appears four times in the corpus and is followed by "cat" twice, "mat" once and "fish" once. The output is {'cat': 0.5, 'mat': 0.25, 'fish': 0.25}. The probabilities sum to 1, as a distribution must.
Why counting is not enough
The bigram model sees only one token of history. It cannot tell that "the" after "the cat sat on" should lead to "mat", because it has forgotten everything before "the". Longer histories help, but the number of possible histories grows exponentially with length. Most long histories never appear in any training text, so their counts are zero.
A neural network avoids this problem. It does not look up counts for an exact history. It learns a function that maps similar histories to similar predictions, so it can handle sequences it has never seen. The rest of this tutorial builds that function: tokens become vectors, and self-attention lets the model use the whole history.
Common misunderstandings
- "The model looks up answers." It does not store a database of sentences. It stores numeric parameters that produce probabilities.
- "Next-token prediction is a shallow task." To predict well, the model must capture grammar, facts and patterns of reasoning that appear in text. That is why one simple objective yields broad abilities.
- "The model picks the most likely token every time." That is one option, called greedy decoding. Other methods sample from the distribution.
Exercise
A corpus contains the tokens "a b a c a b". Using a bigram model, what is the probability that the token after "a" is "b"?
Show answer
The token "a" is followed by "b", "c" and "b", so "a" has three successors. "b" occurs twice, so the probability is 2/3.
Exercise
Why does a bigram model fail to predict "mat" after "the cat sat on the", and what does a neural language model do differently?
Show answer
A bigram model sees only the last token, "the", and has no information about "cat sat on". A neural language model takes the whole preceding sequence as input and learns a function that generalizes to histories it has not seen before.