presentsUNAIVERSE
The UN AI timeline

Learn AI /Transformers and Attention /Query, key and value

Query, key and value

Also called: query, key, value, QKV.

Detail

In five seconds

What it is

Three vectors produced by learned projections represent matching needs, matching features, and information that attention may pass onward.

In plain words

The model derives three vectors from each token representation: one used to seek matches, one used for matching, and one containing information that may be combined.

An everyday example

A team receives hundreds of short survey comments from people in Nairobi, Lima and Sarajevo. For each comment, the team writes one short note about what issue they are trying to identify, one label card listing the issues the comment might relate to, and one fuller summary card with the useful details from that comment. To group comments, they compare the issue note from one comment with the label cards on all comments. Comments with stronger matches contribute more of their summary cards to the final grouped view.

Where the story stops working

In the analogy, people write readable notes and labels on purpose. In a transformer, the vectors are lists of numbers learned during training, and humans usually cannot read a single dimension as a clear label. Also, the real mechanism works in many layers and heads at once, and it combines values numerically rather than physically sorting cards into piles.

Remember thisA query tests what the current position should match, keys determine the strength of each match, and values supply the information blended into the contextual output.

Understand it

Understand it

Why it matters

If you hear that a transformer uses attention, query, key and value are the parts that make that mechanism work. Knowing their roles helps you judge system behaviour more clearly. You can ask better questions: what is being matched, what information is being passed forward, and why might the model focus on the wrong context? This matters when reading model cards, debugging failures, comparing architectures, or deciding whether a system is suitable for tasks like summarisation, translation or image analysis. It also helps separate a real mechanism from marketing language: the model is not searching a database in a human sense, but computing weighted combinations of internal vectors.

How query, key and value produce contextual output

After viewing this, a reader should be able to explain that attention compares queries with keys to get weights, then uses those weights to mix values.

  1. 01

    1. Start from token representations

    Each token already has a vector representation from the model. A small learned linear transform turns that representation into three versions: Q, K and V.

    Draw four token rectangles in one row. Under each, show three small stacked boxes labelled Q, K and V. Use one colour family per token and consistent labels.

  2. 02

    2. The current token forms a query

    For the token being updated, the model uses its query as a request for relevant context.

    Highlight the first token as the current token. Emphasise its Q box with a thicker outline. Fade the Q boxes of the other tokens slightly.

  3. 03

    3. Compare query with keys

    The current query is compared with every token’s key, including often its own key. These comparisons produce attention scores: higher means a closer match for this head and layer.

    Draw arrows from the highlighted Q box to every K box. Place a score circle on each arrow with example labels such as 0.1, 0.6, 0.2, 0.1.

  4. 04

    4. Use scores to weight values

    The scores become weights. Tokens with larger weights contribute more of their value vectors.

    From each score circle, draw an arrow to the matching V box, then onward toward a merge area. Make thicker arrows for larger scores.

  5. 05

    5. Combine values into output

    The weighted values are added together to form one contextual output vector for the current token.

    Merge all weighted V arrows into one output rectangle labelled contextual output for current token. Add a note that this output is passed to later layers or steps.

Show one current token on the left and several context tokens on the right. Each token splits into Q, K and V boxes. Arrows go from the current token’s Q to all K boxes, then to score circles, then to weighted arrows into the V boxes, and finally to one combined output box. Read left to right.

On hover or tap, each arrow from Q to K highlights its score and the matching contribution from V to the output; a toggle switches between numeric example weights and plain-language labels such as low, medium and high.

Describe this without the diagram

The diagram explains attention for one token. First, each token has three derived vectors called query, key and value. Second, the current token’s query is compared with every token’s key to produce scores. Third, those scores are used as weights on the corresponding value vectors. Fourth, the weighted value vectors are combined into one output vector for the current token. The key idea is that keys are used for matching, while values are used for the information that gets mixed into the output.

With reduced motion: A static left-to-right diagram shows token vectors split into Q, K and V, then arrows from one query to all keys, score labels above those arrows, thicker weighted arrows from values, and one final combined output box.

In a transformer, each token first has a vector, which is a list of numbers representing features. From that token representation, the model creates three different versions: a query, a key and a value. They are related, but they do different jobs.

The query represents features used to seek relevant context. The key represents features used for matching. The value represents information that may be passed onward. The model compares one token’s query with other tokens’ keys to produce compatibility scores. A normalising step turns these scores into attention weights.

Those weights are then used to mix the value vectors. So the output is not the key or the value from one place only. It is usually a weighted combination of many values, with larger weights on tokens whose keys matched the query more strongly. This is how a token representation incorporates context from other tokens.

These three vectors are often confused with search systems because the words sound similar. But here they are internal projections inside the model, not database fields. They are also not fixed meanings attached to words forever. Their numbers depend on the learned projections, the layer, the attention head and the input representation.

How “Sorting survey comments by the issue they raise” maps onto the real thing

In the storyIn the AI system
issue note for a commentquery vector
label card on each commentkey vector
fuller summary cardvalue vector
stronger or weaker match between note and label cardattention score used as a weight

A worked example

While checking a pilot’s draft lessons-learned note, an evaluation officer examines how one attention head updates “it” in the simplified token sequence: “The pilot revised the workplan because it was incomplete.”

Starting from: Illustrative attention weights from “it” to every token: The 0.03, pilot 0.10, revised 0.05, the 0.02, workplan 0.55, because 0.05, it 0.05, was 0.05, incomplete 0.10.

  1. The head projects the representation of “it” into a query. It separately projects every token’s representation into a key and a value using learned matrices.
  2. It compares the query for “it” with every key, scales the resulting compatibility scores and applies softmax, producing weights that sum to 1.
  3. The key for “workplan” receives the largest weight, 0.55. This controls how much of the corresponding value vector enters the output, but it does not mean the key and value vectors are identical.
  4. The contextual output is the weighted sum of all value vectors: 0.03×V(The) + 0.10×V(pilot) + 0.05×V(revised) + 0.02×V(the) + 0.55×V(workplan) + 0.05×V(because) + 0.05×V(it) + 0.05×V(was) + 0.10×V(incomplete).

Result: For this attention head, the value vector at “workplan” makes the largest contribution to the updated representation of “it”. Other heads may attend differently, and later layers combine those outputs before the model produces any text.

In a United Nations workplace

An evaluation officer at a Geneva duty station is finalising the lessons-learned note from a pilot before close of business. Progress reports arrived in Arabic, English and French. In an approved multilingual model, each token produces query, key and value vectors. A query from a pronoun, for example, may match strongly with the key of an earlier noun, causing that noun’s value vector to contribute more to the pronoun’s contextual representation.

The mechanism helps the evaluation officer understand how the model connects related words across a sentence when summarising findings from reports in several languages. It also explains why attention is not a database lookup: learned similarities determine a weighted blend of value vectors.

Before you try thisAttention weights do not prove why the model produced a conclusion and should not be treated as an evaluation finding. Raw interview notes, names or details that could identify participants must not be entered into a tool unless it is authorised for that material, because evaluation records may contain confidential personal information. Outputs still require review against the source reports.

Where you may hear it

  • A vendor demo to an ICT governance board
  • A technical review of model documentation after a pilot
  • A procurement meeting comparing transformer-based tools
  • A briefing with the evaluation office on model interpretability

Avoid confusion

Avoid confusion

Often confused with

Common misconceptions

  • Common misconception: Query, key and value are three different kinds of data stored in a model like fields in a database.

    Correction: They are not database columns. They are learned vector projections created from token representations inside an attention layer. Their job is computational: queries are used for matching, keys for being matched against, and values for providing the content that gets combined.

  • Common misconception: The model picks one value from one token and copies it into the output.

    Correction: Usually it does not make a single hard choice. Attention normally produces a weighted mixture of many value vectors. One token may dominate, but several tokens can contribute at the same time, which is why the output can reflect blended context.

  • Common misconception: A key stores meaning and a value stores facts, so they are stable across all contexts.

    Correction: Keys and values are context-dependent internal representations, not permanent labelled containers. Their exact numbers depend on the layer, the attention head, the surrounding tokens and the learned weights. A token can produce different useful patterns in different contexts.

  • Common misconception: Query, key and value only matter in text models.

    Correction: The same mechanism appears in many transformer-based systems, including language, vision, speech and multimodal models. The items being related may be words, image patches, audio frames or other units, but the matching-and-mixing idea is similar.

Quick check

In an attention head, what is the main role of the value vector?

Go deeper

Go deeper

The simple version

The model derives three vectors from each token representation: one used to seek matches, one used for matching, and one containing information that may be combined.

The technical version

Given an input representation x for each token, an attention head applies three learned linear projections to produce q = xW_Q, k = xW_K and v = xW_V. For one token position, its query is compared with all keys, often by a dot product, to produce raw compatibility scores. After scaling and a normalising step such as softmax, these become attention weights. The head then computes a weighted sum of the value vectors using those weights, yielding a contextualised output for that position. In self-attention, q, k and v all come from the same sequence; in cross-attention, the query comes from one sequence or stream while keys and values come from another. The important distinction is functional: keys determine matching, values determine what information is aggregated.

Reviewed external resources

  • EncyclopaediaAttention (machine learning) (opens in a new tab)Wikipedia

    This link gives a broad overview of attention methods in machine learning and suits readers who want a general reference before focusing on QKV.

    Intermediatelink checked 2026-07-31

  • ArticleAttention Is All You Need (opens in a new tab)Vaswani et al., 2017

    This link is the original transformer paper and suits readers who want the formal source for how attention and QKV are defined in the architecture.

    Deeperlink checked 2026-07-31

  • ArticleThe Illustrated Transformer (opens in a new tab)Jay Alammar

    This link gives a visual walk-through of transformer components and suits readers who learn best from diagrams and intuitive explanations.

    Intermediatelink checked 2026-07-31

  • VideoVideo explanations: Query, key and value (opens in a new tab)YouTube search

    This link is a search results page for videos about query, key and value, which suits readers who want to compare several teaching styles rather than rely on one explanation.

    search results, not one pagelink checked 2026-07-31

  • DiscussionCommunity discussion: Query, key and value (opens in a new tab)Reddit search

    This link is a search results page for community discussions about query, key and value, which suits readers who want informal explanations, questions and points of confusion from practitioners.

    search results, not one pagelink checked 2026-07-31

Where this came from

Drafted with a language model through an offline pipeline, checked against this site's schema, and every external link above fetched to see whether it answers. Treat it as a starting point and check anything that matters against the primary sources. How this site is built and what that means.

The one thing to remember

A query tests what the current position should match, keys determine the strength of each match, and values supply the information blended into the contextual output.