Artificial Intelligence

What a transformer is actually doing when it "reads"

One attention head, nine words, and a pronoun resolving to its referent — without anyone having written a rule about pronouns.

An attention matrix for a nine-word sentence showing which earlier words each word draws from, with the pronoun resolving to its referent
Darker cells are a larger share of the row's attention. Each row sums to one, and the upper right is empty by construction.
In short

A transformer processes a sequence by letting every position compute a weighted average over every earlier position. The weights — attention — are learned rather than specified, which is how a model comes to associate a pronoun with its referent without any rule about pronouns. The mask that prevents a position from seeing later ones is what makes the model a predictor rather than a reader.

Key takeaways
  • Attention is a learned weighted average. Each position mixes in some fraction of every earlier position's representation.
  • Nobody wrote a rule for pronouns. The weight is high because that arrangement predicted the next word better during training.
  • The empty triangle is the causal mask. A position cannot attend to one that has not arrived — which is what makes it a predictor.
  • Parallelism, not insight, made it win. All positions compute at once, which is what made training at this scale affordable.

The problem the architecture solves

Before 2017, sequence models processed text in order: read a word, update a hidden state, read the next. This has an obvious appeal and two fatal properties. Information from early in a sequence has to survive many update steps to influence a decision late in it, and the sequential dependency means you cannot compute position fifty until you have computed position forty-nine.

The first problem limits how much context a model can genuinely use. The second limits how fast you can train, which at scale limits everything.

The transformer discards sequential processing. Every position looks at every earlier position directly, in one step, and all positions are computed simultaneously.

What attention computes

Take the sentence in the figure above: "The particle was small enough that it scattered light."

Each word arrives as an embedding — a list of numbers. Attention then does something conceptually simple. For each position, the model computes a score against every other position, converts those scores into weights that sum to one, and replaces the position's representation with a weighted average of all the others.

That is the whole mechanism. The sophistication is entirely in how the scores are produced: each position emits a query describing what it is looking for and a key describing what it offers, and the score is how well one position's query matches another's key. Queries, keys and the values being averaged are all produced by learned projections of the embedding.

Reading the matrix

Look at the row for "it". The overwhelming share of its attention goes to "particle".

The model has resolved the pronoun. Nobody wrote a rule about pronouns; there is no coreference module. That weight is large because, during training, arranging the computation that way produced better predictions of the words that followed — across an enormous number of sentences in which a pronoun referred back to a noun.

This is worth pausing on, because it is the general shape of how these models acquire anything resembling structure. Grammar, factual association, argument form: none of it is specified. All of it is whatever configuration of weights reduced prediction error.

It is also why interpretability is hard. The mechanism found is not required to resemble the one a linguist would describe, and usually does not. This particular head is unusually legible; most heads in a real model do something considerably harder to name.

The empty triangle

The upper right of the matrix is blank. A position cannot attend to a position that has not arrived yet.

This is the causal mask, and it is a design decision rather than a technical limitation. It is what makes the model a next-token predictor: at every position, the model sees exactly what a reader would have seen at that point and nothing more, so predicting the next token is a well-posed problem over the entire training corpus without anyone labelling anything.

That mask is the reason these models could be trained on raw text at scale. It is also the reason they generate strictly left to right, and cannot revise an earlier token once produced — a constraint that a great deal of subsequent engineering exists to work around.

Many heads, many layers

One head, one layer, one nine-token sentence is a diagram. A real model runs dozens of heads in parallel per layer, each with its own learned projections, and stacks dozens of layers.

Heads within a layer specialise. Some track syntactic dependencies; some attend to the immediately preceding token; some appear to do nothing identifiable and can be removed with little effect. Layers appear to build progressively more abstract representations, though this is a description of observed behaviour rather than an architectural guarantee.

The count matters for one practical reason: attention compares every position with every other, so cost grows with the square of sequence length. Doubling the context window quadruples the attention computation, which is why long-context models are expensive and why a large amount of research targets exactly this.

What this explains about the failure modes

Understanding the mechanism makes several otherwise puzzling behaviours predictable.

Confident fabrication. The model produces likely continuations. A plausible-sounding citation is a likely continuation whether or not the paper exists, and nothing in the objective distinguishes true from likely. Hallucination is the mechanism working normally on a question it lacks the information to answer.

Sensitivity to phrasing. Attention weights depend on the actual tokens present. Rewording a question changes the keys and queries, therefore the weights, therefore the computation. This is not fragility bolted on — it follows directly from the design.

Degradation in the middle of long contexts. With thousands of positions competing for a share of a distribution that sums to one, material in the middle of a long context reliably receives less weight than material at either end. Putting the important thing at the start or the end of a prompt is not folklore; it is a consequence of the arithmetic.

Sources & further reading

  1. Vaswani, A. et al. — Attention Is All You Need. NeurIPS (2017). Link →
  2. Elhage, N. et al. — A Mathematical Framework for Transformer Circuits. Anthropic (2021). Link →
  3. Liu, N. F. et al. — Lost in the Middle: How Language Models Use Long Contexts. TACL (2024). Link →

Common questions

Is attention the same as understanding?

No. It is a weighted average with learned weights. Whether the resulting behaviour constitutes understanding is a question about definitions rather than about the mechanism, which is fully specified and contains no such notion.

Why is context length expensive?

Attention compares every position with every other, so cost scales with the square of sequence length. Doubling the window quadruples that part of the computation.

Do I need to implement a transformer to work with them?

Not to use one. Implementing a small one once is the fastest way to stop being surprised by their behaviour, and it is a weekend's work in about two hundred lines.

Nanoschool AI Desk

Artificial intelligence editorial team · Reviewed by Nanoschool faculty

Covers machine learning for people who intend to build with it rather than read about it: what the architectures actually do, what the benchmarks actually measure, and which claims survive contact with a dataset you did not choose.

Hi! Need help? Chat with NSTC ✨