Volume 1 / Part 3 Mechanics — What Happens Inside / Chapter 29
Attention — The Mechanism That Decides Where to Look in a Sentence
When you read a long document, you instinctively know which words deserve your attention. Something similar happens inside an AI model. This time we'll look at Attention, the mechanism at the heart of the Transformer, through three angles: Query, Key, Value, and something called multi-head attention.
Attention Is the Mechanism That Chooses Where to Look
When you read a long document, you instinctively know which words deserve your attention. Think about skimming meeting notes: your eyes snag on dates and names, but you glide right past the word "however." Something similar happens inside an AI model, and the mechanism responsible for it is called Attention.
In the previous article, we talked about how "similar meaning" really means "nearby coordinates." Once a word is converted into a vector, words with similar meanings cluster together in that coordinate space. But being nearby in space doesn't tell you which word actually matters for the task at hand right now. You need a separate calculation that picks out the important parts of the input.
The 2017 paper "Attention Is All You Need" defines Attention as a function that takes a query along with a set of key-value pairs and produces an output. That output is a weighted sum of the values, and each weight comes from how well the query matches its corresponding key. In other words, the model doesn't look at everything equally. It places more weight on the parts that matter most.
Attention is a computation that decides, in numerical terms, where to place weight within the input.
Query, Key, and Value — Think of Them as Question, Headline, and Content
Three ingredients show up in every Attention calculation: Query, Key, and Value. Here's a mental shortcut: imagine you're searching for a book in a library. What you want to know is the Query. The titles and headings on the spines of the books are the Keys. And the actual text inside the book is the Value. This isn't language from the original paper — it's just a teaching analogy — but it's a handy way to grasp what each piece is doing.
In the actual math, you take the dot product of the Query and the Key, divide that number by the square root of the dimension size, and run it through a function called softmax. This operation hands bigger weights to Keys that are more relevant. Finally, you use those weights to combine the Values, producing an output that bundles together the information worth paying attention to.
Back to the library: the more a book's title matches what you're looking for, the more often you'd reach for it. Books with unrelated titles barely get touched. This idea — adjusting weight according to relevance — is the whole heart of Attention. So how does this help with something like figuring out what the word "it" refers to in a sentence?
Query equals what you want to know, Key equals the headline, Value equals the content — this framing makes the math much easier to picture.
How Self-Attention Figures Out What 'It' Refers To
The mechanism that calculates how one word in a sentence relates to every other word in that same sentence is called Self-Attention. The paper explains that in a self-attention layer, the keys, values, and queries all come from the same place — typically the output of the previous layer. In other words, the model isn't borrowing information from outside the sentence. It's working out relationships entirely among the tokens already present.
This property turns out to be especially useful for resolving what a pronoun points to. The appendix of the paper includes visualizations of Attention patterns from a trained model. One of these figures shows Attention for a pronoun-like word concentrating sharply on one particular word elsewhere in the sentence. In other words, a well-trained model naturally learns to focus its weight on whatever word a pronoun refers to.
The same appendix includes another figure showing multiple heads tracking the connection between a verb and a phrase located far away in the sentence. The reason Self-Attention can find relationships between distant words is that it computes every possible pair of words at once. In theory, it doesn't matter how far apart the beginning and end of a sentence are — the computational effort stays the same. That said, this "look at everything simultaneously" approach comes with a cost, which we'll get to shortly.
Because Self-Attention computes relationships among all tokens in the same sentence, it can connect a pronoun to a noun even when they're far apart.
Multi-Head: Watching Several Perspectives at Once
Everything so far describes a single Query-Key-Value calculation. But real Transformers run this calculation many times in parallel. This is called multi-head attention. The base model in the original paper uses h=8 heads, with each head's dimension scaled down to 64.
Picture eight readers going through the same document simultaneously, each taking notes from a different angle. One tracks grammatical structure, another tracks semantic closeness, a third tracks which pronoun points to which noun, and so on. Because each head's dimension is kept small, running eight heads in parallel costs roughly the same as a single full-dimension head would.
Does that mean more heads are always better? Not according to the paper's experiments. Dropping down to a single head cost 0.9 points of BLEU — the standard metric for translation quality — compared to the best configuration. But the experiments also showed that piling on too many heads hurts quality too. It's not simply "more is better." Finding the right number of perspectives was itself part of the design work.
Multi-head attention is a way of watching several perspectives in parallel. The original paper uses 8 heads, but more heads isn't automatically better.
The Cost: Computation That Scales with the Square of Sentence Length
Self-Attention has real advantages, but it comes with a cost you can't ignore: computation. According to Table 1 in the paper, a self-attention layer's computational complexity is O(n²·d), where n is the sequence length and d is the representation dimension. That means if a sentence doubles in length, the computation roughly quadruples.
For comparison, the paper lists the complexity of a Recurrent layer — the older, sequential-style mechanism — as O(n·d²). The paper notes that when sequence length n is smaller than the representation dimension d, Self-Attention comes out ahead on computational efficiency. For the word-piece and byte-pair tokenization schemes commonly used in machine translation, this condition usually holds.
Here's something worth trying yourself: take a piece of text, double its length, and estimate in your head how much the Attention computation grows. That squared growth becomes a real burden once you're working with long documents or long stretches of code. Keep in mind, though, that this is the theoretical cost for a single layer — actual implementations and hardware optimizations can change how the real-world load scales.
Self-Attention's computational cost grows with the square of sentence length, so longer text means a heavier load.
FAQ
What's the difference between Query, Key, and Value?
As a teaching analogy: Query is what you want to know, Key is the heading you match against, and Value is the actual content you retrieve. The original paper defines Attention as a function that computes an output from a query along with a set of key-value pairs.
What is Self-Attention?
It's the mechanism that computes relationships among tokens within the same sentence or sequence. The paper explains that the keys, values, and queries all come from the same source, such as the output of the previous layer.
Why does multi-head attention need multiple heads?
Because a single perspective misses things. In the paper's experiments, dropping to a single head cost 0.9 points of BLEU compared to the best configuration.
What problems come from Attention's computational cost?
As sentences get longer, the computation grows with the square of the length, which makes processing long text increasingly demanding. Table 1 in the original paper shows this as O(n²·d).
Next time, we'll pull together everything we've covered, including Attention, and trace the entire Transformer architecture in a single diagram.
Primary sources
- Attention Is All You Need (arXiv PDF) — Source for the Q/K/V definitions, scaled dot-product attention, the self-attention definition, and the appendix visualizations
- Attention Is All You Need (NeurIPS PDF) — Source for the Table 1 complexity comparison and the multi-head settings (h=8, d_k=d_v=64)
- Attention Is All You Need (arXiv HTML, v7) — Source for the Table 3 head-count experiment results (BLEU difference)
This article organizes general knowledge based on our experience and public sources. Product specs, prices, and regulations reflect the time of writing (October 6, 2026) and may change.