Forward-deployed / AI Knowledge Graph
Visual explainershow it works
Explainer 03

One token through a transformer

Last reviewed: 2026-11 · Volatility: medium

In one minute

A transformer is a stack of identical blocks. Each block does two things to every token: it lets the token look at the other tokens, and then it reworks the token alone. This page follows one token, "bank" in the phrase "the river bank", through one block, and then up through the stack. The aim is to see how the model can tell a river bank from a money bank.

You will meet embeddings, attention with its query, key and value, heads, the feed-forward layer and the residual stream.

Reading time: about 10 minutes. The tiny vectors and weights are made up so the arithmetic fits on a page. Real models use thousands of numbers per token.

1 · A token becomes a vector

The model starts with a token ID. It looks the ID up in a table and gets a list of numbers: the token's embedding. This is the token's starting meaning. The table is learned. Tokens used in similar ways end up with similar lists.

At this point "bank" has the same embedding whatever sentence it is in. It could mean a river edge or a place for money. Context has not yet been used. That is what the rest of the stack adds.

Stage 1

From an ID to a vector

A table lookup. The table is learned.

Token IDbank
Embedding tableone list of numbers per token
Vectorthe same whatever the sentence

Context has not been used yet.

Remember: the first vector for a word ignores its context.

2 · Position is added

Attention on its own does not know word order. "Dog bites man" and "man bites dog" would look alike. So the model adds a position signal to each token's vector. In our phrase, "the" is first, "river" second and "bank" third.

Some models add fixed wave patterns. Some learn a vector per position. Some, such as those with rotary embeddings, turn the numbers by an angle that depends on position. Whatever the method, the token now carries both what it is and where it is.

Stage 2

Adding position

Attention alone does not see word order.

Word vectorwhat the token is
+ position signal1st, 2nd, 3rd
Readywhat and where

Waves, learned vectors or rotation. The goal is the same.

Remember: order has to be added, because attention alone does not see it.

3 · Three views: query, key and value

Each block turns every token's vector into three new ones, using three learned matrices.

  • The query says what this token is looking for.
  • The key says what this token offers, so others can match it.
  • The value is the information it hands over when someone chooses it.

Think of a library. The query is your question, the keys are the labels on the shelves, and the values are the books.

Stage 3

Three views of each token

Made with three learned matrices.

Querywhat this token is looking for
Keywhat this token offers for matching
Valuethe information it hands over

Library: query is your question, key is the shelf label, value is the book.

Remember: query asks, key advertises, value delivers.

4 · Scores: who matters to me?

To decide where "bank" should look, the model compares its query with the key of every token it is allowed to see: "the", "river" and "bank" itself. The comparison is a dot product: a large number means a good match. The scores are divided by the square root of the vector size to keep them from growing too large.

In our toy numbers, "bank" scores 0.07 against "the", 0.89 against "river" and 0.58 against itself. "River" is the best match.

Stage 4

How well does each key match the query of "bank"?

Toy numbers: score = query · key ÷ √2.

the
0.07
river
0.89
bank
0.58

River is the best match.

Remember: a score says how relevant another token is to this one.

5 · Scores become weights

The scores go through softmax, which turns them into positive numbers that add up to 1. Higher scores get most of the weight. Here "bank" gives about 20% to "the", 46% to "river" and 34% to itself.

Generation models use a causal mask: a token may look only at itself and tokens before it. That keeps the model honest when it predicts the next word. "Bank" is last, so it can see all three.

Stage 5

Softmax turns scores into shares

The shares add up to 1.

the
0.20
river
0.46
bank
0.34

Causal mask: a token sees itself and the tokens before it.

Remember: attention weights are shares of attention, and they sum to 1.

6 · Mixing the values

The new vector for "bank" is the weighted sum of the values: 20% of the value of "the", 46% of "river" and 34% of "bank". Information flows from the tokens that matter into this one.

Now the vector for "bank" carries a trace of "river". It is no longer the same as it was in a sentence about money. This is how context gets in.

Stage 6

A weighted mix of values

New vector for bank = 20% the + 46% river + 34% bank.

20% of thevalue
46% of rivervalue
34% of bankvalue
New vectornow carries a trace of river

This is how context gets into the vector.

Remember: attention is a weighted mix of what other tokens carry.

7 · Many heads

One round of attention can ask only one kind of question. So a block runs several heads side by side, each with its own query, key and value matrices. One head may track grammar. Another may track what a pronoun refers to. Another may follow names.

The heads' results are joined and mixed by one more matrix. A useful view is that each head writes its own small note, and the notes add.

Stage 7

Heads ask different questions in parallel

Each head has its own query, key and value matrices.

Head 1grammar
Head 2what a pronoun refers to
Head 3names and places
Join and mixone more matrix

The roles shown are examples. Real heads vary.

Remember: heads let a block ask several questions at once.

8 · The feed-forward layer

Attention moves information between tokens. The feed-forward layer then works on each token alone. It widens the vector, applies a nonlinear function and narrows it again. No information crosses between tokens here.

This layer is large. In a typical block it holds about two thirds of the parameters, and research suggests much of the model's stored knowledge sits in these layers. Attention finds the right information, and the feed-forward layer processes it.

Stage 8

The feed-forward layer works on one token alone

About two thirds of a block's parameters.

Vectord numbers
Widento about 4 × d
Nonlinear step
Narrowback to d

No information crosses between tokens here.

Remember: attention shares information. The feed-forward layer thinks about it, one token at a time.

9 · The residual stream

Attention and the feed-forward layer do not replace the token's vector. They add to it. This running total is called the residual stream. Each part reads the stream, writes a small note and leaves the rest alone.

This has two benefits. A block that writes nothing changes nothing, so deep stacks train well. And the stream gives a place where many parts can collaborate on one vector.

Stage 9

The residual stream

Each part adds a note. Nothing is overwritten.

Streamthe running vector
+ attention note
+ feed-forward note
Streamricher than before

A block that writes nothing changes nothing.

Remember: each layer adds to the vector. Nothing is overwritten.

10 · Up the stack, and out

The block repeats, with different weights, many times: a few dozen layers in large models. In the early layers, "bank" may only pick up nearby words. In later layers, its vector can hold richer ideas, such as "the edge of a river".

At the top, the last token's vector is turned into a score for every word in the vocabulary. The model picks the next word. The Transformer paper showed that a network built only on attention, without recurrence or convolution, reached 28.4 BLEU on English-to-German translation and 41.8 on English-to-French.

Stage 10

Up the stack and out

The block repeats with different weights.

Layer 1nearby words
Layer 2
... layer Nricher ideas
Scoresone per word in the vocabulary

Vaswani et al.: attention only, no recurrence or convolution.

Remember: a transformer is one block repeated, and the vector grows richer each time.

The whole journey

Summary

One token's journey

EmbedID to vector
Positionwhere it is
Attentionlook at others
Feed-forwardrework alone
RepeatN layers
Next wordscores

One block, repeated.

Where it breaks

  • Long inputs. Attention costs grow with the square of the length.
  • Lost in the middle. Information buried in a long prompt is used less well.
  • Position limits. A model can fail at lengths it never saw in training.
  • Confident errors. Attention finds patterns. It does not check facts.

Go deeper

Test yourself

  1. What are the query, key and value?

    AnswerThe query is what a token looks for. The key is what it offers for matching. The value is the information it passes on if chosen.

  2. In the example, which token gets the most attention from "bank", and why?

    Answer"River", with about 46% of the weight. Its key matches the query of "bank" best.

  3. Why is the residual stream useful?

    AnswerEach part adds to the vector instead of replacing it, so a block that writes nothing changes nothing, and deep stacks train well.

Sources

  • Ashish Vaswani et al., Attention Is All You Need, NeurIPS 2017, abstract read 2026-11: a network "based solely on attention mechanisms, dispensing with recurrence and convolutions entirely", with 28.4 BLEU on WMT 2014 English-to-German and 41.8 on English-to-French.
  • This site's transformers/code/ and lessons for the two-thirds feed-forward share, heads that add, and the residual stream. The tiny vectors and the scores 0.07, 0.89 and 0.58 are made up for the example. This page's fact-check is pending.
Core insight: Follow one example through the stages and the system stops being magic: each stage has one job, one cost and one way to break.
Part of the learning modules collection.