Lesson 5

How does a model know which earlier words matter?

How a model works out what a word means from the words before it, and the design that does this, called the Transformer.

1Why can't a model tell which "bank" you mean from the word alone?

Because the word bank has two meanings, and its token is the same in both. The model needs the other words in the sentence to choose.

The two sentences as tokens

I sat on the river bank
  1. I40
  2. ␣sat10139
  3. ␣on402
  4. ␣the290
  5. ␣river20608
  6. ␣bank6922
I put money in the bank
  1. I40
  2. ␣put3006
  3. ␣money3905
  4. ␣in306
  5. ␣the290
  6. ␣bank6922

Real ids from GPT-4o's tokenizer. ␣ marks a space that is part of the token. The orange border marks bank: the same id, 6922, in both sentences. Every word here is one token, so on this page a word and a token are the same thing.

2How does a model decide which earlier words matter?

Attention is the step where a word looks at every word before it, and at itself, gives each one a share of 100%, and then mixes in their lists by those shares.

A big share means "this word matters a lot to me". A small share means "this word matters a little".

How does the model work out the shares? Think of a search box.

3How does attention change the numbers for "bank"?

Attention mixes the earlier words' lists into bank's list. A word with a big share adds a lot, and a word with a small share adds a little.

  1. 1️⃣Take each word's share of its list. In I sat on the river bank, river has 60%, and the first number in its list is 1.00. 60% of 1.00 is 0.60.
  2. 2️⃣Add up those parts. The total is the mix. The first numbers of the six parts (I, sat, on, the, river, bank): 0.01 + 0.02 + 0.01 + 0.01 + 0.60 + 0.06 = 0.71.
  3. 3️⃣Add the mix to bank's own starting list. Bank's first number starts at 0.40. 0.40 + 0.71 = 1.11.

Bank's list, before and after attention

Step 1 of 5

Made up for illustration: the names water and money, every list and every share. Real lists have thousands of numbers and no names. The river sentence uses the shares from section 2. Orange marks the word with the biggest share, and in the last step, the number that grew most. Real models first change each list with learned dials before they mix it. This picture mixes the lists as they are, so the sums stay easy. Every sum is worked out by the page script.

4How does a model know what "it" refers to?

The model looks back at the earlier words for one that fits. In The animal didn't cross the street because it was too tired, "it" is the animal.

The last word's attention: the share it gives to each word

The animal didn't cross the street because it was too tired.

  1. The1%
  2. animal45%
  3. didn't2%
  4. cross3%
  5. the2%
  6. street6%
  7. because3%
  8. it20%
  9. was7%
  10. too6%
  11. tired5%

Made-up shares, adding up to 100%. Each line runs from the last word (dark box, right) back to one word. A thicker line means a bigger share. The word in italics at the bottom is the last word itself, giving a share to itself.

5What is a Transformer?

A Transformer is a design for a model: a stack of layers, where every layer has attention. Most LLMs today are built on it, including the ones behind ChatGPT.

It first appeared in 2017.

How a Transformer works, from bottom to top

  1. 🎯 Out: from the last word's list, a chance for every possible next word
  2. Layer 32: 👀 attention, then 🧠 the second part
  3. … layers 3 to 31, each the same …
  4. Layer 2: 👀 attention, then 🧠 the second part
  5. Layer 1: 👀 attention, then 🧠 the second part
  6. 📋 In: one starting list for each word (its embedding)

32 layers, as in Phi-3-mini. Each layer gets every word's list from the layer below, and passes every word's new list to the layer above.

6Why did the Transformer change so much?

The Transformer made training much faster. Faster training let people build much bigger models.

After the last layer, the model has a chance for every token it knows. How does it pick just one, and why do you get a different answer when you ask again? Lesson 6 starts there.

← Lesson 4  ·  All lessons  ·  Next lesson →

Where this comes from

  • Token ids: run on 2026-10-31 with OpenAI's tiktoken 0.14.0, encoding o200k_base, which tiktoken maps to gpt-4o. github.com/openai/tiktoken. I sat on the river bank = I 40, ␣sat 10139, ␣on 402, ␣the 290, ␣river 20608, ␣bank 6922. I put money in the bank = I 40, ␣put 3006, ␣money 3905, ␣in 306, ␣the 290, ␣bank 6922. Both "it" sentences are one token per word (␣tired 25920, ␣wide 8174).
  • One token, one fixed starting list; "bank" is the classic case: Hands-On Large Language Models (Alammar, Grootendorst; O'Reilly 2024), ch.1, PDF p.32-35: static embeddings give a word like "bank" the same vector whatever the sentence. ch.2, PDF p.95: the embeddings matrix is part of the model.
  • Attention gives every earlier word a share, the shares add up to 100%, then the information is mixed in: Hands-On Large Language Models, ch.3, PDF p.137 (figure 3-16): "1. A way to score how relevant each of the previous input tokens are to the current token being processed. 2. Using those scores, we combine the information from the various positions into a single output vector." PDF p.141-143: the scores go through softmax "so they sum up to 1", and the rendered "Sarah fed the cat because it" example includes the current token scoring itself. PDF p.135-136: "Attention adds information from the context into the representation of the 'it' token". Softmax is the "different formula" of section 2; it raises e to each match number before dividing by the total, so the top match gets an even bigger share than plain dividing gives.
  • Search box and tags: How Large Language Models Work (Raff, Farris, Biderman; Manning 2025), ch.3, PDF p.60-61: the transformer layer is a fuzzy dictionary with a query ("what you are looking for"), keys (possible answers to match against) and values (what is returned), where "we are evaluating all keys, weighted by their degree of similarity to the query". The search-box picture is the course blueprint's simplification of this; "tag" stands for the key.
  • Searches, tags and the mixed lists come from learned dials: Hands-On Large Language Models, ch.3, PDF p.138-141: the query, key and value projection matrices are produced by training. The values (the lists that get mixed) are made with one of these, which is why section 3's note says real models change each list before mixing it. The mix is added to the word's own list (the residual connection): Vaswani et al. 2017, section 3.1.
  • The "it" sentence, tired / wide: a classic example from public explainers of the Transformer, used here with made-up shares. Decoder models such as GPT "only attend backwards": Hands-On Large Language Models, ch.3, PDF p.144-146; How Large Language Models Work, ch.3, PDF p.60: "compares it to the key for all previous tokens".
  • Stack of layers, each passing its lists up; Phi-3-mini has 32; more than a hundred in large models: How Large Language Models Work, ch.3, PDF p.53-54 and p.62 ("dozens of transformer layers"). Hands-On Large Language Models, ch.3, PDF p.124 (printing Phi-3-mini shows 32 decoder layers) and PDF p.132-133 (from about six blocks in the original paper "to over a hundred" in large LLMs).
  • The second part of each layer: Hands-On Large Language Models, ch.3, PDF p.133-134 (the feedforward layer). It works on each position on its own: Vaswani et al. 2017, section 3.3, the feed-forward network "is applied to each position separately and identically".
  • 2017; most LLMs today are Transformers: Vaswani et al., "Attention Is All You Need", arXiv, June 2017, arxiv.org/abs/1706.03762. GPT and Gemini are decoder-only transformers: How Large Language Models Work, ch.3, PDF p.52-53.
  • Parallel training, older designs read word by word, scaling: How Large Language Models Work, ch.4, PDF p.77-78: RNNs had to train sequentially, while transformers train all positions in parallel, "a prerequisite for building today's state-of-the-art LLMs using terabytes of data"; PDF p.85-86: transformers "easily scale to the tens of billions" of parameters. ch.3, PDF p.62: matrix multiplication is what makes it fast on GPUs. Hands-On Large Language Models, ch.1, PDF p.35-42: RNNs "cannot be trained in parallel"; the Transformer can. The 1,000-word text and the cashiers are this page's own examples.
  • Writing is still one token at a time; a chance for every token: Hands-On Large Language Models, ch.3, PDF p.118-120 ("it actually generates one token at a time") and p.122-123 (a probability score for every token in the vocabulary). How Large Language Models Work, ch.3, PDF p.62: an "autoregressive process".
  • Made up on purpose: the match numbers (river 12 and the rest) and the shares at bank, the names water and money, every list in the section 3 stepper, and every share in the "it" picture. Each is labelled where it appears. The sums were also checked with a separate script. Web page accessed 2026-10-31.