25 November 2026 · 3 min readInteractive 3D
How does attention work in transformers?
How does a chatbot know that “it” means the cat? Each word scores every earlier word. See the whole table of scores as 3D bars.
In “The cat chased the mouse because it was hungry”, what does “it” mean? You know it is the cat. A language model works this out with attention: each word looks back at every earlier word and decides how much each one matters.
Rows of percentages
Each row is one word looking back. Its bars are percentages that add up to 100%. Pick “it” and the tallest bar is on “cat”. Pick “chased” and attention spreads between “cat” and “mouse”, the chaser and the chased.
Where the numbers come from
- Each word makes a query: what am I looking for?
- Each word also makes a key: what do I contain?
- The dot product of a query and a key gives a score.
- Softmax turns each row of scores into percentages.
The causal mask stops a word looking at words after it, which is how a model writes one word at a time without peeking ahead.
Real models run dozens of these attention tables side by side, called heads, each learning to look for something different.
A worked example
In the sentence “The cat sat because it was tired”, the word “it” should look back at “cat”. Attention gives “it” a score for every earlier word, using the dot product of a query (what “it” is looking for) with each word’s key (what that word offers).
- Scores for it → The, cat, sat, because: 0.1, 2.8, 0.4, 0.2
- Softmax turns them into weights: about 5%, 79%, 7%, 6% (plus a little for “it” itself)
- The new meaning of “it” is mostly the meaning of “cat”
- This is one row of the attention table
Queries, keys and values
Each word’s embedding is multiplied by three learned matrices to get a query, a key and a value. Scores come from queries and keys; the output mixes the values using those scores. Dividing the scores by the square root of the key length keeps them from getting too large before softmax.
Many heads at once
A transformer runs several attention heads in parallel. One head might track which noun a pronoun refers to, another the verb that goes with a subject, another nearby words. Their outputs are combined, and the whole block is repeated dozens of times in a large model.
Attention lets every word look at every other word in one step, which is why transformers handle long sentences better than older models that read one word at a time.
Why it was a breakthrough
Older language models read text one word at a time and had to squeeze everything into a memory that faded over long sentences. Attention lets every word look directly at every other word, and all positions can be computed at once on GPUs. That made training on vast text practical and led to today’s chat models.
Glossary
- Query: what a word is looking for.
- Key: what a word offers to be found by.
- Value: the information a word passes on.
- Head: one independent attention calculation.
Try these yourself
- Q1. Scores 1 and 1: softmax shares? 50% each
- Q2. In “The girl took her bag”, “her” should attend most to? girl
- Q3. What do queries and keys produce? Scores
- Q4. Why divide by the square root of the key length? To keep scores from growing too large
In short
Attention scores every word against every other, turns scores into shares and mixes meanings. Many heads learn different relationships at once.
Keep going
Questions people ask
What is attention in a transformer?
A way for each word to score every other word with dot products, turn the scores into percentages, and mix in information from the words it scores highly.
What are queries, keys and values?
Three versions of each word made by learned matrices: queries ask, keys answer, and values carry the information that gets mixed.
Why do transformers use many attention heads?
Each head can learn a different kind of relationship, such as grammar or meaning, and the model combines them.