Attention — where do language models look?

When ChatGPT, Claude or DeepL try to understand a sentence, they have to decide — for every single word — which other words matter right now. That targeted listening is called attention, and you can watch it happen here. Click through heads, layers and sentences.

What is attention?

AnalogyDefinition

Picture a loud cocktail party: dozens of voices talking at once — but the moment someone across the room says your name, your head turns automatically. Out of all that noise, your brain picked one piece of information and gave everything else less weight.

That selective act — turning the volume up on what matters, down on what doesn't — is attention.

Reading is the same thing in miniature: the moment you see the word „it", your eye flicks back to the noun it refers to. You weight, in a flash, which earlier words are relevant and tune the rest out.

A transformer language model does this for every single word at the same time — freshly per layer and per attention head.

Example: „The animal didn't cross the street because it was too tired."

What does „it" refer to? You know instantly: the animal — not the street, not „cross". The language model has to make the same call, otherwise the translation breaks or the answer turns to nonsense. In a trained model, a single attention head learns exactly this pattern: pronoun → correct antecedent.

Without attention there would be no ChatGPT, no Claude, no DeepL translator today — they all rest on this one idea, first formulated in this form in the 2017 paper „Attention Is All You Need".

Step by step

Here is the picture behind it. Imagine every word in the sentence walking into a library at the same time.

Each word brings a search slip (that's the Query), reads through the index cards of all the other books (the Keys), weights which cards match its search best, and takes home a weighted mixture of the matching books' contents (the Values).

That mixture is the word's new, context-enriched meaning. Technically, this runs in five steps per layer:

  • Each token is projected into three vectors: Query (Q), Key (K), Value (V).
  • Q·K\u1D40 produces an n×n score matrix — how similar is every query to every key?
  • Row-wise softmax turns scores into proper attention weights (each row sums to 1).
  • A token's output is the weighted sum of all value vectors — information flows in from wherever it looks hardest.
  • Multi-head attention does this in parallel with different projections — each head learns a different relation type (syntactic, semantic, positional).

Layer 1 usually listens locally — „what's right next to me?" Layer 2 builds the bigger-picture relations: pronoun resolution, verb-object binding, sense disambiguation.

Real models like GPT-4 or Claude don't have 2 layers, but 80 to 100 layers stacked on top of each other, each with 32 to 128 heads.

This demo is a deliberate mini-version with only 2 layers and 4 heads — so the principle stays in your head instead of drowning in a fireworks display of numbers.

Query, Key, Value — every token plays three roles

Stick with the library image from above — it carries surprisingly far.

Each word in the sentence is simultaneously searcher, index card, and book content. From a single word embedding (a list of numbers describing the word), the model computes three derived vectors — Q, K and V — through three learned projection matrices.

You can picture them as three different lenses the model puts on each word. Each lens highlights a different aspect:

  • Query (Q) — the search slip — „What am I looking for right now?" Q is the search slip a word hands out to all the others.

    When the pronoun „it" becomes active, its query asks, roughly: „Which word earlier in the sentence is a singular noun, an animate thing that could be tired?"

    The question isn't written in words on the slip — it lives as a direction in a number vector.
  • Key (K) — the index card — „What do I offer?" K is the index card each word holds up clearly in the library.

    The card on „animal" effectively says: „I'm a noun, singular, an animate being."

    When the search slip from „it" hits this card, they line up — the dot product Q·K becomes large. The better the question and the card match, the more attention flows to that word.
  • Value (V) — the book content — „What do I hand over when I get picked?" V is the book content the word releases once it has been given attention.

    As soon as the weights are fixed (every row sums to 100 %), all value vectors are added up according to their weights. The result is the query word's new, context-enriched meaning.

    Mnemonic: K decides who is heard; V carries what is heard.

Why „self"-attention? Because every word in the same sentence acts simultaneously as Query and Key and Value for every other word (and for itself). The sentence essentially looks at itself — every word querying every other and comparing.

In the library image: every reader is also a book with its own index card. That's what sets self-attention apart from cross-attention, where Q comes from one sequence and K/V from another (classically: encoder→decoder in machine translation).

Why ÷ √d_k? A detail that looks harmless — and rescues training.

At high vector dimensions (d_k = 64 or 128 in real models) the Q·K dot products grow large by nature. Softmax with large inputs peaks sharply: nearly 100 % on a single word, all others near zero, and the gradient dies.

Dividing by √d_k keeps the input variance constant no matter how big the vectors are. A small number with a big effect — and exactly why the method is called scaled dot-product attention.

The formula in detail

Now the math — but don't worry. You don't need any linear algebra to follow along.

The formula looks compact at first because it writes what you've just met — search slip, index card, book content — in mathematical shorthand. Read it line by line, top to bottom.

Every step gets a plain-text sentence next to it explaining what happens. By the time you reach the final expression after six steps, you've effectively read a weighted average with similarity weights — that's really all it is.

Q = X · W_Q
Project the token into a "question vector" — what we are looking for.
K = X · W_K
Project the token into a "key vector" — what it advertises.
V = X · W_V
Project the token into a "value vector" — the information it would provide.
S = (Q · Kᵀ) / √d_k
Dot product of all queries × keys, divided by √d_k to stabilise.
A = softmax(S, axis = -1)
Row-wise softmax turns scores into probabilities (each row sums to 1).
output = A · V
Attention weights mix the value vectors — that is the new representation.
Put together:
Attention(Q, K, V) = softmax(QKᵀ / √d_k) · V

One thing to take away: these six lines are essentially the whole mechanism. Everything a modern transformer does is, at its core, the same calculation — only with learned projection matrices, hundreds of dimensions, dozens of heads and dozens of layers stacked on top.

Q·K workshop — how vectors become attention

Enough theory — here you see the calculation once, hands-on, with mini vectors in just 4 dimensions.

We deliberately name the dimensions: ref = reference, act = action, mod = modifier, gen = generic content. That helps you build an intuition for what a vector dimension could mean.

Click a pronoun, verb or determiner in the heatmap further down — the workshop will then show you, step by step: query vector → dot product with the candidates → softmax.

Real models use 64 or 128 dimensions that aren't named so human-readably. The principle is exactly the same — only richer and finer.

Important: in real transformers the Q and K vectors are learned from training data — billions of sentences slowly shape the direction every vector points in. Here they are hard-wired per role (pronoun, antecedent, verb, …) for the demonstration, so the mechanism itself becomes visible. Think of it like a plastic anatomy model: not real, but honestly built.
1Q vector of the selected token (what it looks for)
Q8„er"Role: pronoun (seeking antecedent)
ref 0.90
act 0.00
mod 0.00
gen 0.10
2Dot product Q·K with candidate keys (how well do they match?)
K4„Hund"Role: antecedent
ref 0.90
act 0.00
mod 0.00
gen 0.10
Q · K = 0.90 · 0.90 + 0.00 · 0.00 + 0.00 · 0.00 + 0.10 · 0.10 = 0.820
÷ √4 = 0.410
K1„Leonie"Role: background
ref 0.10
act 0.10
mod 0.10
gen 0.20
Q · K = 0.90 · 0.10 + 0.00 · 0.10 + 0.00 · 0.10 + 0.10 · 0.20 = 0.110
÷ √4 = 0.055
K2„gab"Role: background
ref 0.10
act 0.10
mod 0.10
gen 0.20
Q · K = 0.90 · 0.10 + 0.00 · 0.10 + 0.00 · 0.10 + 0.10 · 0.20 = 0.110
÷ √4 = 0.055
K8„er"Role: self token
ref 0.20
act 0.20
mod 0.20
gen 0.30
Q · K = 0.90 · 0.20 + 0.00 · 0.20 + 0.00 · 0.20 + 0.10 · 0.30 = 0.210
÷ √4 = 0.105
3Softmax → attention distribution (each row sums to 100%)
  • Hund32%
  • Leonie22%
  • gab22%
  • er23%
Aha moment: tokens with similar directions in vector space produce high dot products — tokens that have nothing to do with each other produce low ones.

The whole attention game is, at its core, a similarity game in number space. In a real model the vector directions are learned from data and encode much finer aspects than our four demo dimensions — but if you've understood what happens here with just 4 numbers, you've understood the essence of what happens inside a 128-dimensional GPT vector.

Worth knowingBefore you dive into the heatmap, a quick reality check: the patterns you're about to see are a deliberate didactic simplification. We set the attention weights via hand-written rules — so you can see what attention does without having to train a model for hours to get anything recognisable on screen.

Real transformers like GPT-4, Claude or BERT learn very similar patterns — but from billions of sentences of training data, with nobody hard-coding „it → animal". That is the real wow moment: the patterns emerge on their own from the data.

So this demo shows what comes out — so you'll recognise it in a real heatmap later — not how the training itself proceeds.

Interactive heatmap

Tip:How to read the heatmap: each row belongs to a word that is looking (the query). Each column belongs to a word that is being looked at (the key). A bright cell means strong attention. Click a highlighted token (e.g. a pronoun) — you'll see its row stand out.
Advanced view — 4 more patterns + layer/head selector
HeatmapArcs
Pronoun→antecedent

The crucial head for language understanding: pronouns find their antecedent. "it" → "animal", "sie" → "Aufgabe". Real disambiguation in action.

Layer 2, head 1: "er" looks back at "Hund" — clean pronoun resolution.
Click a token — see what it looks at
Keys — these tokens get looked at
Queries — these tokens look
10×10 attention grid · pattern: Pronoun→antecedent. The crucial head for language understanding: pronouns find their antecedent. "it" → "animal", "sie" → "Aufgabe". Real disambiguation in action.Leonie → Leonie: 0.10Leonie → gab: 0.10Leonie → dem: 0.10Leonie → Hund: 0.10Leonie → einen: 0.10Leonie → Knochen: 0.10Leonie → weil: 0.10Leonie → er: 0.10Leonie → hungrig: 0.10Leonie → war: 0.10gab → Leonie: 0.10gab → gab: 0.10gab → dem: 0.10gab → Hund: 0.10gab → einen: 0.10gab → Knochen: 0.10gab → weil: 0.10gab → er: 0.10gab → hungrig: 0.10gab → war: 0.10dem → Leonie: 0.10dem → gab: 0.10dem → dem: 0.10dem → Hund: 0.10dem → einen: 0.10dem → Knochen: 0.10dem → weil: 0.10dem → er: 0.10dem → hungrig: 0.10dem → war: 0.10Hund → Leonie: 0.10Hund → gab: 0.10Hund → dem: 0.10Hund → Hund: 0.10Hund → einen: 0.10Hund → Knochen: 0.10Hund → weil: 0.10Hund → er: 0.10Hund → hungrig: 0.10Hund → war: 0.10einen → Leonie: 0.10einen → gab: 0.10einen → dem: 0.10einen → Hund: 0.10einen → einen: 0.10einen → Knochen: 0.10einen → weil: 0.10einen → er: 0.10einen → hungrig: 0.10einen → war: 0.10Knochen → Leonie: 0.10Knochen → gab: 0.10Knochen → dem: 0.10Knochen → Hund: 0.10Knochen → einen: 0.10Knochen → Knochen: 0.10Knochen → weil: 0.10Knochen → er: 0.10Knochen → hungrig: 0.10Knochen → war: 0.10weil → Leonie: 0.10weil → gab: 0.10weil → dem: 0.10weil → Hund: 0.10weil → einen: 0.10weil → Knochen: 0.10weil → weil: 0.10weil → er: 0.10weil → hungrig: 0.10weil → war: 0.10er → Leonie: 0.006 .01 er → gab: 0.006 .01 er → dem: 0.006 .01 er → Hund: 0.89 .89 er → einen: 0.006 .01 er → Knochen: 0.006 .01 er → weil: 0.006 .01 er → er: 0.06 .06 er → hungrig: 0.006 .01 er → war: 0.006 .01 hungrig → Leonie: 0.10hungrig → gab: 0.10hungrig → dem: 0.10hungrig → Hund: 0.10hungrig → einen: 0.10hungrig → Knochen: 0.10hungrig → weil: 0.10hungrig → er: 0.10hungrig → hungrig: 0.10hungrig → war: 0.10war → Leonie: 0.10war → gab: 0.10war → dem: 0.10war → Hund: 0.10war → einen: 0.10war → Knochen: 0.10war → weil: 0.10war → er: 0.10war → hungrig: 0.10war → war: 0.10 Leonie gab dem Hund einen Knochen weil er hungrig war Leonie gab dem Hund einen Knochen weil er hungrig war
low
high
Max weight 0.89
Tokens10

You are looking at layer L2, head H1 — pattern: Pronoun→antecedent.

What does er look at? (Top 6) Longest bar = strongest attention.
  • Hund0.89
  • er0.06
  • Leonie0.006
  • gab0.006
  • dem0.006
  • einen0.006

Key takeaways — three things to remember

  1. Attention = weighted sum: (the cocktail-party principle): every word aggregates information from every other word, weighted by a learned similarity between query and key vectors. No fixed rules, no hard-coded grammar — everything emerges from comparing number vectors.
  2. Multi-head means division of labour: different heads learn different relation types because every head has its own Q/K/V projection matrices. One head becomes the pronoun specialist, another spots verb-object bindings, a third watches local neighbourhoods. A model like GPT-4 has 32 to 128 such specialists per layer — and 80 to 100 layers stacked on top of each other.
  3. Deeper layers, more abstract relations: layer 1 is dominated by positional patterns (previous token, local window) — the library does coarse sorting first. Semantic links like pronoun resolution and sense disambiguation only emerge in layer 2 (and from the middle layers onwards in real models).
  4. And why this matters: this one mechanism — Q·K similarity, softmax weighting, weighted sum of values — is the foundation underneath every large language model of the past years. Whoever understands attention understands the engine of ChatGPT, Claude, Gemini and DeepL in principle — and that's more than 95 % of users will ever see.

Did you get it?

  • I can explain what Query, Key and Value are and how they get combined.
  • In a heatmap, I can spot when a pronoun attends to its antecedent.
  • I can describe why several attention heads run in parallel and what each can learn.
TheoryPseudocodeStep by stepFlow diagram

What attention computes

Self-attention lets every token pull weighted information from every other token in the sentence. It does not rely on position or hand-coded rules but on a learned similarity game between three projections:

  • Query (Q): what am I looking for? — the token projected as a "question vector"
  • Key (K): what do I advertise? — projected as a "keyword vector"
  • Value (V): what information would I provide? — the content projection

The formula

Attention(Q, K, V) = softmax( Q · Kᵀ / √d_k ) · V

Step by step: Q · Kᵀ produces an n×n score matrix — entry (i,j) is the dot product between query i and key j, i.e. "how well do they match?". Dividing by √d_k stabilises softmax (dot products grow with dimensionality). Row-wise softmax turns raw scores into proper probabilities (each row sums to 1). Multiplying by V gives every token a weighted sum of all value vectors.

Multi-head — many attentions in parallel

A single attention mechanism can only learn one kind of relation well. Multi-head attention runs h attention computations in parallel, each with its own projection matrices W_Q, W_K, W_V. Their outputs are concatenated and linearly projected. One head may learn pronoun resolution, another verb-object bindings, a third positional patterns.

Properties

  • Parallel: all tokens are processed at once (a big win over RNNs)
  • Distance-independent: a token can interact with the first or the last word equally well
  • Quadratic: complexity O(n²·d) in sequence length — the bottleneck for long contexts
  • Interpretable: attention weights show "where the model looks" (read with care)

Try the demo! Pick a sentence with a pronoun and the pattern "Pronoun → antecedent" (layer 2, head 1). The aha moment is immediate: the pronoun looks at its antecedent, not at some random token.

Test your understanding

Question 1 / 4

What does self-attention compute for every token?

Select one answer
Answer Key: 1) B · 2) A · 3) C · 4) B