Under the hood · Transformer · 8 min
Almost every AI assistant is built on one diagram. Here it is: how it learns, then how it answers.
The Transformer is the design of the model behind most AI assistants, drawn in a 2017 research paper, “Attention Is All You Need”. Scroll: the diagram stays in view and lights up one block at a time.
Two words first
- Training
- When the model learns: its numbers (its settings) are adjusted, little by little, on huge numbers of examples, before anyone uses it.
- Inference
- When you use it: its numbers no longer change; from your text, it only works out the next word, then the next.
1. Training: how a Transformer learns
The 2017 paper built its Transformer to translate. We follow it learning, on one English sentence and its French translation. An illustration: no recorded numbers in this part.
Step 1 of 6
Scroll to follow the diagram
- 01Encoder · DecoderA model built to translate, in two halvesIn 2017, researchers at Google drew this diagram for a translation model. Its left half, the encoder, reads the English sentence; its right half, the decoder, writes the French, one word at a time.ReadsThecatsleepsWritesLechatdort
- 02Before trainingAt first, it writes nonsenseBefore training, all of the model’s numbers are random: whatever it reads, it strings words together at random.ReadsThecatsleepsWritesdortdortLe
An illustration, not a recording.
- 03Outputs (shifted right) · MaskedIt guesses each next word, with what follows hiddenWe give it the English sentence and the right French, and it guesses each French word from the ones before it, all at once. The words that come after are hidden, so it can’t copy the answer.Must guess 1[start]hiddenhidden→LeMust guess 2[start]Lehidden→chatMust guess 3[start]Lechat→dort
- 04SoftmaxIts guesses are compared with the right wordsEach guess is compared with the right word. “chien” instead of “chat” is a miss: the less chance it had given “chat”, the bigger the error.Its guessesLechiendortRight wordsLechatdort
right:Lewrong:chatright:dort
An illustration, not a recording.
- 05Every blockEvery number is nudged a littleThen every number in the model, in both halves, is nudged toward what would have made the right words likelier. No rule is written down: only numbers change.
Next time, “chat” is a little likelier and “chien” a little less. Across the model, every number moves by a tiny amount.
- 06Decoder onlyMillions of sentences laterThe paper’s model learned this way from about 36 million English–French sentence pairs. Chat models learn the same way, with the right half only: they guess each next word of huge amounts of text.
About 36 million English–French sentence pairs, in the paper.
Any text → its next word, then the next, then the next…
2. Inference: how it answers you
Training is over: the numbers stay fixed. Here is what happens inside a real model while it completes one sentence, with the numbers we recorded.
Step 1 of 7
Scroll to follow the diagram
- 01Decoder onlyThe right half onlyThe model we recorded, a small open model from 2024, keeps only the right half of the diagram, the decoder, as chat models do. It isn’t an assistant: it continues a text, one word at a time.
“Attention Is All You Need”, 2017
The capital of France is …
- 02OutputsThe text is cut into tokensFirst, the text is cut into tokens: pieces of words, often whole words, as on the LLM page. Here, 5 tokens.1The2capital3of4France5is
- 03Output Embedding · Positional EncodingEach token becomes numbers, with its placeA model only computes with numbers: each token becomes a list of 896 numbers, learned in training. Each list also carries its place, or “is France of capital the” would read the same.The1capital2of3France4
… 896 numbers
- 04Masked Multi-Head AttentionEach word looks at the words before itEach word gives every word before it, and itself, a weight: how much it counts. “is” looks most at “France” (47%) and “capital” (27%); it can’t see what comes next.Those weights are attention.ThecapitalofFranceisnot written yet
- The13%
- capital27%
- of8%
- France47%
- itself5%
- 05Feed Forward · N×Then the calculation repeats, 24 timesEach word then goes, on its own, through a large block of numbers where much of what training taught is stored. Looking at the others, then this block: that makes one layer, and this model stacks 24 of them.
- 06Linear · SoftmaxEvery token gets its chanceAt the top, each of the roughly 152,000 tokens the model knows gets a chance: the fan from the LLM page, seen from inside. Spaces and punctuation are tokens too.
Chances for the next word
- Paris32%
- ______11%
- ____6%
- __6%
- :5%
- 07Output ProbabilitiesOne word, then it all starts againOne word is chosen and added to the text, then everything starts again for the next one: “Paris. It is”.
The capital of France is Paris. It is
Recorded from Qwen/Qwen2.5-0.5B on October 26, 2026, in a single run on a laptop.
Your turn
The bars show how much each earlier word, and the word itself, counts for the word you pick, with the same weights as the attention step above. Later words never show: it can’t look ahead.
The capital of France is
Pick a word
“is” looks at
“is” looks most at “France”: 47%.
- The13%
- capital27%
- of8%
- France47%
- is5%
Hype vs reality
“A Transformer understands the sentence, then answers.”
It turns words into numbers, mixes them through attention and other blocks dozens of times, and scores every possible next token.
“Attention means it pays attention like we do.”
Attention is a set of weights: for each word, how much each earlier word counts. You saw the numbers.
“A chat model reads with the left half and answers with the right one.”
A chat model has no left half. Reading your text and writing the answer are one stream of next words, through the right half only.
Under the hood
In the paper, each word asks a question (a query), every word up to it, itself included, offers a key, and the closer the match, the more that word’s value counts. The scores are divided by the square root of a key’s size (64 numbers per head), then turned into weights that add up to 100%.
- Multi-Head: in each layer, attention runs 14 times side by side, in “heads” that each weigh the words their own way. In layer 16, one looks mostly at the token before, another at the first token; the attention step showed a third, picked because it looks most at “France” and “capital”.
- Add & Norm: around each block, each word keeps what it had and adds what the block brought; “Norm” keeps the numbers from growing too big.
- Positional Encoding: the 2017 paper adds each token’s place once, at the bottom. The model recorded here marks it inside each attention block instead.
- Layer by layer: the 2017 paper stacked 6 layers, this model 24. Reading each layer’s numbers as if the answer had to come out there, the first layers give noise, bits of code and blanks like “____”; from layer 22, it would say “Paris”.
scores = query @ keys.T / sqrt(d_k) # how well each word up to it answers the questionscores[future] = -inf # masked: no looking aheadweights = softmax(scores) # the page’s percentages; with the word’s weight on itself, they add up to 100%word = weights @ values # what the word picks up from the others
Diagram redrawn after Figure 1 of A. Vaswani et al., “Attention Is All You Need”, arXiv:1706.03762 (2017).
In 3 sentences
- 1.Training sets the model’s numbers before anyone uses it: it guesses the next word of millions of examples, and every number is nudged toward the right answer.
- 2.Inference is using it: the numbers stay fixed; the text becomes numbers, goes through attention and other blocks dozens of times, and every possible next word gets a chance.
- 3.The 2017 Transformer had two halves to translate: the encoder reads, the decoder writes, in training and in use. Chat models keep only the decoder.