Deep Learning Intermediate ⏱ 40 min attentionself-attentionquery key valuetransformerscontextLLMsoftmaxcausal maskfoundations

Reading the Room: The Idea at the Heart of Every Language Model

Published 2026-07-19 — Dr Neal Aggarwal
📖 Chapter Three of a ground-up account of how large language models work. Chapter One turned text into tokens; Chapter Two gave each token a place in a space of meaning — but a frozen place, the same in every sentence. We ended on a single unsolved problem: the word “bank” cannot be both a riverside and a savings institution while wearing one fixed vector. This chapter solves it, and in solving it builds the mechanism the whole field is named after.

Reading the Room: The Idea at the Heart of Every Language Model

Let me put the last chapter's unfinished business back on the table, because everything here grows out of it.

We had given every token a vector — a location in a vast space where nearness means similarity of meaning. It was a real achievement: the model could now generalise, treating ramipril sensibly on first sight because it lives near lisinopril. But the vector was fixed. The embedding table hands over the same coordinates for bank whether the sentence is about a river or a cheque. And I argued that this is fatal, because meaning in language is not a property of words in isolation. It is a property of words in company. The single most important token for the sense of a sentence often arrives wearing a blank mask, and the mask is identical in situations that mean opposite things.

So the requirement is clear, even before we know how to meet it. Each token must be allowed to look at the other tokens around it and update its own representation in light of what it finds. The bank sitting near river and current should end up shaded toward riverside; the bank sitting near deposited and cheque should end up shaded toward finance. Same starting vector, two different destinations, and the difference decided entirely by the company each keeps.

The mechanism that does this is called attention, and it is, without exaggeration, the idea that made modern language models possible. Everything before it — tokenization, embeddings — is preparation. Everything after it is elaboration. This is the hinge. So we are going to build it slowly, from the ground, and I promise that by the end it will feel not clever but inevitable — the obvious thing to want, arrived at by asking a sequence of reasonable questions.

Part I — The naive idea, and why it is not enough

Let us start with the simplest possible attempt, because its failure teaches us exactly what we really need.

A token must incorporate information from its neighbours. The crudest way to do that: just average the vectors of all the tokens in the sentence and mix a little of that average back into each token. Now every token knows, vaguely, "what kind of sentence am I in." The bank in the river sentence would pick up a little riverside flavour simply because river and current are in the average.

It is not a stupid idea. It is just badly undiscriminating. An average treats every neighbour as equally relevant. But when bank is trying to work out which sense it is, river is enormously relevant and the word the is almost totally irrelevant, and a flat average drowns the signal in the noise. What we actually want is a weighted average — one where bank leans heavily on river, glances at current, and all but ignores the.

Which raises the only question that matters: where do the weights come from? They cannot be fixed in advance, because which words matter depends entirely on the sentence. In "the bank of the river" the relevant partner is four words away; in "he robbed a bank" it is two words away and a completely different word. The weights have to be computed on the fly, from the actual content of the tokens involved. A token has to be able to look out at the sentence and decide, for itself, this one matters to me, that one does not — and it has to make that decision based on meaning, not position.

💡 The whole problem, in one sentence. Each token needs to compute, for every other token, a relevance weight — how much should I let this one influence me? — where the weight depends on the content of both tokens, and then take a weighted average of the others accordingly. Attention is nothing more than a specific, learnable, beautifully parallel way of doing exactly this. The rest of this chapter is just filling in the "specific."

Part II — Three questions every token learns to ask

Here is the move that turns the vague wish above into a concrete mechanism, and it is genuinely elegant. To decide how much token A should attend to token B, we let each token play up to three roles, and we build each role out of the token's vector using a small learned transformation.

Think of it as each token being able to ask and answer three questions:

  • A query"what am I looking for?" This is token A's advertisement of what would be relevant to it. The bank-token's query might, loosely, encode "I'm an ambiguous noun; I'm looking for something that disambiguates me — a body of water, or money."
  • A key"what do I offer?" This is each token's advertisement of what it contains, phrased so that a matching query will recognise it. The river-token's key encodes "I am about water, geography, nature."
  • A value"what will I actually hand over if you attend to me?" This is the information a token contributes once it has been selected — the payload.

Each of these three is produced from the token's embedding by its own learned projection — three different "views" of the same underlying vector, each shaped during training to be good at its job.

THREE VIEWS OF ONE TOKEN “bank” vector its embedding from Chapter Two ×W_Q ×W_K ×W_V QUERY “what am I looking for?” something that disambiguates me KEY “what do I offer?” a label other tokens can match against VALUE “what will I hand over?” the information I contribute if chosen
Figure 1. Every token is projected into three roles by three learned matrices (W_Q, W_K, W_V). The same word plays all three parts at once: it asks its own question (query), advertises itself to others (key), and stands ready to contribute (value). These three projections are among the parameters the model learns during training — the model discovers, from data, what makes a good question and a good answer.

The trick, now, is how a query and a key are compared. Both are vectors — arrows in the same space — and there is a standard way to measure how well two vectors align: the dot product, which is large and positive when two vectors point the same way, near zero when they are unrelated, and negative when they point apart. So the relevance of token B to token A is simply: take A's query, take B's key, and dot them together. A high number means "B is exactly the kind of thing A was looking for." That single number is the raw ingredient of every attention weight.

Part III — From raw scores to a spotlight

Let us make this concrete with the sentence that started us off. The bank-token issues its query, and we score it against the key of every token in the sentence — including itself. We get a row of raw numbers, one per token: high for river, moderate for current, low for the and of.

Raw scores, though, are awkward to use directly. They can be any size, positive or negative, and they do not add up to anything meaningful. What we want is to turn them into a clean set of weights — all positive, and summing to exactly one — so they behave like a spotlight of fixed total brightness that we can shine across the sentence, pouring most of it on the relevant words and only a sliver on the rest. The function that does this conversion is called the softmax, and while its formula is a small piece of arithmetic, all you need to hold is what it accomplishes: it takes any row of scores and squashes it into a tidy distribution of attention that sums to one, exaggerating the gaps so the strong scores get the lion's share.

“bank” DECIDES WHERE TO LOOK sentence: “he sat on the bank of the river” query( bank ) dotted against every key ↓ token (its key) raw score attention weight (softmax) river 8.9 0.62 current* 6.1 0.21 sat 3.0 0.09 the 1.2 0.04 of 0.8 0.04 *for a token appearing later, see Part V on why the future is hidden the weights sum to 1.00 — a spotlight of fixed brightness most of it falls on “river”, a little on “current”, almost none on “the”
Figure 2. The scoring step for a single token. Its query is dotted against every key to produce raw scores; softmax turns those into attention weights that sum to one. Crucially, these weights are not stored anywhere — they are recomputed from scratch, from the actual content of this particular sentence, every single time. Change one word and the whole spotlight redraws.

We now have, for our bank-token, a spotlight: 62% on river, 21% on current, and dribs and drabs elsewhere. The final step is the one we were aiming for all along. We take a weighted average of the value vectors — 62% of river's value, 21% of current's value, and so on — and that blended vector becomes the information bank has gathered from its context. Mixed back into bank's own representation, it drags the meaning firmly toward riverside. The blank mask has been painted.

GATHER: A WEIGHTED BLEND OF VALUES value( river )× 0.62 value( current )× 0.21 value( sat )× 0.09 value( the/of )× 0.08 Σ context gathered a blended vector, mostly “water / geography” “bank” now shaded toward riverside In the “deposited the cheque” sentence, the very same machinery would light up “deposited” and “cheque” instead — and “bank” would be dragged toward finance. One mechanism, opposite outcomes, decided by content.
Figure 3. The payoff. The token's updated representation is a weighted sum of the value vectors of everything it attended to. The static embedding of Chapter Two has become contextual: “bank” is no longer a fixed point but a point that has moved, in this sentence, toward the meaning its neighbours implied. The crack we ended the last chapter on is healed.

And that — query, key, value; score, softmax, blend — is the entire mechanism. Read those two figures again and notice there is no magic anywhere in them, only a learnable, content-driven weighted average. Every token in the sentence does this simultaneously, each issuing its own query, each ending up with its own context-updated representation. The sentence walks in as a row of frozen, isolated vectors and walks out as a row of vectors that have talked to each other.

⚕️ The ward-round analogy, because it genuinely fits. Picture a multidisciplinary team meeting. Each patient's case (a token) silently broadcasts a question — “what here is relevant to me?” (its query). Every other case advertises what it holds — “I'm the one with the deranged clotting” (its key). Each case then listens, weights the others by relevance, and updates its own plan with a blend of what the relevant cases contribute (their values). Nobody reads every chart end to end; relevance is negotiated by content, in parallel, all at once. And — the part worth remembering — when a model attends to the wrong case, confidently attributing a fact to the wrong antecedent, you are watching an attention weight land in the wrong place. A surprising share of an LLM's plausible-but-wrong answers are, at bottom, attention pointed at the wrong word.

Part IV — Why this was the breakthrough

It is worth pausing to appreciate why this particular design changed everything, because it did not merely work — it unlocked the entire scale of the modern era. Two properties are responsible.

The first is that attention is content-addressed, not position-addressed. Older approaches to sequence modelling processed text strictly left to right, carrying a running summary forward and asking, in effect, "what was a few steps back?" That makes reaching across long distances hard — by the time you are five hundred words in, the details of word twelve have been squeezed to mush. Attention asks a completely different question: not "who is near me?" but "who matches me?" A token can reach directly across a thousand others in a single step to the one word that resolves it, with no decay over distance. The same machinery, unchanged, handles a pronoun and its antecedent, a subject and its far-off verb, a closing bracket and its opening one, and a fact stated three paragraphs earlier. It never needed to be told which of these tasks it was doing; relevance is relevance.

The second is that attention is completely parallel. Because every token computes its query, its key, and its value independently, and because all the scores are just one big multiplication of queries against keys, the entire operation runs at once — not word by word, but the whole sentence in a single sweep. This is not a minor efficiency note. It is the reason these models could be trained on a meaningful fraction of the internet at all. The architecture finally matched what modern hardware is savagely good at: doing enormous numbers of multiplications simultaneously. An idea that was merely elegant would have stayed in a research paper. An idea that was elegant and parallel became the foundation of an industry.

Part V — The one rule: you may not read the future

There is a single, crucial constraint I have been quietly deferring, and now it clicks into place with everything from Chapter Two.

Recall the game: the model is trained to predict the next token from the ones before it. Now imagine we let a token attend freely to every other token in the sentence, including the ones that come after it. When the model is trying to predict the word after bank, it could simply peek at that word through attention — and the whole exercise collapses into cheating. It would ace training by copying the answer and learn nothing.

So we impose one rule. When a token computes its attention, it is permitted to look only at itself and the tokens before it — never at the ones that follow. Before the softmax, the scores for all future positions are blanked out entirely, so they receive exactly zero weight. Information is allowed to flow backward-to-forward, never forward-to-backward. This is called the causal mask, and it is why this whole family of models earns the name autoregressive: each position is built only from the past, so that predicting the future remains an honest test.

THE CAUSAL MASK — NO PEEKING AHEAD rows = the token doing the looking · columns = the token being looked at the patient took two tablets the patient took two tablets allowed blocked Every token sees the past and itself; none sees the future. The green lower triangle is the whole rule. It is what keeps “predict the next token” an honest exam rather than a memory test with the answers showing. this is the exact link between Chapter Two's game and Chapter Three's mechanism
Figure 4. The causal mask, drawn as a grid of who-may-look-at-whom. The permitted cells form a lower triangle: each token attends to itself and everything before it, never after. This single constraint is the seam that stitches attention to the prediction objective — it is why a model can be trained to predict the future without ever being shown it.

Part VI — What one glance cannot do

We have built something remarkable, and it is worth being precise about exactly how far it gets us — because the honest limits are what point straight at the next chapter.

Our attention mechanism lets every token look once across the sentence, gather the relevant context, and update itself. The frozen vectors of Chapter Two are now fluid, context-aware, alive to their surroundings. But look closely at two things it does not yet do.

First, a single attention operation can only track one kind of relationship at a time. Its one set of query and key projections learns one notion of "relevant" — perhaps "which earlier noun does this pronoun refer to." But a sentence is threaded with many relationships at once: grammatical agreement, who-did-what-to-whom, the matching of quotation marks, the long-range echo of a topic. One query-key scheme cannot be all of these simultaneously. We are going to need many attention operations running in parallel, each free to specialise in a different kind of relationship — and then a way to combine what they each found. That plurality has a name, multi-head attention, and it is the first thing we build next.

Second — and this is subtler — attention moves information around but does very little thinking with it. It is a superb librarian, fetching the right passages and laying them on the desk, but fetching is not the same as reasoning over what was fetched. After each token has gathered its context, it needs a moment to sit and compute on what it now holds — to transform the gathered information into something new. That step, a small computation applied to each token on its own, alternates with attention in a rhythm that repeats: gather, then think; gather, then think. Stack that rhythm dozens of times and you have the full engine — the transformer block, and the deep tower built from it.

So this is where we stand. We have the beating heart. We have watched a word turn its head, read the room, and change its meaning. What we do not yet have is the whole body: the many parallel glances, the per-token thinking that turns gathered context into inference, and the depth — the stacking of block upon block — that lets meaning be refined layer after layer until a confident next-token forecast can finally be read off the top.

Building that body, from this heart, is Chapter Four. And when it is finished, you will be able to look at a diagram of a real transformer — the kind that has intimidated readers for years — and find, in every box, something you understand from the inside.

— Neal

📖 Next chapter: The Transformer Block — many attention heads looking at once, the per-token computation that turns gathered context into thought, and the deep stack that refines meaning layer by layer into a prediction. We assemble the whole engine from the single part we just built.
tags: attention self-attention query key value transformers context LLM softmax causal mask foundations