Skip to main content

Attention Mechanism

Attention lets each token ask a question of every other token, "are you relevant to me?", and blend the best answers into its own meaning, with multiple heads asking different kinds of questions simultaneously.

Intermediate5 min readv1.0Updated Jul 2, 2026
AI-assisted content — reviewed by the author, but verify important details independently

Visual SummaryClick to explore

Learning Objectives

  • Explain the query–key–value idea behind attention, without equations.
  • Describe what multi-head attention adds and why heads specialize.
  • Understand causal masking and why chatbots can't peek ahead.
  • Connect attention costs to context window economics.
  • Interpret attention as the model's learned "what matters to what."

Why This Matters

AI-012 gave you the roundtable intuition; this lesson opens the mechanism one level deeper, the level at which practitioners actually discuss models. Query/key/value, heads, causal masking, and quadratic cost are working vocabulary in every model card, paper, and engineering debate about context length (AI-061) and serving cost (AI-008).

Everyday Analogy

A library search. You bring a query ("books about resilient leadership"). Every book has a key (its spine label). You compare your query against all keys, and the best matches hand over their value (the actual content), which you blend into your understanding — weighting the strong matches heavily and skimming weak ones.

In attention, every token does all three jobs at once: it broadcasts a key ("here's what I'm about"), offers a value ("here's my information"), and sends a query ("here's what I'm looking for"). Meaning forms by matching queries to keys and blending values.

The Mechanism in Four Steps

For the sentence "The cat sat on the mat because it was warm," resolving "it":

  1. Every token creates three vectors: a query, a key, a value, by multiplying its embedding (AI-014) by three learned weight matrices.
  2. Score. "it"'s query is compared against every token's key — a similarity score each: "mat" 0.62, "cat" 0.24, "sat" 0.03…
  3. Normalize. Scores become percentages that sum to 1 (softmax): mat 60%, cat 25%, the rest small.
  4. Blend. "it"'s new representation = 60% of mat's value + 25% of cat's value + …, so "it" now means mostly-mat.

All learned, none hand-coded: training (AI-005) shaped the three matrices so that useful relevance emerges.

Multi-Head: Many Questions at Once

One attention pattern can track only one relationship type. So each layer runs many heads in parallel — 32 to 128 in large models — each with its own query/key/value matrices, each free to specialize. Research inspecting real models finds heads that track: subject↔verb agreement, pronoun↔referent links, opening↔closing quotes, and heads that attend to the previous token, or to sentence boundaries. No one assigned these roles; specialization emerged from training pressure, the same story as the vision layers of AI-003.

Causal Masking: No Peeking

For generation (AI-009), a token predicting the future must not see it. During training, attention is masked: each token may attend only to tokens before it. This single constraint is what makes the model a generator: it learns to build meaning left-to-right, exactly the condition it faces when producing text token by token. (Embedding models, which only read, skip the mask and attend in both directions — one reason reading and generating are different model families, AI-014.)

The Cost: Why Context Isn't Free

Every token scores against every other token: 1,000 tokens → 1M comparisons; 100,000 tokens → 10 billion. This quadratic growth is the engineering reason behind everything in AI-061: why context windows were once tiny, why long context is priced steeply, and why a research industry exists around cheaper approximations (sparse, sliding-window, and linear attention) and clever caching of keys/values during generation.

Real-World Showcase

  • KV caching, storing every token's keys and values so each new generated token only computes its own, is the optimization that makes chatbot streaming affordable. It's also why long conversations consume growing GPU memory.
  • Attention maps as microscopes: interpretability researchers (including at Anthropic) study attention patterns to see what the model looked at when answering — one of the few windows into the black box.
  • FlashAttention, a 2022 algorithm that reorders the same math to fit GPU memory better, sped up training industry-wide, a reminder that attention efficiency is an active frontier.

Try It Yourself

  1. Hand-run the library: for "The keys to the cabinet were on the table" — ask which earlier word determines "were" vs "was." Your answer ("keys," plural, not "cabinet") is precisely what a syntax head learns to score highly.
  2. Feel the mask: cover a sentence with paper and reveal it word by word, predicting each next word before unveiling. That constrained, left-only view is exactly the causally-masked condition an LLM trains under.
  3. Estimate the quadratic: compute comparisons for 10 tokens (100), 1,000 tokens (1M), 200K tokens (40B). Now the pricing of long-context requests (AI-061) is arithmetic, not mystery.

Common Mistakes

  • Memorizing Q/K/V as jargon without the search analogy: query asks, key advertises, value delivers.
  • Thinking heads are assigned topics — specialization emerges; nobody labels a "grammar head."
  • Forgetting the mask: chatbots build text strictly left-to-right; there is no lookahead to "plan" tokens (planning emerges differently, AI-063).
  • Treating attention scores as explanations: they show where the model looked, not why. Interpretability is deeper than attention maps.
  • Ignoring the quadratic cost. It's the physics behind every context-window product decision.

Key Takeaways

  • Attention = query·key matching, value blending: a learned relevance search run by every token.
  • Multi-head = many parallel relationship-trackers per layer; roles emerge from training.
  • Causal masking makes generation possible: each token sees only its past.
  • Cost grows quadratically with context — the root of long-context economics.
  • KV caching, FlashAttention, and attention maps are this mechanism meeting production reality.

Glossary

Query
The "what am I looking for?" vector each token sends out, like bringing a search request to a library. It is created by multiplying the token's embedding by a learned weight matrix.
Key
The "here's what I'm about" vector each token broadcasts, the spine label every book advertises. Queries are scored against all keys to find relevant tokens.
Value
The actual information a token hands over when its key matches a query well. The new representation of a token is a percentage-weighted blend of other tokens' values — "it" becomes 60% mat, 25% cat.
Multi-Head Attention
Running many parallel attention patterns per layer (32 to 128 in large models), each with its own query/key/value matrices. Heads specialize (subject↔verb agreement, pronoun links, quote pairing) without anyone assigning roles; specialization emerges from training pressure.
Causal Masking
The constraint that each token may attend only to tokens before it, so a generator never peeks at the future. This single restriction is what makes left-to-right generation possible; embedding models that only read skip it. (see AI-014)
Softmax
The normalization step that turns raw relevance scores into percentages summing to 1, so value blending has well-defined weights.
Quadratic Cost
Attention's scaling problem: every token scores against every other, so 100,000 tokens means 10 billion comparisons. It is the engineering reason context windows were once tiny and long context is priced steeply. (see AI-061)
KV Caching
Storing every token's keys and values so each newly generated token only computes its own. It's the optimization that makes chatbot streaming affordable, and the reason long conversations consume growing GPU memory.

References

Diagram

Loading diagram…

Knowledge Check

6 questions