Back to Generative AI Notes
Topic #41

Query, Key, Value in Attention

Query, Key, and Value are the three vectors self-attention computes for every token — the actual mathematical mechanism behind the "which tokens matter to which" weighting described conceptually in Attention.

An Analogy Before the Math

Think of a library search: your Query is what you're looking for. Every book has a Key — a short descriptor of what it's about. You compare your query against every book's key to find good matches, then actually read the Value — the book's real content — weighted by how good each match was. Self-attention does exactly this, but for every token comparing against every other token, simultaneously.

How Q, K, V Are Produced

For each token's input representation (a vector, x):
  Query = x × W_Q   (a learned weight matrix)
  Key   = x × W_K   (a different learned weight matrix)
  Value = x × W_V   (a third learned weight matrix)

W_Q, W_K, W_V are learned during training — the model
discovers what makes a useful "query," "key," and "value"
representation purely from optimizing prediction accuracy.

The Attention Calculation

For a given token's Query, against every token's Key:

  score = Query · Key   (dot product — measures similarity)
  scaled_score = score / sqrt(key_dimension)   (keeps values stable)
  attention_weight = softmax(scaled_scores)     (normalizes to sum to 1)

  output = sum(attention_weight × corresponding Value, for all tokens)

This is the "scaled dot-product attention" formula from the original transformer paper. The softmax step is what turns raw similarity scores into a clean probability-like distribution — tokens with higher relevance get proportionally more weight in the final blended output.

Why the Scaling Step Matters

Without dividing by sqrt(key_dimension), dot-product scores can grow very large as vector dimensions increase, pushing softmax into a regime where it produces extremely peaked (nearly one-hot) outputs — which can hurt training stability. The scaling keeps the values in a more numerically well-behaved range.

Practical Use Case

Understanding Q/K/V is what's needed to actually read transformer research papers or implement attention from scratch — for most application-building work (prompting, RAG, fine-tuning), this level of detail isn't required day-to-day, but it demystifies what's happening "under the hood" and is common ground in more technical interviews.

Common Mistakes

  • Assuming Query, Key, and Value are three separate pieces of input data — they're all derived from the same token representation, just via three different learned transformations
  • Skipping the scaling step when explaining the formula — it's a small but specifically-motivated detail, not an arbitrary addition

Interview Relevance

"Walk me through the scaled dot-product attention formula" is a common question in more technical/research-oriented AI engineering interviews — being able to state the formula and explain why each step exists (not just recite it) is what distinguishes a strong answer.

Practice Question

Explain, in plain language, what would likely go wrong with attention if the scaling step (dividing by sqrt(key_dimension)) were removed.

Want to go beyond the notes?

Join Coding Now Tech Institute's Generative AI course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

Query, Key, Value in Attention – FAQs

Quick answers about learning Query, Key, Value in Attention in Generative AI.

This free note from Coding Now Tech Institute explains Query, Key, Value in Attention in Generative AI — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every Generative AI topic on Coding Now Tech Institute, including Query, Key, Value in Attention, is 100% free with no signup required.
With focused practice, most students grasp Query, Key, Value in Attention in 1–3 days from these notes; pairing it with Coding Now Tech Institute's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the Coding Now Tech Institute Community (/community) — expert instructors answer within 24 hours.
Call NowEnroll Now