Kolkata ·  
AI engineering
foundations
deep · 6 min

Attention

How each token decides which earlier tokens matter for predicting the next one.

updated 23 Sept 2026

Picture a whole mystery novel pasted into a prompt, ending like this:

Therefore the murderer was

The model has to predict the name. And it predicts the next token from one place only, the vector at the last position (that's how next-token prediction works). That last token is "was". When it enters the model, its vector is just the plain, generic vector for "was", looked up from a table of embeddings. It knows nothing about the book.

So somewhere between the start and the end, the clues from hundreds of pages back have to get into that one vector. Attention is how they get there. Let's build it one piece at a time.

A smaller example

A novel is too big to follow, so take a short phrase:

A fluffy blue creature roamed the verdant forest

The lookup gives "creature" the same vector it would get in any sentence. We'd like it to soak up "fluffy" and "blue", so it ends up meaning something closer to "fluffy blue creature".

Attention gives every token three new vectors, each made by multiplying its embedding by a learned matrix:

  • A query: what this token is looking for. For "creature", think of it as asking "any adjectives in front of me?"
  • A key: what this token can offer. The keys for "fluffy" and "blue" come out looking like a good answer to that question.
  • A value: what this token passes along if it gets picked. For "fluffy", roughly "make it fluffy".

Then, for "creature", we compare its query with every earlier token's key. The comparison is a dot product: multiply the numbers pairwise and add them up. A big result is a good match. "creature" scores high against "fluffy" and "blue" and low against "a".

Those scores get divided by the square root of the key size (it keeps the numbers in a range where the next step behaves), then softmax turns them into weights that are all positive and add up to 1. That set of weights is the attention pattern. Finally we take each token's value, multiply by its weight, add them up, and add the result to "creature"'s own vector.

You can think of it as a fuzzy dictionary. A normal dictionary returns the one entry whose key matches exactly. Attention compares the query with every key and hands back a blend of all the values, weighted by how well each matched. Every token does this at the same time, each with its own query.

One honest caveat. The adjective story is a teaching example. Nobody programs a head to look for adjectives. The matrices are learned, and what a real head ends up doing is usually much harder to describe.

Only looking back

There's one rule we skipped. Before softmax, any score for a later token is set to minus infinity, which softmax turns into exactly 0. So "creature" can see "a", "fluffy" and "blue", but never "roamed".

Why? Because of how the model is trained. It practises predicting the next token at every position of a text at once: "fluffy" after "a", "blue" after "a fluffy", and so on. One sentence becomes many practice questions. If "creature" could peek at "roamed", the answer would be sitting right there.

That has a side effect worth knowing when you write prompts. A token's vector can never be changed by anything after it. Only later positions get to combine an earlier part of the prompt with a later one. So where you put things in a prompt changes what can be mixed with what.

Many heads, many layers

What we built is one attention head: one set of query, key and value matrices, so one kind of relationship. Adjectives to nouns is one kind. A pronoun finding the name it points to is another. So models run lots of heads side by side, each with its own matrices, and add up all their changes. The 2017 transformer had 8 heads per layer and 6 layers. GPT-3 had 96 heads per layer and 96 layers.

When researchers looked inside that 2017 model, different heads had picked up different jobs, many tied to grammar. Anthropic's interpretability work puts it plainly: a head's basic job is moving information from one position to another. In small models they found induction heads, which look for an earlier copy of the current token and copy what came after it. If "Mr Dursley" appeared earlier and the text now says "Mr", the head finds the earlier "Mr" and pushes "Dursley" forward as a likely next token. That's a basic way of learning from the prompt itself, the effect few-shot prompting leans on. (Those heads only showed up once a model had at least two attention layers, and they were found in toy models. The authors think the idea carries over to big ones, partly.)

Stacked in layers, heads build on each other: a later head works on vectors that earlier heads have already filled with context. That's how the clues in our novel can reach "was" in the end. How attention takes turns with the other half of each layer is in transformer.

Where it came from

Attention started in translation. In 2014, translation models read a whole sentence and squeezed it into one fixed-length vector before writing the translation. Bahdanau, Cho and Bengio argued that single vector was a bottleneck, and let the model search back over the source words that mattered for each word it wrote.

Back then attention sat on top of a recurrent network, which reads one word after another. The 2017 paper "Attention Is All You Need" dropped the recurrent part. A recurrent network has to go word by word, but attention can handle every position at once, which suits GPUs. That model reached top translation quality after as little as twelve hours on eight GPUs. Being easy to run in parallel is a big part of why attention took over.

What it costs

Every token scores every earlier token. For a prompt of n tokens that's a grid of about n × n scores, per head, per layer. Double the prompt and the grid gets four times bigger. That's why long context windows were hard to build and still aren't free.

While a model writes, recomputing the keys and values of every earlier token for each new one would be wasteful, so they're kept. That store is the KV cache, and it grows with every token.

Open models have been changing attention mostly to cut that cost. Grouped-query attention lets several heads share one set of keys and values, so there's less to keep, and it has largely replaced the original design. Sliding-window attention lets some layers look only at nearby tokens; Gemma 3 uses five of those (with a 1,024-token window) for every one layer that sees everything.

And attention isn't all of the model, even if it gets the fame. In GPT-3, the attention weights are just under 58 billion of the 175 billion parameters, about a third.

So will this design last?

It's not settled. In 2025, MiniMax-M1, Qwen3-Next and DeepSeek V3.2 swapped regular attention in most or all of their layers for cheaper "linear" kinds whose cost doesn't grow with the square of the length. Then MiniMax went back to regular attention for its next model, M2, saying the linear kind was tricky in production and weak on reasoning and multi-turn tasks. Qwen3-Next kept one regular attention block for every three cheap ones, because the cheap kind is worse at finding one exact detail far back.

Which is our murderer problem again: finding the one clue on page 40 that the last token needs. As of 2026-09, mixes of the two kinds are where the experimenting is happening. We only know any of this about open-weight models, though. Closed models don't publish their designs.

Further reading