What Nobody Explains About How a Transformer Actually Works

Photo: Unsplash

First Principles

What Nobody Explains About How a Transformer Actually Works

The 'Attention Is All You Need' paper is one of the most cited in history — and almost nobody who cites it can explain what attention actually does
transformersmachine-learningattention-mechanismfirst-principlesartificial-intelligence

The 2017 paper “Attention Is All You Need” has been cited over 100,000 times. It introduced the transformer architecture that underlies GPT-4, Claude, Gemini, and essentially every large language model running today. Most people in the industry can tell you it exists. Very few can tell you what the attention mechanism actually does, at a level of precision that would let you reason about why it works or when it fails.

This is that explanation. No linear algebra required, but no dumbing down either.

What You’re Actually Trying to Solve

Start with the problem that existed before transformers. When you want a model to process language, you need it to handle the fact that words get meaning from other words — often words that are far away in the sentence. “The trophy didn’t fit in the suitcase because it was too big” — what does “it” refer to? The trophy. Your brain resolves this instantly, using contextual relationships across the sentence.

The approach that dominated before 2017 was recurrent neural networks (RNNs). The basic idea: process words one at a time, left to right, carrying a “hidden state” forward that theoretically encodes everything relevant so far. For short sequences, this worked reasonably well. For long ones, it failed. The reason is structural: information from the beginning of a long sequence has to survive through dozens of sequential computation steps to influence the end, and at each step it gets diluted, overwritten, lost. By the time you reach word 100, word 3 might as well not exist.

This is the memory problem that attention solves. Not with a clever trick but with a completely different approach to how information flows through the computation.

The Mechanism, Without the Mysticism

Attention is, at its core, a learned weighting over relationships between tokens.

A token is roughly a word (or a subword unit — “transformers” might be tokenized as “transform” + “ers,” but ignore that for now). When processing any given token, the attention mechanism allows the model to look at every other token in the sequence simultaneously and decide, for each pair, how much they should influence each other.

Here’s the concrete procedure: for every token, the model produces three vectors. These are called Query, Key, and Value — terminology borrowed from database retrieval, which is apt but also somewhat misleading. The Query is roughly “what is this token looking for?” The Key is roughly “what does this token offer?” And the Value is “what information should actually flow if the match is good?”

For each token, the model computes a dot product between that token’s Query vector and every other token’s Key vector. This produces a score. Higher score = stronger relationship. The scores get normalized (softmax, which converts them to probabilities that sum to 1), and then those probabilities are used to compute a weighted average of the Value vectors. The result: each token ends up with a new representation that’s an aggregation of information from every other token in the sequence, weighted by relevance.

Run this for every token simultaneously. That’s attention.

The critical thing to understand: those Query, Key, and Value vectors aren’t hand-designed. They’re learned from data during training. The weights that produce them start random and get adjusted over millions of examples until the model produces weights that make good predictions. The network is not programmed to care about noun-pronoun relationships — it learns that caring about noun-pronoun relationships produces better predictions, so it adjusts toward that.

Why “Attention” Is the Wrong Word

Attention implies something cognitive — a spotlight that some agent moves around deliberately. That’s not what this is.

What attention actually does is compute a dynamic lookup table. For any input sequence, each token generates a query, and the mechanism retrieves a weighted blend of information from the entire sequence based on how well that query matches the keys. There’s no agent doing the looking. There’s no meaning being understood. There is a differentiable function, learned from data, that has internalized patterns about which kinds of tokens tend to be relevant to which other kinds of tokens in which contexts.

“Learned weighting of contextual relationships” would be a more accurate name. Nobody would have cited it 100,000 times under that title, but it would have set better expectations.

This matters because when people say models “understand” text, they’re often gesturing at attention as the mechanism. But attention doesn’t understand anything. It finds statistical relationships. Those relationships can be remarkably useful — and in aggregate, across dozens of layers, billions of parameters, and trillion-token training runs, they can produce outputs that look very much like understanding. But the mechanism itself is closer to a very sophisticated pattern-matching function than to comprehension.

Multi-Head Attention and Why It’s Necessary

The transformer doesn’t run a single attention computation — it runs many in parallel. This is called multi-head attention.

The intuition: different kinds of relationships need to be tracked simultaneously. In the sentence “John gave Mary the book she wanted,” you have noun-verb relationships, pronoun-antecedent relationships, subject-object relationships, all relevant at once. A single attention computation computes a single weighted average — it has to collapse all these relationships into one representation. Multi-head attention runs multiple attention computations in parallel (say, 16 or 32), each with its own set of Query, Key, and Value weight matrices, and concatenates the results. Different heads can specialize in different relationship types.

Do they actually specialize? Interpretability research suggests yes, to a meaningful degree. Some heads in large models have been found to attend to previous-word patterns, others to syntactic relationships, others to coreferential expressions. This wasn’t designed in — it emerged from training on language prediction. The heads that learned useful specializations survived gradient descent; the ones that learned useless patterns got trained out.

Why This Beats Recurrent Networks for Long Sequences

The fundamental advantage of attention over RNNs isn’t accuracy on any specific task — it’s the path length between any two tokens.

In an RNN processing a 1,000-word sequence, the relationship between word 1 and word 1,000 has to travel through 999 sequential computation steps. Information degrades. Gradients vanish (a technical problem where the training signal becomes too small to update early layers effectively). Long-range dependencies are systemically hard to learn.

In a transformer, word 1 and word 1,000 are one attention computation apart. The path length between any two tokens is always one. This makes long-range dependencies structurally equivalent to short-range ones — neither is harder to learn as a consequence of sequence length.

The tradeoff is computational cost. Attention requires computing relationships between every pair of tokens, so the cost scales quadratically with sequence length. An RNN scales linearly. For short sequences, RNNs are more efficient. For long sequences — which is where the interesting capabilities live — transformers win. Modern models with context windows of 100,000 or 200,000 tokens have had to develop approximations to pure attention to make this tractable, but the core insight holds.

The Emergent Capabilities Nobody Predicted

When Vaswani et al. published the original paper in 2017, they were solving a specific problem: machine translation. They showed their architecture outperformed the then-state-of-the-art on translation benchmarks. That was the claim.

Nobody expected that scaling this architecture — more layers, more parameters, more training data — would produce models capable of writing code, doing arithmetic, answering trivia questions in dozens of languages, reasoning through multi-step logic problems, and generating coherent text across arbitrary topics. The 2017 paper didn’t predict GPT-3. The researchers who built GPT-3 were genuinely surprised by some of what it could do.

This is the part that nobody has a satisfying explanation for. The attention mechanism as a mathematical object is well-understood. What happens when you stack 96 transformer blocks, train on a trillion tokens of internet text, and scale to hundreds of billions of parameters is not. The emergent capabilities appear to arise from the interaction of many such layers, each refining the representations passed to it by the previous one, but why this produces coherent multi-step reasoning is genuinely unclear. Current interpretability research is scratching the surface. The field knows the mechanism but not the complete causal story from mechanism to capability.

What This Means for How You Think About These Systems

If you walk away with one thing: attention is not cognition. It is a differentiable aggregation function that produces representations weighted by learned statistical relationships. When a model uses “attention” to resolve a pronoun, it’s not understanding who “it” refers to in any meaningful sense — it’s doing a computation that has learned, from millions of examples, that pronoun tokens should pull heavily from certain nearby noun tokens in certain structural positions.

The outputs of this computation can be extremely useful. They can also fail in ways that don’t look like a cognitively coherent system failing — they look like a pattern-matching function encountering something outside its training distribution. When GPT-4 makes a strange error on a logic problem that any ten-year-old would get right, it’s not having a moment of stupidity. It’s doing exactly what it always does, and what it always does doesn’t generalize to that case.

Understanding the mechanism doesn’t mean the tools are less impressive. The thing attention actually does — efficiently aggregating contextual relationships across arbitrary sequence lengths — is genuinely clever. Building a differentiable architecture that can learn to do this from data alone, without any explicit programming of language rules, is a real intellectual achievement. What it isn’t is thinking.

The distinction matters more now than it did in 2017, when these systems were curiosities. It matters now because we’re making decisions — about deployment, about trust, about risk — based on assumptions about what large language models can and can’t do. Those assumptions are better if they’re grounded in what the mechanism actually is.

Attention is all you need, the paper said. What attention is, though — that part they left for everyone else to figure out.

Get the next live webinar in your inbox

One email a month: the upcoming live event + free recording access for subscribers. No spam, unsubscribe anytime.