Attention is the step that lets a language model connect one part of the text with another. It works like a soft lookup.
Each token carries three short vectors, all computed from its internal state by three different weight matrices. The query says what this token is looking for. The key says what this token can be found by. The value is what this token hands over if it is chosen.
To decide where to look, a token takes the dot product of its query with the key of every token it can see. A large result means “this key matches what I am looking for”. These attention scores are divided by the square root of the head size, which keeps them from growing too large, and softmax turns them into weights that add up to 1. The token’s output is the weighted average of the values.
It is a soft lookup because nothing is chosen outright: every token it can see contributes a share, and a strong match simply gets most of it. In a model that writes left to right, a mask stops each token looking at anything after it.
A model runs many attention heads side by side in every layer, each with its own weights, so different heads can track different kinds of relationship, such as the previous word or an earlier mention of the same word. Stacked over many layers, this is how information moves from one position in the text to another.
Because each new token looks back at every earlier one, attention is also why a model keeps a KV cache, and why longer contexts cost more memory. The explainer runs the formula one query at a time, with every number computed live.