# Attention, explained without formulas > How a language model works out which earlier words matter for the current one, why that made transformers win, and why it gets expensive as your text grows. Source: https://rfriedmann.de/blog/attention-without-formulas/ Published: 2026-11-23 · Track: learn · Level: Beginner Every explanation of modern language models eventually says "attention", usually followed by a formula. You don't need the formula to understand what it does, and what it does explains a lot: why models handle context well, why long documents cost more, and why people like me spend so much time on one kind of GPU code. ## The problem: words don't mean much alone Take the word "bank". In "we sat on the bank and watched the river" it is a strip of grass. In "the bank froze my account" it is a company. The letters are the same. What decides the meaning is the other words around it. Or take "it". In "the cat didn't eat because it was full", "it" is the cat. In "the cat didn't eat the food because it was cold", "it" is most likely the food. To make sense of "it", you have to look back and pick the right earlier word. A [language model](/blog/what-is-an-llm/) reads text as a row of tokens, roughly word pieces. Each token starts out as a fixed list of numbers that stands for that token in general: "bank" the same for river and money. Attention is the step that fixes this. It lets every token pull in information from the other tokens around it, so that "bank" next to "river" ends up meaning something different from "bank" next to "account". ## Each token looks back and decides what matters The core move is simple. For the current token, the model goes through every token that came before it and gives each one a score: how much does this earlier word matter for understanding the current one? Then it builds an updated picture of the current token, mostly from the high-scoring words and barely from the rest. In a chat model, a token can only look backwards, at itself and the tokens before it, never ahead. That is how text gets written one token at a time: the next word does not exist yet, so nothing can look at it.
"it" looks back at every earlier word
[diagram omitted — see the page for the chart]
A drawn example of the idea, not numbers from a real model. Real models compute one such score for every earlier token, in every layer, for every token in the text.
## Question, label, content The usual technical terms are query, key and value. They sound abstract, but an analogy gets you most of the way. Think of a library desk: - **Query, the question.** The current token asks something like "I am a pronoun, which earlier noun could I be referring to?" - **Key, the label.** Every earlier token carries a label describing what it offers: "I am a noun, an animal, singular." - **Value, the content.** Behind each label is the actual information that gets handed over when the label matches. The model compares the question with every label. Good matches get a high score, poor matches a low one. The scores are scaled so they add up to 100 percent, and the token collects content from each earlier token in that proportion. Nobody writes these questions and labels by hand: the model learns them during [training](/blog/how-a-model-learns/), because getting them right makes its next-word guesses better. ## Many heads, many questions One question per token is not enough. A word relates to its neighbours in several ways at once: grammar, who did what, which noun a pronoun points to, what topic the paragraph is about. So the model runs several attention lookups side by side, each with its own questions and labels. These are called heads. The original transformer paper, ["Attention Is All You Need"](https://arxiv.org/abs/1706.03762) from June 2017, used 8 heads per attention layer. Its authors looked at what the heads had learned and found that individual heads "clearly learn to perform different tasks", including two that appeared to link a pronoun back to the noun it refers to. That is the "it" problem from above, solved by training rather than by a rule someone wrote. ## Why this made transformers win Before 2017, many of the leading translation models were recurrent networks. They read text the way you read a sentence aloud: one word after another, carrying a running summary along. That works, but it is slow to train, because word 50 has to wait for word 49. Attention removed the waiting. During training the whole text is known in advance, so every token can compute its scores against every earlier token at the same time. That is exactly the kind of work a [GPU](/blog/what-is-a-gpu/) does well: thousands of small, identical calculations in parallel. The paper's abstract says its models were "more parallelizable and requiring significantly less time to train", and reports a top English-to-French translation result after 3.5 days of training on eight GPUs. Faster training meant bigger models on more text, and that is the road that led to today's LLMs. ## Why long context costs more Here is the catch. If every token compares itself with every token before it, the number of comparisons grows much faster than the text. Double the text and each token has twice as many earlier words to check, and there are twice as many tokens doing the checking: four times the work. In round numbers, counting every token against every token (n × n):
Text grows 16 times, pairs grow 256 times
[diagram omitted — see the page for the chart]
Computed: 1 000 × 1 000 = 1 M, 4 000 × 4 000 = 16 M, 16 000 × 16 000 = 256 M (M = million). A chat model only looks backwards, which is n × (n + 1) / 2, about half of each figure. The growth is the same. All bars share one scale.
The paper lists this itself: one self-attention layer costs work proportional to the square of the sequence length. That is a big part of why [the context window](/blog/the-context-window/) has a hard limit, why a long prompt takes noticeably longer before the first word appears, and why providers charge per input token. While the model writes, each new token still has to look at every earlier token. To avoid recomputing the labels and contents of the whole text for every new word, engines keep them in memory, the KV cache, short for keys and values. That cache grows with every token and is often what actually runs out first. The details are in [the KV cache and long context](/blog/kv-cache-and-long-context/). ## Where the engine work goes Because attention touches every pair of tokens, it is where inference engines spend much of their kernel engineering, and imp, the engine I build for one RTX 5090, is no exception. The naive version would build the full score table, every token against every earlier token, in memory. The standard trick is to never build it: stream through the earlier tokens in small blocks and keep a running result. If you want the deep version, with the arithmetic and the GPU registers, read [online softmax and FlashAttention](/blog/online-softmax-and-flashattention/). ## Bottom line Attention is the step where each token looks back at all earlier tokens, scores how much each one matters, and mixes in the relevant ones. That is how "bank" learns whether it is near a river and how "it" finds the cat. It made transformers win because it trains in parallel. Its price is that the work grows with the square of the text length, which is why long context is slow, memory-hungry and billed by the token.