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.
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.
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:
- Question Tag (Query) — “What kind of clue am I hunting for right now?” For example, the word its holds up a Question Tag asking: “Whose thing am I?”
- Label Tag (Key) — “Here is what I am, in case someone is looking for me.” The word dog holds up a Label Tag saying: “I am a living creature & the main subject.”
- Info Packet (Value) — “If our tags match, here is the meaning I will send you.”
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.
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.
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.