Attention helps AI “decide what matters” by calculating, for each token, how much every other available token should contribute to its updated representation. Tokens that score as more relevant receive larger numerical weights, and their information is blended in more strongly [1, 2]. The word decide is shorthand: nothing is concentrating, noticing or choosing. The weights come from learned calculations.
For a language model, this is how words that may be far apart get connected. In the sentence The dog chased the ball because it was moving, the representation of it needs information from the surrounding words to reflect what it most likely refers to. Attention builds that context-sensitive representation instead of treating every token in isolation.
Where Attention Fits in a Transformer
The previous guide, How Does AI Process a Sequence of Words? Transformers Explained Without the Math, described a Transformer as a stack of layers that repeatedly updates a vector for every token. Attention is the step inside each layer where those vectors exchange information. The other parts of the layer then process each token’s updated vector further.
This guide zooms in on that one step: what it calculates, why it uses queries, keys and values, and what it can and cannot tell you about a model.
TL;DR
- Attention calculates how strongly one representation should draw from others [3, 1].
- Queries ask what information is relevant, keys describe what information is available and values carry the information to combine [1, 2].
- Self-attention applies that operation within one sequence [1].
- Multiple attention heads can learn different patterns in parallel, although individual heads do not always have neat human-readable roles [1].
- Attention weights are not guaranteed to explain why a model reached an answer [4].
- Attention is a mathematical mechanism—not awareness, intention or consciousness.
- The context-rich representations attention produces are what a language model uses to predict the next token.
The Basic Idea: Weighted Information
Suppose you search a library catalog for a book about training a puppy. The words in your search are compared with descriptions of the books. Strong matches receive more weight, so the results most relevant to your request rise to the top.
Attention works differently in its technical details, but the analogy captures the central idea: compare a request with available information, then combine the most relevant information more strongly.
Early neural attention systems used this approach in translation. When generating a translated word, the model could place different weights on parts of the source sentence instead of forcing the entire sentence through one fixed representation [3]. Transformers later made attention the central mechanism for processing sequences [1].
Queries, Keys and Values
Most explanations of attention use three terms:
- A query represents what the current position is looking for.
- A key represents what each available position offers for matching.
- A value contains the information that may be passed forward.
The model compares a query with many keys. Those comparisons become attention scores. After the scores are normalized into weights, the model creates a weighted combination of the corresponding values [1, 2].
Think of the query as a request, the keys as catalog labels and the values as the content you receive. The comparison does not retrieve a single winner. Several values can contribute, each by a different amount.
The query, key and value are learned numerical representations. They are not literal questions, labels or facts written inside the model.
A One-Sentence Walkthrough
Consider: Jordan put the glass on the table because it was unsteady.
When the model updates the representation for it, that position can compare its query with keys from Jordan, glass, table, because, unsteady and the other tokens. The resulting weights determine how much information from their values contributes to the updated representation.
The model may draw strongly from table and unsteady. That does not mean it applied a hand-written grammar rule. Training changed the model’s parameters so patterns that helped its objective became more likely.
What Makes It Self-Attention?
It is called self-attention when the queries, keys and values come from the same sequence [1, 2]. Each token can incorporate information from other allowed tokens in that input.
The word bank, for example, should be represented differently in river bank and bank account. Self-attention lets the surrounding tokens change the model’s internal representation of the same word.
Some systems also use cross-attention, in which one sequence attends to representations from another source. A text generator might attend to image features, or a translation decoder might attend to an encoded source sentence.
Why Models Use Masks
Attention does not always have access to every position. A mask can block certain connections.
When a language model is trained to predict the next token, a causal mask prevents each position from seeing future tokens. Otherwise, the model could look at the answer it is supposed to predict. During generation, it can attend to the prompt and tokens already produced, but not to tokens that do not exist yet.
Other attention designs may allow information to move in both directions across the available text. The right pattern depends on the model’s task and architecture.
Why Use Multiple Attention Heads?
Transformers usually run several attention operations in parallel. This is called multi-head attention [1]. Each head has its own learned transformations, allowing the model to represent different relationships in different subspaces.
One head may become sensitive to nearby word relationships while another helps with longer-distance connections. That is a useful intuition, not a promise. A head may participate in several behaviors, and a human-friendly label may oversimplify what it does.
The outputs of the heads are combined and processed by the rest of the layer. Repeating that process across many layers lets the model build increasingly contextual representations.
Does Attention Explain a Model’s Answer?
An attention map can show which positions received higher weights in a particular operation. That can be useful for inspection, but it is not automatically a faithful explanation of the final output.
Research has shown that substantially different attention patterns can sometimes produce similar predictions and that attention weights may not align with other measures of feature importance [4]. A model’s output also depends on values, feed-forward networks, residual connections, other heads and many layers.
So “the model paid attention to this word” should not be treated as proof that the word caused the answer.
Attention Is Not Awareness
The everyday word attention suggests a mind directing its focus. The AI term is narrower. It describes a learned mathematical mapping from queries and key-value pairs to an output [1, 2].
Attention alone does not establish:
- consciousness or self-awareness;
- intention or understanding;
- a complete record of the model’s reasoning;
- factual accuracy;
- perfect use of long inputs.
The mechanism can route information effectively while the overall system still produces an incorrect or misleading answer.
Where This Leads
Attention, repeated across heads and layers, leaves every token with a representation shaped by the rest of the input. In a language model, the representation at the last position is then turned into a score for every possible next token. Why Does AI Predict the Next Token—and How Does That Become an Answer? explains that final step and how repeating it produces a full response. Readers who want to see how different model families build on attention can continue later to the AI Models + Systems chapter.
Practical Takeaways
- Read attention as weighted information routing, not human concentration.
- Remember the simple roles: query asks, key matches, value contributes.
- Self-attention connects information within the same sequence.
- Masks control which positions are allowed to influence one another.
- Treat attention visualizations as clues, not definitive explanations.
- Evaluate a model’s accuracy separately from how its attention is arranged.
Sources
- Attention Is All You Need. arXiv, 2017-06-12, accessed 2026-11-08.
- Transformers. Google for Developers, accessed 2026-11-08.
- Neural Machine Translation by Jointly Learning to Align and Translate. arXiv, 2014-09-01, accessed 2026-11-08.
- Attention is not Explanation. arXiv, 2019-02-26, accessed 2026-11-08.
