Attention internals · Query, Key, Value

Seek. Show. Give.

Every token in a transformer is projected into three vectors that play three roles. Its Query says what it's looking for. Its Key advertises what it is, so others can match against it. Its Value is the content it hands over when attended to. Attention is just a soft lookup built from these three.

00 Start here — a picture, before any math

Working the room.

Picture a packed industry mixer: a few hundred people, one evening, and you walked in after something specific. Everything attention does has a face here — and the three roles fall out before a single vector appears.

Query
your goal tonight — what you’re hoping to find
Key
your name-tag pitch — the line others scan to size you up
Value
the conversation itself — what someone walks away with after talking to you

You scan everyone’s name-tags against your goal and spend more of your finite night on the better fits — your evening is a budget that adds up to one whole night. What you leave with is a blend of conversations, weighted by the time you gave each. Pick whose night to follow.

the mixer floor · whose night are we watching? illustrative — affinities are hand-set, not computed
follow someone’s evening

No mutual handshake. How much you work the room is your goal against their pitch — a private calculation, run from your side alone.

That’s the whole of attention, with faces on it. Now watch the same three roles become actual vectors. ↓

01 One token, three roles

Q, K and V aren't three tokens. They're three views of the same one.

The most common confusion: people imagine Query, Key and Value are different words. They're not. Each token starts as a single embedding vector x, and the model passes it through three separate learned matrices — WQ, WK, WV — producing three vectors from the one input.

Pick a token to see its embedding split into the three roles.

Three projections of one embedding illustrative 4-dim toy vectors · hover any number
what the four toy dimensions track
pick a token
embedding  x  =  
Q
query
“what am I looking for?”
K
key
“what do I advertise, to be found?”
V
value
“what do I contribute if chosen?”
↑ hover, tap, or focus any number for a plain-language read · the role-colored pill is the slot this vector leans on most

The same vector, seen through three lenses. Nothing here is a different word — it's one word asking, advertising, and offering at once.

02 The match — a dot product

How well does a query line up with a key?

To decide how much one token should attend to another, attention measures how aligned the query is with each key — using a dot product. Point the same way: large, positive. Perpendicular: zero. Opposite: negative. Longer vectors push the score further. Drag the query arrow and watch the match scores move.

q · k for each key  ·  drag the coral arrow 2-dim, for intuition

Softmax later turns these raw scores into attention weights that sum to 1 — so the best-aligned keys win most of the attention.

03 The soft lookup — one query, end to end

Match every key, soften into weights, blend the values.

This is the whole mechanism. Choose which token is doing the looking. Its query is matched against every token's key to get scores; the scores are scaled and softmaxed into attention weights; then the output is the weighted blend of every token's value. Step through it.

self-attention for one query token "the fox chased a rabbit" · toy values
query token:

step 1 · raw match

output context vector

04 Why divide by √dₖ?

Big vectors make big dot products — and softmax panics.

Add up products across many dimensions and the scores grow with the dimension dₖ. Feed huge scores into softmax and it collapses to nearly all-or-nothing: one weight near 1, the rest near 0. That's a saturated softmax with vanishing gradients — hard to train. Dividing by √dₖ keeps the scores in a sane range. Raise the dimension and compare.

attention weights over 6 keys · same vectors, with vs without scaling dₖ = 16

without ÷√dₖ

top weight
spread (entropy)

with ÷√dₖ

top weight
spread (entropy)

Crank dₖ toward 256 on the left: the unscaled distribution spikes into a single bar. On the right, scaling holds the spread roughly steady regardless of dimension.

05 Why three matrices, not one?

Separating the roles is what lets attention point one way.

If query and key shared a single projection, the score matrix would be forced symmetric: every token would attend to another exactly as much as it's attended back. Language isn't like that — a pronoun leans hard on its noun without the noun leaning equally back. Separate WQ and WK buy that asymmetry. Toggle the shared projection and watch the attention map snap symmetric.

attention score map · rows = query · cols = key brighter = stronger match

Mathematically: with a shared projection the scores become xi(WWᵀ)xj, and WWᵀ is symmetric — so Sij = Sji always. Two matrices break that.

06 The mental model

It's a fuzzy dictionary lookup.

A normal dictionary takes a key, finds the one matching entry, returns its value. Attention does the same shape of thing, but soft: the query is compared to every key, and instead of one exact hit you get a blend of all the values, weighted by how well each key matched.

RoleIn attentionDictionary analogy
Querywhat this token is looking for (x·WQ)the lookup request
Keyhow each token is matched against (x·WK)the index / labels
Valuethe content blended into the output (x·WV)the entry's contents
Softmaxturns match scores into weights summing to 1fuzzy match, not exact
OutputΣ weightⱼ · valueⱼa weighted blend of all entries

The only learned parts are the three matrices WQ, WK, WV. Training shapes them so that "looking for," "found by," and "offered" line up usefully — and the rest is dot products, a divide, a softmax, and a weighted sum.

One head shown · real models run many

Multi-head attention runs several Q/K/V projections in parallel, each learning a different kind of relationship, then concatenates the results. Everything here is one such head.

Self- vs cross-attention

In self-attention, Q, K and V all come from the same sequence. In cross-attention, the query comes from one sequence and the keys/values from another — same machinery, different sources.