Self-Attention: The Mechanism That Makes Transformers Work

LLM foundationsModels and inferencePublished By Simon Budziak

Self-attention is the mechanism that lets every token in a sequence weigh its relevance to every other token. It is how a transformer connects a pronoun to its noun across a paragraph, and the reason the architecture handles long-range context in parallel instead of word by word.

How does self-attention actually work?

Each token’s embedding is projected three ways: into a query (what am I looking for), a key (what do I offer) and a value (what I carry). Every token’s query is scored against every other token’s key, the scores become weights, and each token’s new representation is the weighted blend of the values. Nothing in the mechanism is sequential, every comparison happens at once, which is what let transformers train on orders of magnitude more data than the recurrent models they replaced. “Attention Is All You Need”, the 2017 paper that introduced the transformer architecture, is named for exactly this removal.

What is multi-head attention?

The same trick run several times in parallel with different learned projections. One head might track syntax, another coreference, another position; the model learns the division itself. Concatenated, the heads give each token a composite view of the sequence. The heads are why the mechanism scales in quality, not just in speed: a single attention pattern could not carry grammar, meaning and position at once.

Why does self-attention set the limits of LLMs?

Because its cost grows with the square of the sequence. Doubling the context window roughly quadruples the attention compute, which is why long contexts are priced the way they are and why models can degrade on material buried mid-context even when it technically fits. Most practical context engineering is working around the economics of this one mechanism. Every token you feed the model joins that all-pairs comparison, which is the literal reason trimming context cuts cost.

Where else does self-attention show up?

Anywhere data can be written as a sequence of embeddings. Vision transformers attend over image patches, audio models over sound frames, protein models over amino acids; the mechanism is indifferent to what the tokens mean. That generality is why one architecture took over AI: the field converged on a single trainable way to ask which parts of the input matter to which.

This entry was drafted with AI assistance.

Frequently asked questions

Is self-attention the same as attention?

Self-attention is one kind of attention: a sequence attending to itself. Cross-attention connects two different sequences, such as a decoder reading an encoder's output, and both use the same query-key-value math.

What is self-attention in simple terms?

For each word, the model asks how much every other word in the input matters right now, and blends their information by those weights. Every word does this simultaneously.

Why does self-attention make long contexts expensive?

Because every token compares itself to every other token, cost grows roughly with the square of sequence length, which is why long context windows cost real money and research keeps hunting cheaper variants.

Summarize this page with

Train your team to build this