Part II — Inside the Transformer Chapter 04

Attention: How Words Talk to Each Other

How glowing spotlights let a word like “bank” discover whether you mean a river or money.

9 min read · 3 interactive labs · Zero math required

In Chapter 3, we gave every word a permanent home on the map of meaning. puppy lives near dog; king lives near queen. It is a wonderfully tidy picture — right up until a word refuses to sit still.

Consider the word bank. Where on the map of meaning should it live? If you place it beside river and ducks, it makes no sense when someone walks into a bank to deposit cash. If you place it beside vault and cash, it looks absurd sitting next to a muddy shoreline.

And bank is hardly alone. Think of bat, spring, light, or even a humble pronoun like it. A word in isolation is only half a meaning. Its true meaning is born from the company it keeps.

The Big Idea

Attention is the mechanism that lets every word look around the sentence, shine bright spotlights on the neighbors that matter most to it, and move on the map of meaning to match the context.

1. The Chameleon Word

When a Transformer first reads a sentence, bank starts at a single, neutral midpoint — halfway between nature and finance. Then the attention layer wakes up. bank scans the other words in the sentence, notices which clues are strongly related to it, and pulls a blend of their meaning toward itself.

Try flipping the sentence below. Watch the glowing spotlights swing to different clues — and watch bank physically glide across the meaning map below from one neighborhood to the other. You can also click or hover any other word in the sentence to see what it pays attention to.

Interactive Lab 1 The Chameleon Word — “Bank” Context Switcher
Spotlight strength Active word
Click or hover any word above
In Sentence A, bank shines its brightest beams on river and ducks — pulling its dot straight into the Nature & Water neighborhood. Switch to Sentence B to watch it cross the map.

Notice what just happened: the exact same token bank entered the layer with the exact same starting coordinates, yet left the layer in two completely different places. Attention turned a static dictionary into a living, context-aware conversation.

2. Query, Key & Value: The Library Analogy

This raises a natural question: how does bank know to shine its spotlight on river instead of the? You do not need any equations to understand this. Imagine every word walks into a quiet library carrying three glowing index cards:

When a word’s Query aligns closely with another word’s Key, a bright beam snaps between them. The stronger the match, the wider the beam — and the more of that neighbor’s Value flows across to enrich the asking word.

Interactive Lab 2 Query, Key & Value — The Library Analogy
Query (Question) Key (Label) Value (Info Packet)
Click a word below to inspect its Query, matching Keys, and incoming Value packets.
Inspect word:
Watch how its asks “Whose thing?” and lights up dog, while tired asks “Who am I describing?” and also locks onto dog. Different questions pull different glowing packets across the sentence.
Why Separate the Key from the Value?

Think of a book’s spine versus its pages. The Key is the short title on the spine that helps you spot the right book from across the room. The Value is the rich story inside the pages that you actually take home once you pull it off the shelf.

3. Causal Masking: No Peeking at the Future

There is one crucial rule we haven’t mentioned yet. When a language model writes a response, it generates text one word at a time from left to right. When it is deciding what comes after The cat sat, the words on the warm mat do not exist yet!

Even during training, when the whole sentence is sitting right in front of the model, we have to enforce a strict no-peeking rule. Otherwise, guessing the next word would be as trivial as glancing one inch to the right.

So Transformers drop a digital curtain called a Causal Mask. Each word may shine its spotlight backward at earlier words (and onto itself), but every word to its right is locked behind the curtain. Drag the slider below — or click Step Forward — to watch the curtain slide open one word at a time.

Interactive Lab 3 Causal Masking — No Peeking Ahead
Visible past Hidden future
Active Word: 4 / 7
Notice the glowing staircase triangle on the right: word 1 sees only itself, word 2 sees words 1–2, and only the final word can see the entire sentence. Every word predicts the future using strictly the past.

Pause and admire how clean this is. With just three simple ingredients — matching Questions to Labels, passing Info Packets along the brightest beams, and keeping the future behind a curtain — a collection of isolated word-dots becomes a fluid web of meaning.

Yet there is one limitation lurking here: a single spotlight can only focus on one kind of relationship at a time. What happens when a sentence requires tracking pronouns, adjectives, and actions all at once? That is our next stop.

Check Your Intuition

In the sentence “The river overflowed its bank,” why does the attention layer change the representation of the word bank?