Each token looks back at the others.
A word means different things in different company. Attention is how a transformer lets each token pull in what it needs from the tokens before it, and it’s the idea the whole architecture is named after.
- Explain queries, keys and values
- Work through one attention step by hand
- Say why attention gets expensive on long text
You need: Lesson 2, Embeddings.
-
Words take meaning from neighbours
Predict first
In “I sat by the river bank”, which word tells the model what “bank” means?
Just show me
“river”: it’s a river bank, not a bank that keeps money.
A token’s embedding alone can’t tell “bank” the river from “bank” the money. Attention lets each token mix in information from other tokens, weighted by how relevant they are, so “bank” ends up carrying a bit of “river”.
Quick check
What does attention produce for each token?
An updated list of numbers that blends in what matters from the context.
-
Query, key and value
Predict first
Work it through below. Which earlier token will “bank” weigh most?
Just show me
“river”: 37% of the mix.
Each token makes three lists from its embedding: a query (what it’s looking for), a key (what it offers to be found by) and a value (what it passes on). “bank”’s query is compared with every key by a dot product; the scores are divided by √d (here d = 4) and turned into shares by softmax; the shares mix the values. The numbers below are small and made up so you can follow them; a real model uses hundreds per head.
The query of bank: [1, 0.2, 1.2, 0.1] (d = 4)
Token Key q · k ÷ √4 Share I 0.1 0 0.2 0.1 0.35 0.17 11.7% sat 0 0.3 0.1 0 0.18 0.09 10.8% by 0.2 0.1 0 0.1 0.23 0.12 11.0% the 0.1 0.1 0.1 0.1 0.25 0.13 11.2% river 0.9 0.2 1.4 0.3 2.65 1.32 37.0% bank 0.4 0.5 0.6 0.2 1.24 0.62 18.3% Mixed with these shares, the values give bank’s new vector: [0.433, 0.22, 0.221], the largest part from river.
Quick check
What decides how much “river” contributes to “bank”?
The query-key score sets the share; the value is what gets passed on.
-
Softmax: scores into shares
Predict first
After softmax, what do the shares for “bank” add up to?
Just show me
1: softmax turns any scores into shares of one whole.
Softmax raises e to each score and divides by the total, so big scores get most of the share and every share stays positive. Dividing by √d first keeps scores from growing with the size of the vectors; without it, softmax would give nearly everything to one token and training would stall.
Quick check
Why divide the scores by √d?
Dot products grow with dimension; scaling keeps the softmax in a range where it can still learn.
-
No peeking ahead
Predict first
While a GPT-style model works on “bank”, can it look at the words after it?
Just show me
No: a causal mask hides every later token, because when it writes, those words don’t exist yet.
Models that write (decoders, like GPT and Qwen) use a causal mask: a token attends to itself and earlier tokens only. Models that read a whole text at once (encoders, like BERT) attend in both directions, which suits classifying or embedding text but not writing it word by word.
Quick check
You want to embed whole sentences for search. Which kind fits?
Embedding models are usually encoders: they can use the whole sentence at once.
-
Many heads, many layers
Predict first
How many times does attention run for each token in Qwen 3 (8B)?
Just show me
36 layers, each with 32 attention heads, from its config file: 1,152 attention patterns per token.
A head is one query-key-value set. Several heads in a layer can each follow something different (researchers have found heads that track the previous token, or a verb’s object), and stacking layers lets later ones build on earlier ones. Qwen 3 (8B) shares each key and value between groups of query heads: 32 query heads but 8 key-value heads, which the next lessons come back to.
Quick check
Why have many heads instead of one big one?
Different heads learn different patterns; the layer combines them.
-
Why long text gets expensive
Predict first
A text doubles from 1,000 to 2,000 tokens. How do the attention scores per head grow?
Just show me
Four times: every token is scored against every earlier one, so 1,000 tokens make about 5,00,500 scores and 2,000 about 20,01,000.
The scores grow with the square of the length. At 32,768 tokens that’s about 537 million scores per head per layer. Much of the engineering in modern models, from the KV cache to faster attention kernels, is about this cost.
Quick check
Which grows fastest as a prompt gets longer?
Weights and vocabulary are fixed; attention work grows with length squared.
Lesson 3 done.
Next: Picking the next token. A language model doesn’t write a sentence; it scores every token it knows as the next one.
In the interview: attention.
EasyWhat is self-attention?
MediumWhy does scaled dot-product attention divide the scores by the square root of d_k?
MediumWhat do queries, keys and values do in attention, and why does GPT mask future tokens while BERT doesn’t?
MediumSelf-attention ignores word order. How do transformers know where each token is?
MediumWhat does the causal mask do in a decoder? Apply one in PyTorch.
HardHow can attention be made faster or cheaper?
Ask Prism about this lesson
Sources
- Vaswani and others, “Attention Is All You Need”, NeurIPS 2017: the transformer and scaled dot-product attention.
- Qwen, Qwen 3 (8B), Apache 2.0: its config file and model card for the layers and heads.