A visual, plain-English walkthrough

How an LLM turns your words into its next word.

Follow one tiny prompt through the full transformer path—from text, to numbers, to context, to a generated answer.

No ML background needed 6 core steps Toy numbers you can follow
One prompt, end to endReady
You ask where is italy
01Encodewords → tokens → vectors
02Thinkattention → layer stack
03Predict“Europe”
The big picture first

See the whole loop in six clicks

Use this as a quick explanation, then open any full section below when someone wants the “how.”

~60 second walkthrough
Step 1 · Tokenize

Cut the sentence into familiar chunks

The model starts by splitting text into tokens. Each token gets a vocabulary ID the model can work with.

Say it simplyThe model reads chunks of text, not letters or whole thoughts.
where is italy
whereID 8497isID 318italyID 15045
1 of 6 Open the full step ↓
01 Tokenize

Split the text into tokens

The prompt is first split into tokens: common chunks of text. Each token maps to an integer ID in the model's vocabulary. From here on, the model works with those IDs, not the raw letters.

"where is italy" → 3 tokens → 3 integer IDs
where
#8497
is
#318
italy
#15045
02 Embed · vectors

Look up a vector for each token

Each ID points to a row in a learned embedding table. That row is a vector: the token represented as numbers. Real models use thousands of numbers per token; here we show 4 so the math fits on the page.

embedding table lookup → one 4-d vector per token
where
is
italy
E  (3 × 4)
0.21
-0.13
0.84
0.30
-0.52
0.41
0.12
0.88
0.74
0.63
-0.19
0.55

New to vectors, dot products, and matrices? How vectors work covers the math on this page with small numbers.

03 Position

Add where each token sits

Embeddings know what a token is, but not where it appears. Without position, "where is italy" and "italy is where" would have the same three vectors. Adding a positional vector gives each token an order slot.

embedding + position = input to the network
where
is
italy
E
0.21
-0.13
0.84
0.30
-0.52
0.41
0.12
0.88
0.74
0.63
-0.19
0.55
+
P (pos 0–2)
0.00
1.00
0.00
1.00
0.84
0.54
0.10
1.00
0.91
-0.42
0.20
1.00
=
X
0.21
0.87
0.84
1.30
0.32
0.95
0.22
1.88
1.65
0.21
0.01
1.55
why use a position vector instead of the number 2?
Position is a vector, not a number. Each of the 4 columns is a sine/cosine wave sampled at the position index: col 0 = sin(pos) col 2 = sin(pos / 10) col 1 = cos(pos) col 3 = cos(pos / 10) pos 2 → sin(2)=0.91 cos(2)=-0.42 sin(.2)=0.20 cos(.2)≈1.00 Why waves instead of 0, 1, 2? • it must be a 4-wide vector so it can be ADDED to the embedding • sin/cos stay in -1…1, so position 5000 won't swamp the meaning • different wave speeds give every slot a unique fingerprint — and the gap between two positions stays consistent, so the model can read off how far apart two tokens are.
04 Attention

Let tokens give each other context

Attention is the transformer's main move. Each token is projected into three vectors: a Query, a Key, and a Value. A Query asks what matters, Keys are matched against it, and Values are the information that gets blended in. Below we follow the last token, italy.

In a real model, each token's vector is first multiplied by three learned matrices — WQ, WK, WV — giving three views of the same token. To keep every number on this page checkable, this toy skips that step and uses each token's vector directly as its own Q, K and V. The projection step is shown move-by-move in How attention works.

now use Q, K and V — attention in 4 steps
softmax( Q · Kᵀ / √d ) · V
↑ scores become weights ↑ Values get blended
1
Q · Kᵀ
raw scores
2.55
3.64
5.17
relatedness
2
÷ √d
scale
1.28
1.82
2.58
keeps softmax stable
3
softmax
weights · sum→1
0.16
0.27
0.57
HOW MUCH
4
× V, sum
blend Values
[1.06, 0.52,
0.20, 1.60]
WHAT · context vec
in the first three cards, each line is one token — where · is · italy
unpacking step ① — Q · K on the real numbers

K means the Keys are flipped into columns. That lets one matrix multiply compare every Query with every Key. We only need italy's row for the next-token prediction.

Q · queries are rows
where
0.21
0.87
0.84
1.30
is
0.32
0.95
0.22
1.88
italy
1.65
0.21
0.01
1.55
·
Kᵀ · keys are columns
where
is
italy
0.21
0.32
1.65
0.87
0.95
0.21
0.84
0.22
0.01
1.30
1.88
1.55
=
Q·Kᵀ · scores
where
is
italy
where
·
·
·
is
·
·
·
italy
2.55
3.64
5.17
one cell, worked out — italy's Query (blue row) · where's Key (green column):
1.65·0.21 + 0.21·0.87 + 0.01·0.84 + 1.55·1.30 = 2.55  →  repeat per column to fill italy's score row.
softmax, as bars — how much italy attends to each token
→ where
16%
→ is
27%
→ italy
57%
do the math — the italy↔italy score & softmax
1 · dot product q·k for italy vs each token (vectors from X) italy · where = .346 + .183 + .008 + 2.015 = 2.55 italy · is = .528 + .200 + .002 + 2.914 = 3.64 italy · italy = 2.722 + .044 + .000 + 2.402 = 5.17 2 · scale by √4 = 2 → 1.28 , 1.82 , 2.58 3 · softmax eˣ / Σeˣ e^1.28=3.60 , e^1.82=6.17 , e^2.58=13.20 (Σ = 22.97) → 0.16 , 0.27 , 0.57 (these are the bars above)

Now pull the content. The weights say how much to use from each token. Multiply each Value by its weight, then add the rows. The result is italy's new context vector.

weighted sum of Values · weight × V → context vector
0.16×Vwhere
0.21
0.87
0.84
1.30
0.27×Vis
0.32
0.95
0.22
1.88
0.57×Vitaly
1.65
0.21
0.01
1.55
Σcontext
1.06
0.52
0.20
1.60
do the math — the weighted sum, dimension by dimension
each output number = (weight × that token's V), summed down the column: dim0: 0.16·0.21 + 0.27·0.32 + 0.57·1.65 = 1.06 dim1: 0.16·0.87 + 0.27·0.95 + 0.57·0.21 = 0.52 dim2: 0.16·0.84 + 0.27·0.22 + 0.57·0.01 = 0.20 dim3: 0.16·1.30 + 0.27·1.88 + 0.57·1.55 = 1.60 context_italy = [1.06, 0.52, 0.20, 1.60] italy's new vector = 57% its own Value + 27% "is" + 16% "where". It started as just "italy"; now it carries context from the tokens it attended to.

↑ that's one contextualized vector — the output of attention for one head, in one layer.

Want to watch a full block run — all 12 heads, the MLP, the residual adds — one move at a time? How attention works.

05 Layers · MLP

Pass the contextualized tokens through the layer stack

Step 4 gives each token a context-aware vector. A real transformer does not stop there: each layer runs many attention heads, mixes their outputs, sends each token through an MLP (also called the FFN, feed-forward network), then passes the result to the next layer. The same pattern repeats until the top of the stack.

the real scale — many heads per layer, many layers deep
↑ to step 6 · predict
Layer 32attention + MLP
Layer 2attention + MLP
Layer 1attention + MLP
↑ contextual tokens · from step 4
⟶ zoom into one layer
one layer · attention, then MLP
x
in
head 1QKVsyntax
head 2QKVmeaning
head 3QKVorder
… h heads
concat
+ mix WO
MLP
per token
x
out
every head runs softmax( Q·Kᵀ / √d ) · V — with its own WQ/WK/WV, on its own slice of the vector (dmodel ÷ h dims). Roles shown are illustrative.
This is an MLP — also called an FFN (feed-forward network). Attention mixes information between tokens; the MLP then transforms each token's vector independently before the next layer. How the MLP works shows what those neurons store.
↺ xout becomes the next layer's xin — re-projected into fresh Q/K/V, then attention + MLP run again, layer after layer, until the top.
expanded — how the per-head vectors pop out, combine, and move on
① each head outputs its own short vector for "italy" — its dmodel ÷ h slice
head 1 · syntax
1.06
0.52
0.20
1.60
head 2 · meaning
0.40
1.12
0.05
0.88
… and so on for all h heads
↓  concatenate — stitch the head vectors side by side
② concat → one full-width vector (dmodel)
1.06
0.52
0.20
1.60
0.40
1.12
0.05
0.88
↓  × WO — lets the heads' information mix together
③ xout — blended; the heads are no longer separable
0.91
0.34
1.20
0.77
0.15
1.05
0.60
0.42
↓  MLP transforms each token vector, then residual add
Layer 2 — projects fresh Q/K/V from this vector and runs the whole block again ↑

Real models run this in many heads and many layers. Each head has its own Q/K/V matrices and can learn a different relationship; concatenation puts the head outputs side by side, and WO mixes them so later layers can combine what different heads found. The MLP then transforms each token's vector independently, and a residual add — the update is added onto the token's running vector rather than replacing it — folds the result back in before the next layer treats it as its new input. Numbers are illustrative.

06 Predict

Turn the vector back into a word

After the layer stack, each token has a fully contextualized vector. To generate the next token, the model uses the vector at the last position: here, the vector for italy.

what comes out of the stack — one final vector per token (3 × dmodel)
where
0.4-0.90.21.1-0.30.7 4096 dims
not used for next token
is
-0.20.61.4-0.80.50.1 4096 dims
not used for next token
italy
0.90.3-1.20.82.1-0.5 4096 dims
→ feeds step 6 · last position

During training, every row predicts its own next token. During generation, the current last row is the one used.

That last vector is multiplied by the output matrix to score every possible next token. Those raw scores are logits. A final softmax turns them into probabilities, the model chooses a token, appends it, and repeats.

context · output matrix → logits → softmax → next-token probabilities
Europe
logit 3.69 · 45%
Rome
logit 3.39 · 33%
Mediterranean
logit 2.61 · 15%
located
logit 1.73 · 6%
banana
logit −1.34 · 0.3%
Generated answer
Italy is in southern Europe, on the Mediterranean.
C Extra · KV cache

Follow-up questions reuse cached Keys and Values

An LLM does not keep human-style memory between turns. The app sends the conversation so far again as one long token stream. To avoid recomputing all of it, the system keeps the Keys and Values from earlier tokens. New tokens make fresh Queries that attend back into that cache.

"where is italy" → "Italy is in Europe" → "where is europe" · one growing stream
turn 1 · prompt
wherepos 0
ispos 1
italypos 2
turn 1 · answer
Italypos 3
ispos 4
inpos 5
Europepos 6
turn 2 · prompt — computed now
wherepos 7
ispos 8
europepos 9
cached — K/V reused, not recomputed new this turn — runs through the pipeline
why Q isn't cached — Queries are rows, Keys/Values are columns
Keys / Values · cached as columns ↓
where
is
italy
where
is
italy
europe
Keys/Values = columns — cached, reused by every new row "europe" Query = a new row — computed now, reaches every cached column past Queries = old rows — each ran once, then discarded

Adding a token adds a row (its Query) that reaches across all the cached columns (Keys/Values). The old Query rows already ran and handed off their output — nothing ever reads them again — so only the K/V columns are worth keeping. (europe also drops its own K/V in as a new column for whatever comes next.)

europe Query · pos 9
attends back to
KV cache · stores K + V of positions 0–6
whereisitaly Italyisin Europe

↑ "europe" (pos 9) can attend straight to the "Europe" the model itself wrote at pos 6 — that's how turn 2 "knows" turn 1.

what's actually stored vs recomputed
cached across turns K and V of every past token (for a fixed prefix, those past K/V do not change) not cached • the Query of past tokens — only needed once • the final context vector — recomputed for new tokens the catch the cache is only an optimization. Drop it, re-feed the same transcript, and the model recomputes the exact same K/V (this re-processing is called "prefill" — keeping it warm is what "prompt caching" bills for). Nothing is lost; it's just cheaper to keep.
+ Beyond vanilla

What newer architectures try to change

Everything above is the standard transformer path used by models such as GPT, Llama and Claude. New architecture work usually targets the two expensive parts: step 4, where attention compares tokens to other tokens, and step 6, where generation normally happens one token at a time.

replaces step 04 · attention

Mamba & SSMs

all-pairs attention → a running state

Instead of building the all-pairs attention matrix from step 4, a state-space model scans left to right and updates a fixed-size memory at each token. The model decides what to keep and what to forget as it moves.

Changes: attention's O(n²) cost becomes closer to linear work with length.

transformer every pair · n²
Mamba t₁ S t₂ S t₃ one state · n
state-space models selective state hybrids
replaces step 06 · the loop

Diffusion LLMs

left-to-right loop → parallel refinement

Instead of emitting one token at a time, a diffusion language model starts with a block of masked placeholders and refines many positions in parallel over several passes.

Changes: generation can fill a block in a fixed number of refinement passes instead of one full pass per word.

transformer Italy is in Europe 4 passes
diffusion Italyin Italy is in Europe ~3 parallel passes
masked generation parallel refinement fewer serial steps
adds to step 04 · attention

Titans & neural memory

attention window → learned long-term memory

Attention is a precise short-term lookup over whatever tokens are still in the window. Titans (arXiv:2501.00663) adds a small network that compresses older context into its own weights as the sequence runs — surprising tokens update it more — and the model queries that network for information from beyond the window.

Changes: context isn't limited to the attention window — and unlike the KV cache (which just stores exact past K/V to skip recomputation), this memory learns and compresses.

neural long-term memory test-time learning memory as context / gate / layer
Most "new architecture" arguments are about steps 4 and 6: how tokens share context, and how text is generated. Steps 1–3 usually stay recognizable.
Go deeper

Four deep dives, each with its own visual guide

How vectors work The math primer: vectors, dot products, and matrices, worked out with small numbers.
How attention works One full block, move by move: all 12 heads, the MLP, and the residual adds.
How the MLP works Where facts like France → Paris actually get stored.
How interpretability works J-space, sparse features, circuit tracing, causal tests, and the current research frontier.