In-context Learning
Key idea: The text you've already written is context the model can learn from without changing a single count---copy what followed last time (induction), or lean toward recent words (memory).
Used in Under the hood --- ready-to-run lessons and talks that include this module.
A bigram only ever looks at the word before. But the page in front of you holds everything you’ve written so far, and that’s context the model can use. This module adds two rules that read that page. Neither one changes the model itself, which makes them a hands-on version of In-context learning Picking up a pattern from the prompt and continuing it, with no change to the model's weights. The "learning" happens in the context the model is given, not in the model itself---which is why a few examples in a prompt can steer an LLM's output. View in glossary .

You will need
- any model you can already generate from: your bigram grid, a cutouts spread, or a pre-trained booklet
- pen and paper for your generated text (you’ll be reading back over it, so keep it tidy)
- dice as per Generation
There’s nothing new to print, because both rules are procedures you lay over a model you already have.
Your goal
Part 1: make the model complete a made-up pattern it could not possibly have learned in training. Part 2: generate twice from the same model—once plain, once with a short-term memory—and compare how long each stays on topic. Stretch goal: find the memory setting that keeps the text on topic without making it repetitive.
Key idea
Both rules take the text you’ve already written and use it to weigh the next word.
- Induction: find the last time this exact word appeared and copy whatever followed it. This completes patterns the model has never seen.
- Memory: give recently-used words a small boost. This keeps a story about a dog being about the dog.
The model never retrains. The “learning” lives in the context, not the counts.
Part 1: finish the pattern
Start here; it takes two minutes. Write down this sequence, using words your model has never seen together:
moon five apple moon five apple moon five
Now generate the next word. Your current word is five. Instead of consulting
the model, scan back up the page to the last time five appeared, and copy
the word that followed it: apple. Do it again from apple and you get moon,
then five, then apple…
You’ve completed the pattern moon five apple, which appears nowhere in the
training text and which the base model has no row for. The model picked up your
invented rule from a single example, with no retraining at all. Now turn the
rule off and generate from the base model alone: the pattern disappears.
The induction rule
- Look at your current word.
- Scan back through your written output to the most recent earlier time that same word appeared.
- If you find one, the word that followed it is your induction candidate.
- Blend with the base model: roll a die—mostly copy the candidate, occasionally generate from the model as normal. If there’s no earlier occurrence, there’s no candidate; just generate.
- Write the word down and repeat.
The rule only ever reads your page, so it works identically on a grid, a spread or a booklet. Only the fall-back differs: roll on the row, pick from the matching cutouts, or look up the booklet entry.
Part 2: stay on topic
The induction rule is exact: the word matches or it doesn’t. The memory rule is softer. Mention a dog, and two words later a bigram has already forgotten. Real language reuses what came before, so we keep a running list of recent words and tilt the odds toward them.
The memory rule
- Keep a memory list of the last eight or so words you’ve written (underline them, or jot them on a sticky note).
- Find the model’s candidates for the next word as you normally would—the row in your grid, the matching cutouts, the booklet entry.
- Boost any candidate that’s also in memory (see below for how, per base).
- Pick from the boosted candidates, write the word down, add it to memory, and drop the oldest word so the list stays short.
- Repeat.
Worked example
Your current word is the, and the model offers cat 3, dog 1, sun 1. A
moment ago the text mentioned a dog, so dog is in memory. Give it a bonus of,
say, +3:
cat: 3dog: 1 + 3 = 4sun: 1
Roll on the new totals (1–3 → cat, 4–7 → dog, 8 → sun). dog has gone
from unlikely to favourite, so the story is more likely to stay about the dog.
cat and sun are still possible, though; the memory only tilted the odds.
Applying the boost to your model
- On a grid: add the bonus straight to the counts in the current word’s row, then roll on the new totals, as in the worked example.
- On a cutouts spread: once you’ve gathered the cutouts that match your current word, check their next words against memory. For any match, count that cutout twice (or write a duplicate) before you pick, so the spread does the reweighting for you.
- On a booklet: the dice thresholds are pre-printed, so blend instead. Roll a “memory die” first (a d10, 1–3 is a hit). On a hit, scan the current entry’s options and, if one is in memory, pick it. On a miss, or if nothing matches, roll the booklet exactly as normal. You only ever pick from this entry’s options, so you never leave what the model allows.
Instructor notes
Discussion questions
- the model completed
moon five apple, which it never trained on. Where was that “knowledge” stored? - why does the demo use made-up words rather than an ordinary sentence?
- what happens if you only ever copy, and never fall back to the base model?
- with the memory on, does the text hold a topic or a character for longer? Where does “on topic” tip over into “stuck repeating”?
- why does the memory only boost words the model already offers?
- induction and memory both read the page. When would each one help? How are they different from the trigram, which also uses more than one word?
- (stretch) if induction matched on the last two words instead of one, why would the completion be more reliable?
Connection to current LLMs
This module is a hands-on version of three things modern models do.
Induction heads. These are an identified circuit inside transformers, and
one of the main mechanisms behind in-context learning:
an
Induction head
A circuit found inside transformers that completes patterns by finding an earlier place where the current token appeared and copying what came next. Induction heads are a key mechanism behind in-context learning.
View in glossary
finds an earlier
place where the current token appeared and copies what came after it—exactly
your scan-back-and-copy rule. It’s why few-shot prompting works: give an LLM a
few “input → output” examples and it continues the pattern. Researchers detect
these circuits by feeding a model a random sequence repeated twice and checking
that it predicts the second copy far better than the first, which is the machine
version of your moon five apple demo. Real induction heads match softer,
richer patterns and blend smoothly with everything else the model knows, but the
behaviour is the same idea.
Attention. A bigram looks back one word; your memory looks back over everything you’ve generated and lets it influence the next choice. That’s the heart of the Attention mechanism The ability to focus on relevant previous words when making predictions. In real LLMs, attention is learned, weighted, and dynamic---the model decides what to focus on for each prediction. The in-context learning module illustrates the motivation for attention: reusing more than just the immediately preceding word. View in glossary : when predicting the next word, the model reaches back across the whole context and weights what it finds. Your memory is a crude, fixed version (boost recent words); attention is learned and content-sensitive, and can decide on the fly which earlier words matter.
Repetition. Real models get stuck on rails too, which is why generation settings include repetition penalties: a deliberate down-weighting of recent words, the opposite of the boost you added.