# In-context Learning

> Make your model finish a pattern it never trained on, purely from what's already on the page, then give it a short-term memory that keeps the text on topic. The tricks behind few-shot prompting and attention.

A <GlossaryTerm id="bigram-model">bigram</GlossaryTerm> only ever looks at the word before. But the page in front of you holds
everything you've written so far, and that's <GlossaryTerm id="context-window">context</GlossaryTerm> 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 <GlossaryTerm id="in-context-learning">in-context learning</GlossaryTerm>.

![](../../assets/images/hero-in-context-learning.avif)

## You will need

- any model you can already generate from: your
  [bigram grid](https://www.llmsunplugged.org/modules/training/), a [cutouts spread](https://www.llmsunplugged.org/modules/generation/),
  or a [pre-trained booklet](https://www.llmsunplugged.org/modules/pretrained-generation/)
- pen and paper for your generated text (you'll be reading back over it, so keep
  it tidy)
- dice as per [Generation](https://www.llmsunplugged.org/modules/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, and the "learning" lives in the context.

## 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`. 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 <GlossaryTerm id="base-model">base model</GlossaryTerm> 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

1. Look at your **current word**.
2. **Scan back** through your written output to the most recent earlier time
   that same word appeared.
3. If you find one, the word that **followed** it is your _induction candidate_.
4. **Blend with the base model**: roll a die, and mostly copy the candidate
   but occasionally generate from the model as normal. If there's no earlier
   occurrence, there's no candidate; just generate.
5. Write the word down and repeat.

<Callout type="info" title="Falling back to the model">

If you _always_ copy, the model just replays whatever pattern it's in, which is its own
way of getting stuck on rails. Keeping a chance of falling back to the base
model lets it break out and start something new.

</Callout>

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 needs an exact match. 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

1. **Keep a memory list** of the last eight or so words you've written
   (underline them, or jot them on a sticky note).
2. **Find the model's candidates** for the next word as you normally would: the
   row in your grid, the matching cutouts, the booklet entry.
3. **Boost any candidate that's also in memory** (see below for how, per base).
4. **Pick** from the boosted candidates, write the word down, add it to memory,
   and drop the oldest word so the list stays short.
5. 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`: 3
- `dog`: 1 + 3 = 4
- `sun`: 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.

<Callout type="info" title="Boost what the model offers">

The tempting shortcut is to grab a recent word from memory and write it straight
down. That ignores the current word (dropping in words that don't follow at
all), and every word you emit goes back into memory, so the text collapses into
repeating a handful of words. The memory only ever boosts words the model
**already offers**.

</Callout>

### 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.

<Callout type="tip" title="Which model shows it best?">

The memory works best on a larger model like the booklet, where a bigger
vocabulary lets it stay on topic without repeating itself. On a very small
hand-built grid, be gentle with the bonus: boosting recent words can amplify
the short loops small models already fall into.

</Callout>

## 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 a sequence of unrelated words?
- 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](https://www.llmsunplugged.org/modules/more-context/), which also uses
  more than one word?
- (stretch) if induction matched on the last _two_ words, 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 <GlossaryTerm id="transformer">transformers</GlossaryTerm>, and
one of the main mechanisms behind in-context learning:
an <GlossaryTerm id="induction-head">induction head</GlossaryTerm> finds an earlier
place where the current token appeared and copies what came after it, which is
your scan-back-and-copy rule. It's why few-shot prompting works: give an <GlossaryTerm id="llm">LLM</GlossaryTerm> 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
idea behind
the <GlossaryTerm id="attention-mechanism">attention mechanism</GlossaryTerm>: 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 <GlossaryTerm id="repetition-penalty">repetition penalties</GlossaryTerm>: a deliberate _down_-weighting of recent
words, the opposite of the boost you added.

Source: <https://www.llmsunplugged.org/modules/in-context-learning/>\
Licence: [CC-BY-SA-4.0](https://creativecommons.org/licenses/by-sa/4.0/)\
Cite as: Swift, B. (2026). _LLMs Unplugged: Understand how AI language models work by building one yourself._ https://doi.org/10.5281/zenodo.17403824
