Skip to content

Plain-language explainer

Query, Key and Value, explained

What do Query, Key and Value actually mean in attention?

Attention is a search. Each token asks a question, its Query. Each token also advertises what it contains, its Key, and carries the content it would hand over, its Value. Matching a Query against every Key produces scores, softmax turns the scores into shares, and the token's new representation is the share-weighted blend of the Values. All three roles are computed from the same token vector through learned weight matrices. Nobody writes them; training finds them.

Last reviewed

Reading is the slow way. Start with a free lesson you can operate right now.

Start free: How attention blends meaning

Free, no code, no signup.

Then go deeper: Query, Key, Value Locked

What people get wrong

  • Q, K and V are stored properties of words. They are produced fresh, per position and per layer, by learned matrices multiplying the token's vector.
  • The model looks words up in a fixed table. Scores are computed from the live context, which is why the same word attends differently in every sentence.
  • Attention chooses the answer. It only decides which information flows where. Scoring the next word happens later, at the top of the stack.

Where you see it in real products

  • Attention heatmaps in AI visualizations are these Query-Key scores drawn as color.
  • The reason 'bank' resolves to river or money per sentence is this per-context matching.
  • Long-document question answering works because Queries can fish the one relevant passage out of thousands of Keys.

Common questions

Where do query, key and value come from?
All three are made from the same token vector, multiplied by three separate weight matrices that were learned during training. Nobody writes them by hand. The model discovers, over billions of examples, which projections make a useful question, a useful label, and useful content to pass on.
What is the difference between a key and a value?
The key is what a token advertises so others can decide whether to look at it. The value is what they actually receive when they do. Splitting the two lets a token be easy to find for one reason and contribute something else entirely.
Why divide the scores by the square root of the dimension?
Dot products of long vectors grow large, and large scores make softmax collapse onto a single token, which stalls learning. Dividing by the square root of the key dimension keeps scores in a range where the gradients stay useful. It is numerical hygiene, not a modelling idea.

Related explainers

More in Inside the transformer

One idea at a time, in your inbox

New lessons and explainers, written the way these pages are. Now and then, not daily, and never a sales sequence.

We email you a confirmation link first. Unsubscribe in one click, any time. Privacy.

Part of See How AI Works, a free interactive course, where you learn how modern AI works by operating it, not watching videos.