The operation that gives the transformer its name. Inside every layer, attention does one thing: it lets each token look at the other tokens it’s allowed to see and decide which ones matter for what comes next. It’s how a token several positions back becomes relevant to the token being processed now.
Query, Key, Value
Each token is transformed into three learned vectors, playing three roles at once:
- Query — “what am I looking for from other tokens?”
- Key — “this is what I offer to tokens looking at me”
- Value — “this is what gets passed along when a match happens”
The Q/K/V transformations are learned matrices, so training decides what each token looks for and offers. Matching works by scaled dot product: a token’s Query is compared against every visible Key (the dot product scores how aligned two vectors are; the scaling keeps numbers stable). Softmax turns those scores into weights that sum to 1, and the output is a weighted average of the Value vectors.
"The cat that I saw yesterday was sleeping."
Processing “was,” the model needs to know what’s doing the sleeping. The Query for “was” produces a high dot product with “cat” (the model learned that verbs need subjects, and subjects produce matching Keys) and a low one with “yesterday.” Softmax gives “cat” a high weight, so the Value for “cat” dominates the new representation of “was.” That’s coreference, done with arithmetic.
Causal Masking
GPT-style models generate left to right, so a token at position 5 may only attend to positions 1–5, never to tokens not yet generated. This is causal masking, implemented simply: future tokens get match scores so low they round to zero weight after softmax.
Induction Heads
One of the clearest interpretability findings (Anthropic, 2022): specialized heads that spot patterns of the form “A B … A” and predict that B comes next. When the model sees “A” the second time, the induction head looks back to where “A” appeared before, sees what followed, and copies it. They’re one of the clearest known mechanisms behind in-context learning — the ability to pick up a pattern from your prompt and continue it.
Multi-head Attention
One attention pass gives one view of token relationships, but language has many at once (subject–verb agreement, pronoun resolution, long-range references, local phrasing). So transformers run attention many times in parallel as heads, each in its own smaller space.
A common tutorial error worth correcting: each head does not get a literal slice of the token vector. Each head has its own learned projection that maps the full vector (e.g. 4,096 numbers) down to its own smaller Q/K/V (e.g. 128). They’re different views of the same token, not different chunks of it. After each head runs independently, the outputs are concatenated and passed through a final learned mixing layer.
Heads specialize on their own during training — nobody assigns roles. Researchers have found heads that track grammar, heads that resolve pronouns, heads that track position, induction heads, and many more. A layer might have 32 heads; a frontier model has dozens of layers, so a typical LLM has thousands of heads, each adding a learned view.
The Cost: Quadratic Scaling and the KV Cache
Full attention compares each token against all visible tokens, so doubling the prompt roughly quadruples the work. This is why long prompts are expensive and why a lot of recent research targets cheaper attention (FlashAttention, sparse attention, linear attention). It’s also the mechanical root of the tradeoffs in Context Window Management Strategies.
During generation, each head must keep its Keys and Values for every already-generated token so it doesn’t recompute the whole prefix each step. This is the KV cache, the main memory cost of running an LLM at long context. Grouped-Query Attention (GQA) reduces it by having groups of query heads share fewer Key/Value heads (LLaMA-2 70B: 64 query heads, 8 K/V heads; Mistral 7B: 32 and 8) — nearly the same accuracy at much lower memory and inference cost.
Related
- Transformer Architecture — the full pipeline this sits inside
- Tokenization — what produces the tokens attention operates over
- Context Window Management Strategies — the practical consequence of quadratic cost + KV cache
- Introduce to LLM — the plain-English overview