# More Context

> Generate from a bigram booklet and a trigram booklet of the same book and compare: two words of context makes the text far more coherent and the booklet far thicker. Then build a trigram by hand, and add cheap extra context with a skip grid.

A <GlossaryTerm id="bigram-model">bigram model</GlossaryTerm> only knows the word immediately before. Give it two
words of <GlossaryTerm id="context-window">context</GlossaryTerm> and the text changes character straight away, and so
does the size of the model. Part 1 puts that trade-off on the table in ten
minutes with two pre-printed booklets. Part 2 builds the same thing by hand,
then finds a cheaper way to reach back.

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

## You will need

- a bigram booklet **and** the matching trigram booklet for the same short text,
  one of each per group:
  - _The Cat in the Hat_:
    [bigram](https://pdf.llmsunplugged.org/booklets/the-cat-in-the-hat.pdf) and
    [trigram](https://pdf.llmsunplugged.org/booklets/the-cat-in-the-hat-trigram.pdf)
  - _Green Eggs and Ham_:
    [bigram](https://pdf.llmsunplugged.org/booklets/green-eggs-and-ham.pdf) and
    [trigram](https://pdf.llmsunplugged.org/booklets/green-eggs-and-ham-trigram.pdf)
- a d10 per group, as in
  [Pre-trained Model Generation](https://www.llmsunplugged.org/modules/pretrained-generation/)
- pen and paper, ruled into two columns, one passage per booklet

Part 2 is optional and needs materials of its own; they're listed there.

## Your goal

Generate from a bigram and a <GlossaryTerm id="trigram-model">trigram model</GlossaryTerm> of the same book, then say what
changed in the text and in the booklet. Stretch goal: build a trigram by hand,
watch it run into a wall, then build a _skip grid_ that recovers some of the
benefit for far less cost.

## Key idea

More context sharpens predictions, but every extra word multiplies the number of
contexts the model has to store. Part 1 shows both halves of that: the trigram
predicts better, and the full trigram booklet blows up. Part 2 builds a cheaper
way to get some of the benefit, the skip grid. Pulling in earlier context
without paying the full cost is the same problem modern models solve with
attention.

## Part 1: two booklets, one book

No training and no cutting: hand out both <GlossaryTerm id="booklet">booklets</GlossaryTerm> and generate. Ten minutes,
and it works with any group that can already roll a booklet.

1. **Generate from the bigram booklet.** Pick a starting word, look up its
   entry, roll, write the next word down, repeat until you have about twenty
   words. This is exactly the
   [Pre-trained Model Generation](https://www.llmsunplugged.org/modules/pretrained-generation/) loop.
2. **Swap to the trigram booklet.** Each entry is headed by _two_ bold
   words (the two words that came before), so to start, open it anywhere and
   copy one entry's pair.
3. **Look up your last two words** in order and read off the next word: roll if
   the entry shows diamonds, and take the single option if it doesn't.
4. **Shift by one word.** Your context is now the second of those words plus the
   word you just wrote. Repeat until this passage is about twenty words too.
5. **Compare.** Read both passages out, then count how many entries on one page
   of each booklet offer a choice.

The trigram passage is far more coherent, to the point where groups recognise
whole lines from the book. And the trigram booklet is thicker, while most of its
entries show a single next word and no diamonds at all, leaving nothing to roll.
The extra pages bought a model that mostly replays the book back at you.

<Callout type="tip" title="Starting, and dead ends">

A trigram entry is keyed on a pair, so there's no single starting word. Copy
the two bold words at the head of any entry. Very occasionally a group lands on
a two-word context with no entry at all: that pair happened to fall at the very
end of the book, so nothing follows it. Back up a word, or start again from
another pair.

</Callout>

The [Library of Babble](https://www.llmsunplugged.org/library/) makes the same comparison in hardbound form. Each
volume is typeset in bigram, trigram and 4-gram variants, so you can see the
trade-off on the shelf: the trigram _Frankenstein_ reads better than the bigram
one, and is considerably thicker.

## Why the trigram parrots

Each extra word of context multiplies the number of possible contexts. A bigram
of a picture book needs an entry per _word_; a trigram needs one per _word
pair_, and a picture book has nowhere near enough words to see each pair more
than once. So almost every two-word context in a short text occurs exactly once,
the model has no choice to make, and it replays the training text until it
reaches one of the few contexts that fork.

This is the trade-off in <GlossaryTerm id="n-gram-model">n-gram models</GlossaryTerm>: **more context sharpens
predictions, while requiring exponentially more data to fill in all those
contexts.** Push to four or five words and you'd need a library to see each
context even once. Part 2 builds a cheaper way to bring in earlier words.

## Part 2: build it by hand

For groups with more time, or who have already built a bigram in
[Training](https://www.llmsunplugged.org/modules/training/). Building the trigram yourself shows the growth
in table size first-hand, and sets up the skip grid.

### Extra materials

- the same materials as [Training](https://www.llmsunplugged.org/modules/training/)
- extra paper for a four-column table (trigram) and a second grid (skip grid)
- pen, paper and dice as per [Generation](https://www.llmsunplugged.org/modules/generation/)

### Training the trigram

A trigram asks what follows a _pair_ of words considered together.

1. Draw a four-column table: `word1 | word2 | word3 | count`.
2. Slide a window over your text, collecting every overlapping triple of words.
3. For each triple, increment its count (or add a new row starting at 1).

After the full text (`see` `spot` `run` `.` `see` `spot` `jump` `.`) the model
is:

| word 1 | word 2 | word 3 | count |
| ------ | ------ | ------ | ----- |
| `see`  | `spot` | `run`  | 1     |
| `spot` | `run`  | `.`    | 1     |
| `run`  | `.`    | `see`  | 1     |
| `.`    | `see`  | `spot` | 1     |
| `see`  | `spot` | `jump` | 1     |
| `spot` | `jump` | `.`    | 1     |

The order of the rows doesn't matter, so you can group them by _word 1_ if that
helps.

### Generating from the trigram

1. Pick any row and write down `word1` and `word2` as your starting words.
2. Find all rows where `word1` and `word2` match your current context; note
   their counts.
3. Roll weighted by those counts to pick a row; take its `word3` as the next
   word.
4. Shift the window by one word (new context is old `word2` + chosen `word3`)
   and repeat from step 2.

### A cheaper way: the skip grid

The trigram is expensive because it tracks the two previous words _jointly_, in
one table indexed by the whole pair. The skip grid tracks them _separately_ and
adds the evidence together.

You keep two bigram-sized <GlossaryTerm id="grid">grids</GlossaryTerm>:

- the **previous-word grid**: your ordinary bigram (the word one back → the next
  word)
- the **skip grid**: the same shape, but for the word _two_ back (the word two
  back → the next word)

Two single-word grids cost far less than one word-pair table: each has one row
per _word_, where the trigram table needs one per _pair_. So you reach back two
words without the combinatorial blow-up.

<Callout type="tip" title="Why this is cheaper">

A trigram table can have up to (<GlossaryTerm id="vocabulary">vocabulary</GlossaryTerm> size)² rows, one per possible word
pair. The two skip-grid tables have at most (vocabulary size) rows _each_. You
trade the ability to track exact pairs for two cheap tables you can actually
fill in.

</Callout>

### Training the skip grid

For every triple `word1 word2 word3` in your text:

1. tally `word2 → word3` in the **previous-word grid** (this is just your normal
   bigram)
2. tally `word1 → word3` in the **skip grid**

That's it: two tally marks per triple, each in a plain two-word grid.

#### Example

Text: `the cat sat . the dog ran .`

The **previous-word grid** (word one back → next) gets the usual bigram counts:
`the→cat`, `cat→sat`, `the→dog`, `dog→ran`, and so on.

The **skip grid** (word two back → next) records what tends to appear two words
after each word:

| two back | next  | count |
| -------- | ----- | ----- |
| `the`    | `sat` | 1     |
| `the`    | `ran` | 1     |
| `cat`    | `.`   | 1     |
| `dog`    | `.`   | 1     |

The skip grid records that `the` is followed two words later by `sat` or `ran`,
whichever animal came in between.

### Generating with the skip grid

To pick the word after a two-word context `word1 word2`:

1. read `word2`'s row in the **previous-word grid**
2. read `word1`'s row in the **skip grid**
3. **add the two rows together**, cell by cell, to get combined counts
4. roll weighted by the combined counts, write the word down, shift the window,
   and repeat

#### Example

Continuing the text above, suppose your context is `the cat` and you want the
next word.

- previous-word grid, `cat`'s row: `sat` 1
- skip grid, `the`'s row: `sat` 1, `ran` 1
- combined: `sat` 2, `ran` 1

Roll on those three (1-2 → `sat`, 3 → `ran`). The plain bigram would have forced
`sat` every time; the skip grid lets `the cat` sometimes go to `ran`, a verb
that followed `the …` elsewhere in the text. The model has generalised beyond
the exact pairs it saw, and there is still something to roll for.

### What the skip grid can't do

The skip grid is smaller and weaker than the trigram. It treats the two earlier
positions as if they contribute _independently_: it adds "what follows `cat`" to
"what tends to come two words after `the`". It never captures cases where the
_pair_ carries the meaning, such as `new york` predicting something that neither
`new` alone nor `york` alone would.

Modern models solve that with
the <GlossaryTerm id="attention-mechanism">attention mechanism</GlossaryTerm>. The skip grid mixes in earlier context
with _fixed_ weights (always the word two back, always added in the same way).
Attention _learns_ which earlier words matter for each prediction, and how much,
so it can use the combination when the combination matters.

## Instructor notes

### Discussion questions

- how did the trigram passage differ from the bigram one? Could you find any of
  it in the book itself?
- which booklet was thicker, and why? What is the extra paper buying you?
- how many entries in each booklet offered a real choice (more than one next
  word)? What does a booklet full of single-option entries actually do?
- what's the trade-off between context length and data requirements? What would
  a 4-gram booklet of the same book look like?
- the skip grid lets `the cat` sometimes continue like `the dog` did. When is
  that helpful? When might it produce nonsense?
- can you think of a two-word phrase where the _pair_ matters more than either
  word on its own? (this is what the skip grid misses)

### Troubleshooting

- **"Our trigram output is just the book."** That's the expected result.
  Ask the group to mark where the passage does depart from the book; those are
  the handful of contexts the text saw more than once.
- **"There's no entry for our two words."** The pair fell at the very end of the
  book, so nothing follows it. Back up a word, or start again from another
  entry's bold pair.
- **"Which word goes first?"** In both the booklet and the hand-built table, the
  two context words are in the order they appear in the text: the leftmost is
  the older one, the rightmost is the word you wrote most recently.

### Connection to current LLMs

This module bridges simple word-pair models and modern <GlossaryTerm id="transformer">transformers</GlossaryTerm> along two
threads.

The trigram shows the **context-length trade-off**: longer context sharpens
predictions, but the number of possible contexts (and the data you need to
cover them) grows exponentially. Current models use context windows of hundreds
of thousands of <GlossaryTerm id="token">tokens</GlossaryTerm>, which is only possible because they don't store an
explicit count for every possible context the way an n-gram table does.

The skip grid shows the first step _beyond_ counting exact contexts: mixing in
earlier positions with fixed weights. Real models generalise this
into <GlossaryTerm id="attention-mechanism">attention</GlossaryTerm>, _learning_ which previous words to pull in for each
prediction. Your hand-built skip grid is fixed and additive; attention is
learned and weighted.

Source: <https://www.llmsunplugged.org/modules/more-context/>\
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
