What is the attention mechanism in AI?
Attention lets an AI model decide, for each word, which other words matter most, and blend in information from them.
Rung 1 Explain like I'm 6
Imagine your teacher reads out, "The dog chased the ball because it was bouncy." Which thing was bouncy, the dog or the ball? To work it out, you think back over the sentence and pay extra attention to "ball", because balls bounce.
An AI does the same thing with every word. For each word it asks, "Which other words should I look at to understand this one?" It gives each word a score, like a spotlight that is brighter on the words that matter and dimmer on the rest. Then it mixes in what it learned from the bright ones. That spotlight trick is called attention.
Rung 2 High-school level
The attention mechanism is a way for a neural network to focus on the most useful parts of its input. It was first used for translation in 2014: instead of squeezing a whole sentence into one fixed list of numbers, the model could look back at the source words most relevant to each word it was writing [1].
In 2017 the paper "Attention Is All You Need" built a whole model, the transformer, out of attention, with no recurrence at all [2]. Today every large language model uses it.
Here is the idea. Text is split into tokens, and each token becomes a vector of numbers. For each token, the model makes three vectors:
- a query: what am I looking for?
- a key: what do I contain?
- a value: what will I pass on?
Each query is compared with every key to get a score. The scores are turned into weights that add up to 1. The token's new vector is the weighted mix of all the values. When each token attends to the other tokens in the same text, it is called self-attention.
Check yourself: In attention, what are the weights used for?
They decide how much of each other token's value gets mixed into a token's new vector.
Rung 3 Undergraduate level
The attention mechanism formula in transformers is scaled dot-product attention [2]:
Attention(Q, K, V) = softmax(Q Kᵀ / √d_k) V
Q, K and V are the query, key and value matrices, made by multiplying the token vectors by three learned weight matrices. d_k is the key length. Dividing by √d_k stops dot products from growing so large that softmax saturates and its gradients become tiny [2].
Worked example: three tokens, vectors of length 2, computing the output for the third token.
- query q = (1, 0); keys k₁ = (1, 0), k₂ = (0, 1), k₃ = (1, 1)
- dot products q·k: 1, 0, 1
- divide by √2: 0.707, 0, 0.707
- softmax: e^0.707 ≈ 2.03, e^0 = 1, so weights ≈ 2.03/5.06, 1/5.06, 2.03/5.06 = 0.40, 0.20, 0.40
- values v₁ = (1, 0), v₂ = (0, 1), v₃ = (1, 1)
- output = 0.40·(1, 0) + 0.20·(0, 1) + 0.40·(1, 1) = (0.80, 0.60)
Multi-head attention runs several of these in parallel with smaller vectors, each head free to learn a different relation, then joins the results. The 2017 base model used 8 heads of size 64 [2].
Two variants matter in an LLM. In causal (masked) attention, a token may only look at earlier tokens, so text can be generated left to right. In cross-attention, queries come from one sequence and keys and values from another, as in the original translation model [2].
Check yourself: Why is the dot product divided by √d_k before softmax?
Without it, large dot products push softmax into a region with tiny gradients, which slows learning.
Rung 4 Graduate level
Dot-product vs additive. The 2014 attention scored pairs with a small neural network (additive attention) [1]. The transformer used dot products because they run as fast matrix multiplies [2]. The 2017 paper notes additive attention beats unscaled dot-product attention for large d_k, which is why the scaling exists [2].
Cost. For n tokens, the score matrix QKᵀ has n² entries per head per layer. Compute grows as n²·d, and storing the scores naively is worse: at 16-bit precision one 128K-token matrix (131,072² entries) is about 34 GB, for one head in one layer. This quadratic cost is the main limit on context window length.
Making it fast. FlashAttention (2022) computes exact attention in tiles held in on-chip SRAM, never writing the full matrix to GPU memory. It reported 3× faster GPT-2 training at 1K tokens and 2.4× on Long-Range Arena (1K-4K tokens) [3]. FlashAttention-3 (2024) reached up to 740 TFLOPS in FP16 on an H100, about 75% of peak, and close to 1.2 PFLOPS in FP8 [4].
Serving. During generation the keys and values of past tokens are kept in a KV cache, so memory grows linearly with context. Sharing keys and values across heads (grouped-query attention) shrinks it; the KV cache page works through the numbers.
Cheaper variants:
- Sparse attention: each token attends to a chosen subset, not all n.
- Linear attention: replaces softmax with a form that keeps a fixed-size running state, so cost grows linearly, at some loss of exact recall.
- Hybrids: mix a few full-attention layers with cheap ones.
Rung 5 PhD level
IO complexity. On GPUs, attention is limited by memory traffic more than arithmetic. FlashAttention's analysis shows standard attention needs Ω(Nd + N²) memory accesses, while its tiled version needs O(N²d²/M) for SRAM size M, and no exact algorithm can do asymptotically better over all SRAM sizes [3]. The open question is how far this approach carries as matrix units outpace memory and softmax's exponentials become a bottleneck of their own [4].
What softmax does, and does badly. Weights must sum to 1, so a head with nothing useful to attend to still has to put its weight somewhere. Models learn to dump it on early tokens, an "attention sink". Qiu et al. (2025) showed that a head-specific sigmoid gate after attention, tested on 1.7B dense and 15B mixture-of-experts models trained on up to 3.5T tokens, improved quality and stability, reduced attention sinks and helped long-context extrapolation [5]. They trace the gain to added non-linearity and sparse, query-dependent gating [5].
Recall vs efficiency. Linear attention and state-space layers compress the past into a fixed-size state. That is cheap, but the state cannot store every token, so exact lookups suffer. Sparse attention keeps exact access but must guess which tokens matter; a wrong guess loses information silently.
Open problems:
- Theory of sparsity: when can a learned selector match full attention, and with what guarantees?
- Interpretability: attention weights show where a head looked, not why the model answered; they are not a full explanation.
- Length generalisation: attention patterns trained at one length often break at longer ones.
- Hardware co-design: whether new attention variants can reach the utilisation that dense attention kernels get [4].
Check yourself: Why can a softmax attention head not simply "attend to nothing"?
Softmax weights always sum to 1, so the weight has to land somewhere, which produces attention sinks.
Rung 6 State of the art: what experts work on now
Last checked 19 November 2026.
As of 2026-11-19.
The 2025 story was attention being reshaped, not replaced.
Gated attention. The Qwen team's sigmoid gate after attention [5] won a NeurIPS 2025 Best Paper award, one of four, announced 26 November 2025 [6]. A gated form of full attention is used in Qwen3-Next's hybrid layers (see the transformer page).
Linear-attention hybrids. Kimi Linear (October 2025) mixes three layers of Kimi Delta Attention, a linear-attention layer, with one layer of full latent attention. In a fair comparison trained on 1.4T tokens it reports beating full attention, with up to 75% less KV cache and up to 6× faster decoding at 1M tokens of context, in a model with 48B total and 3B active parameters [7]. These are the authors' own figures.
Learned sparse attention. DeepSeek-V3.2 (December 2025) uses DeepSeek Sparse Attention: a small "lightning indexer" scores earlier tokens and each query attends to only the top k (2,048 in training), cutting core attention cost from O(L²) to O(L·k) at 128K-token contexts [8]. The indexer itself still scales as L², but is much cheaper [8].
Kernels. FlashAttention-3 (2024) remains the reference point for fast exact attention on H100-class GPUs [4].
Nine years after 2017 [2], the formula is unchanged at its core. What changed is how many tokens each query looks at, and how.
Sources
- Bahdanau, Cho, Bengio: Neural Machine Translation by Jointly Learning to Align and Translate (2014)
- Vaswani et al.: Attention Is All You Need (2017)
- Dao et al.: FlashAttention: Fast and Memory-Efficient Exact Attention with IO-Awareness (2022)
- Shah et al.: FlashAttention-3: Fast and Accurate Attention with Asynchrony and Low-precision (2024)
- Qiu et al.: Gated Attention for Large Language Models: Non-linearity, Sparsity, and Attention-Sink-Free (2025)
- NeurIPS: Announcing the NeurIPS 2025 Best Paper Awards (2025)
- Kimi Team: Kimi Linear: An Expressive, Efficient Attention Architecture (2025)
- DeepSeek-AI: DeepSeek-V3.2: Pushing the Frontier of Open Large Language Models (2025)