Introduction

Query, Key, and Value (commonly abbreviated QKV) are the three learned vector representations that self-attention computes for every token, forming the actual mechanical building blocks behind the attention calculation covered in the previous topic. While the Self-Attention topic walked through the overall formula, this topic zooms in specifically on what Q, K, and V actually are, how they're computed, why three separate vectors are needed instead of just one, and how they're split and managed across multiple attention heads.

Building genuine intuition for Query, Key, and Value — beyond just memorizing the formula — is what makes self-attention feel less like a mathematical black box and more like a sensible, almost intuitive information-retrieval process happening inside every transformer layer.

Why Do Query, Key, and Value Matter?

Understanding QKV helps to:

  • Build concrete intuition for what self-attention is actually computing
  • Explain why three separate vectors, not just one, are needed per token
  • Clarify how the same token can play different "roles" depending on whether it's acting as a Query or a Key
  • Provide the mechanical foundation for understanding multi-head attention
  • Support deeper technical understanding of transformer internals and interpretability research
  • Connect the abstract attention formula to an intuitive real-world analogy

The Information Retrieval Analogy

Whiteboard
Whiteboard diagram


A helpful analogy: think of a database or search engine.


- Query: the search term you type in — "what am I looking for?"
- Key: a label or index attached to each item in the database —
  "what does this item claim to be about?"
- Value: the actual content of each item — "what will I actually
  get back if this item is chosen as relevant?"

In a normal search engine, you'd typically retrieve just the
single best match. Self-attention instead computes a WEIGHTED
combination of every item's Value, weighted by how well each
item's Key matched the Query — nothing is fully excluded,
but more relevant information contributes more strongly.

Computing Q, K, and V From a Token

Every token starts as a single embedding vector (plus positional
encoding). Three separate learned weight matrices transform
this one vector into three different vectors:

Query  = Embedding × W_Q
Key    = Embedding × W_K
Value  = Embedding × W_V

W_Q, W_K, and W_V are learned during training — they are NOT
the same matrix, meaning the same token embedding produces
three genuinely different vectors, each optimized for a
different role in the attention calculation.

Why the Same Token Needs Three Different Vectors

It might seem redundant to compute three separate vectors
from the same starting embedding — why not just compare
embeddings directly?

The answer: a token needs to play different ROLES depending
on context:

- As a Query: "What information am I trying to gather from
  the rest of the sequence right now?"
- As a Key: "How should other tokens recognize me as
  potentially relevant to THEM?"
- As a Value: "What information do I actually contribute
  once I've been identified as relevant?"

These are three distinct jobs, and separating them into
different learned projections gives the model far more
flexibility than forcing one single vector to serve all
three purposes at once.

A Concrete Worked Example (Simplified)

Sentence: "Dogs chase cats"
(Using tiny, illustrative 2-dimensional vectors for simplicity)

Embeddings (illustrative):
"Dogs" → [1.0, 0.2]
"chase" → [0.3, 0.9]
"cats" → [0.8, 0.1]

After applying W_Q, W_K, W_V (illustrative results):
Query("chase")  = [0.5, 0.7]
Key("Dogs")     = [0.9, 0.1]
Key("chase")    = [0.4, 0.6]
Key("cats")     = [0.85, 0.15]
Value("Dogs")   = [1.2, 0.3]
Value("chase")  = [0.5, 0.5]
Value("cats")   = [0.9, 0.4]

Comparing Query("chase") against each Key produces relevance
scores — in this illustrative case, "chase" might attend
strongly to both "Dogs" (the subject doing the chasing) and
"cats" (the object being chased), producing a new representation
for "chase" built from a weighted mix of both Values.

QKV Across Multiple Attention Heads

Rather than using one large Q, K, V per token, multi-head
attention splits each of Q, K, and V into several smaller
sections — one per "head" — allowing each head to learn to
focus on different types of relationships independently.

If a model uses embedding dimension 768 with 12 attention heads:
Each head works with Q, K, V vectors of dimension 768 / 12 = 64

Each head computes its own independent attention using its
own slice of Q, K, and V, and all heads' outputs are
concatenated back together at the end.

Q, K, and V: Side-by-Side Comparison

VectorQuestion It AnswersComputed FromRole in Attention Formula
Query (Q)"What am I looking for?"Current token's embedding × W_QCompared against every token's Key
Key (K)"What do I offer/represent?"Every token's embedding × W_KCompared against the current token's Query
Value (V)"What information do I contribute?"Every token's embedding × W_VCombined (weighted) into the final output

Same-Sequence QKV vs Cross-Sequence QKV

AspectSelf-Attention (Same Sequence)Cross-Attention (Different Sequences)
Query SourceCurrent sequenceDecoder's current sequence
Key/Value SourceSame sequence as QueryA different sequence (e.g., encoder output)
ExampleGPT relating tokens within its own generated textT5's decoder attending to its encoder's output

(This builds directly on the Self-Attention and Encoder & Decoder topics.)

Key Properties of Query, Key, and Value

  • Q, K, and V are computed from the same token embedding using three separate, independently learned weight matrices.
  • Query represents what a token is "looking for"; Key represents what a token "offers"; Value represents the actual content contributed.
  • Separating these roles gives the model far more flexibility than using a single shared vector for all three purposes.
  • In multi-head attention, Q, K, and V are split into smaller sections, letting each head learn different relationship patterns.
  • The same underlying QKV mechanism applies to both self-attention (same sequence) and cross-attention (different sequences).

Where Does Understanding QKV Matter Most?

ContextRelevance
Transformer Interpretability ResearchAnalyzing learned Q/K/V patterns to understand model behavior
Custom Attention Variant DesignModifying or optimizing attention mechanisms requires understanding QKV roles
Debugging Model BehaviorUnderstanding which tokens attend to which helps explain certain outputs
Educational Deep DivesEssential for truly understanding, not just using, transformer models
Efficient Attention ResearchMany optimization techniques specifically target how Q, K, V are computed or compared

Advantages

  • Separating Query, Key, and Value roles gives the model expressive flexibility beyond a single shared representation
  • The information-retrieval analogy makes an otherwise abstract mechanism intuitive to reason about
  • Multi-head splitting of QKV allows multiple relationship types to be captured simultaneously
  • The same QKV framework generalizes cleanly to both self-attention and cross-attention
  • Learned entirely from data, without requiring manually engineered relevance rules

Limitations

  • Computing and comparing Q against K for every token pair contributes to attention's quadratic computational cost
  • Three separate weight matrices per layer (times the number of heads) add meaningfully to total parameter count
  • Individual Q/K/V vectors, especially in higher dimensions, aren't always straightforward to interpret directly
  • Some attention heads can learn less useful or partially redundant patterns
  • Understanding QKV conceptually doesn't automatically explain everything about a model's overall reasoning

Real-World Examples

ApplicationQKV Mechanism in Action
Pronoun ResolutionQuery from a pronoun token matching strongly with the Key of its referent
Translation (Cross-Attention)Decoder's Query attending to the encoder's Keys/Values for aligned source words
Code CompletionQuery for the current code position attending to Keys of relevant earlier variable definitions
Long-Document QAQuery from a question attending to Keys of the most relevant passage in a long document
Attention Visualization ToolsDirectly displaying learned Query-Key relevance scores for interpretability

Best Practices

  • Use the information-retrieval analogy (Query, Key, Value like a search system) to build lasting intuition.
  • Remember that Q, K, and V come from three separate learned projections, not the raw embedding directly.
  • Study multi-head splitting to understand how QKV supports capturing multiple relationship types at once.
  • Distinguish self-attention's same-sequence QKV from cross-attention's different-sequence QKV.
  • Use QKV understanding as a stepping stone toward more advanced topics like attention visualization and efficient attention variants.

Interview Tip

A common interview question is:

"Why does self-attention use three separate vectors — Query, Key, and Value — instead of just comparing token embeddings directly?"

A strong answer is:

Using three separate, independently learned projections allows each token to play distinct roles depending on the situation: the Query represents what a token is currently looking for, the Key represents how a token signals its own relevance to others, and the Value represents the actual content a token contributes once selected as relevant. If the model instead compared raw embeddings directly, a token would be forced to use the exact same representation for all three purposes, which is far less flexible — separating these roles lets the model learn much richer, more nuanced patterns of relevance and information flow between tokens.

Explaining the flexibility gained from separating the three roles makes your answer stronger.

Conclusion

Query, Key, and Value provide the concrete mechanical foundation beneath the self-attention formula, with each vector serving a distinct, learned role inspired by an intuitive information-retrieval process. Understanding QKV at this level of detail — how they're computed, why three separate vectors are needed, and how they're split across multiple heads — completes a genuinely deep, mechanical understanding of how every transformer-based model, from BERT to GPT to T5, actually processes and relates information.