In this lesson
The game a language model plays
No neurons yet. Count what follows each word in a tiny corpus, bet on the next one, and run the generation loop by hand.
Course
From text to chatbot
Course order, not one forward pass.
I · From text to representations
- 01PredictionTransformer stepNot started
- 02TokenizationTransformer stepNot started
- 03EmbeddingsTransformer stepNot started
II · Inside the Transformer
III · Prediction and learning
IV · From model to chatbot
By the end, you can
- Explain next-token prediction without crediting the model with intent or lookup.
- Describe how a longer context changes the probabilities of what comes next.
Better late than ___. You filled that in before finishing the sentence. A language model plays a higher-stakes version of the same game: given some text, it bets on the next piece, writes it, then bets again.
That piece is called a token. Here our tokens are whole words; the next lesson shows why real tokenizers cut text differently. First, play the game with the dumbest bookmaker in the business: a table of counts.
Observe a corpus become counts
The instrument holds a complete corpus of eight short sentences, small enough to count by hand. One rule builds the model: for each context, tally every word seen immediately after it.
“The cat” is followed by sleeps twice and eats once. After “the cat,” the table therefore bets 2-to-1 on sleeping. It has already priced the bet you are about to make.
Predict the winner
The corpus contains eat an orange, peel an orange, and eat an apple. The counting model has just read the word an.
Your bet
In this corpus, which word wins the bet right after “an”?
Commit before manipulating the instrument. This choice is final.
The whole corpus fits on screen. Every percentage is a count divided by a total: check it by hand.
Live missions
- Make the loop go silent
- Make apple win the bet after “an”
The complete corpus
- the cat sleeps on the mat
- the cat sleeps under the table
- the cat eats a mouse
- the dog watches the cat
- the dog sleeps on the mat
- eat an (Observed context)orange (Next word)
- peel an (Observed context)orange (Next word)
- eat an (Observed context)apple (Next word)
Occurrences of the context are underlined; the counted word right after it is boxed.
Edit the corpus
Rewrite the sentences, one per line. Counts, bars, and the loop recompute live.
What follows this context
| Next word | Count | Share |
|---|---|---|
| orange | 2 / 3 | 66.7 % |
| apple | 1 / 3 | 33.3 % |
P(word | context) = count / total
The generation loop
The model rereads its last words, draws according to the shares, appends the word, and repeats.
an
Manipulate the loop
Pick different contexts and watch the shares change. To compare longer context, select the, switch to 2 words, then choose the dog; it selects a different row, with a coin flip between watches and sleeps.
Select the context the, then press Draw a word several times. The model samples from the shares, appends its choice, rereads its latest context, and bets again.
That self-feeding cycle is autoregressive generation. An LLM runs the same loop over tokens: one bet at a time, conditioned on whatever pieces it can still see.
Now open Edit the corpus. Rewrite the eight sentences, one per line, and watch the counts, bars, and loop recompute. At first, orange beats apple 2-to-1 after “an.” One well-aimed sentence can turn the underdog into the favorite.
Primary. Make apple win after “an” by editing the corpus. Explain which count changed and why that changed the odds.
Stretch. Keep drawing until the loop halts. Name the missing row that made the counting model go silent.
Compare your answer
Primary. Replace one “an orange” continuation with “an apple.” The counts become 2 for apple and 1 for orange. The odds move because the row changed, not because the table learned botany.
Stretch. The loop halts after a sentence-final word such as “mat,” “mouse,” or “orange.” That word has no continuation in the corpus, so the table has no row to bet from.
Explain the betting machine
The table implements one formula, visible under the bars:
P(word | context) = count / total
Read it as “the probability of a word, given the context.” The vertical bar marks the condition. Change the context and you may select different counts, so the distribution - the full set of shares - changes too.
That is why one table can continue “eat an” and “peel an” differently. No understanding is required here, only different rows of counts.
- You read… an ___
- The corpus countsorange: 2 · apple: 1
- The model betsorange (66.7%)
The fatal flaw was hiding in the stretch mission: a context with no row, seen or unseen, leaves the bookmaker no counts and no odds to post. Longer rows can be more specific, but they are also easier to miss entirely.
A table of exact matches cannot generalize beyond its rows. The rest of the course builds a machine in which related tokens and contexts can share evidence instead of requiring identical text.
Prediction is not retrieval. This model did not look up an answer or consult a fact. It continued text from a conditional distribution.
A full LLM plays the same next-token game with far better machinery. It can therefore be fluent and false: the training target is fluent continuation, and nothing in that target checks for truth.
Reflect on what a percentage means
Checkpoint
The instrument shows “orange: 66.7%” after the context “an.” What does that number actually report?
Pick an answer first.
Checkpoint
Select “the,” switch the context length to two words, then choose “the dog.” What happens to the bet?
Pick an answer first.
Connect the game to the machine
You know the loop: predict, append, repeat. You also know why an exact-count table loses the game: unseen contexts leave it speechless.
Real models bet on tokens rather than whole words. The next chapter opens the cutting room and asks what those pieces should be, starting with strawberry and its three r’s.
Sources and scope
- Shannon (1948) introduces n-gram models of text and the generation-by-sampling experiment this lesson reenacts.
- Bengio, Ducharme, Vincent & Jauvin (2003) states the n-gram generalization problem and proposes learned word vectors as the remedy - the road this course follows.
- The eight-sentence corpus is an original teaching fixture; its counts are designed to be checkable by hand, not representative of real language statistics.
- Content and claims reviewed on July 28, 2026.