Skip to main content
Lab Grimoire
TW EN
Coffee
Three Letters That Power Modern AI — What Q, K, and V Actually Do
LLM Internals

Three Letters That Power Modern AI — What Q, K, and V Actually Do

On this page

ChatGPT, Claude, Gemini — every modern AI runs on a Transformer at its core, and the soul of the Transformer is the attention mechanism. It uses three matrices — Query, Key, and Value — to teach AI which words relate to which other words. Sound like abstract math? The underlying logic is identical to how you search for a book in a library.


The Library Analogy

Imagine walking into an enormous library, looking for materials on protein folding.

The question forming in your mind — "Where's the information on protein folding?" — that's the Query.

The titles and keywords on each book's spine — "Molecular Biology," "The Joy of Cooking," "Structural Bioinformatics" — those are the Keys.

The actual contents inside each book — diagrams, formulas, experimental data — that's the Value.

What you do is: match your Query against every book's Key, identify the most relevant ones, then blend their Values together to get your answer.

The attention mechanism does exactly this — just with vector dot products instead of text matching.


Walking Through One Sentence

Take "I love AI" as an example. When an AI processes this sentence, each word simultaneously plays three roles:

  • When "AI" wants to know which other words are most related to it → it becomes the Query
  • When other words are deciding how important "AI" is → "AI" becomes a Key
  • When "AI" gets selected and needs to contribute its information → it becomes a Value

How does the math work? Each word is first converted into a numerical vector (an embedding), then multiplied by three separate weight matrices to produce Q, K, and V vectors. These weight matrices are learned by the model during training.


Attention Scores: Who Relates to Whom?

Next, the AI calculates the "relevance" between every pair of words. The method is intuitive: take one word's Query vector and dot-product it with another word's Key vector. The larger the dot product, the more related those words are.

There's a subtle problem here: in high-dimensional spaces, dot products can blow up to very large values, which sends extreme distributions through the softmax function — one word ends up monopolizing all the attention. The fix is to divide by the square root of the dimension — that's where the √d_k term in the formula comes from.

After dividing, apply softmax row-by-row to convert scores into probabilities. Now each word's attention weights over all other words sum to exactly 1.


The Final Step: Weighted Blending

With the attention weights in hand, the AI computes a weighted average over all the Value vectors.

The result? Each word's output no longer represents just itself — it represents "itself, redefined by context."

The word "bank" appearing next to "river bank" will have its output vector pulled toward the "riverbank" meaning. The exact same word next to "bank account" will produce output pulled toward "financial institution." The attention mechanism lets every word dynamically adjust its meaning based on its neighbors.


Why This Design Succeeded So Completely

Before attention, the dominant approach to language AI was RNNs (Recurrent Neural Networks), which processed text sequentially — one word at a time, like reading a book from front to back. The problem: by the time the model reached the end of a sentence, information from the beginning had already faded.

Attention breaks this constraint entirely. Every word in the sentence can "see" every other word directly, regardless of distance. It's like having an index — you don't need to flip through the book page by page to find a concept; you jump straight there.

This is why Transformers can parallelize computation, handle extremely long text, and outperform every previous architecture on nearly every language task.


From Three Matrices to Reshaping the World

When Google published "Attention Is All You Need" in 2017, few predicted that this mechanism built from Q, K, and V matrices would reshape the entire technology industry within a decade.

Now you know: every time you talk to an AI, countless Queries are searching for matching Keys, then blending the corresponding Values into an answer. This process happens billions of times per second, forming the most powerful language understanding engine humanity has ever built.

Three letters. One formula. The way humans and machines communicate was never the same again.


References

  1. Shekhar, A. (2026). Math behind Attention - Q, K, and V. Blog.
  2. Vaswani, A. et al. (2017). Attention Is All You Need. NeurIPS 2017.
  3. Jay Alammar (2018). The Illustrated Transformer. jalammar.github.io.

Found this useful?

Follow for new AI × biomedical research notes:

Or buy me a coffee to keep new content coming.

☕ Buy Me a Coffee