▶ Sign up free for the video, slides & prompts
The lesson below is open to all. Enter your email once to unlock every video, slide deck, prompt and code example on this device.
Checking…
How LLMs Really Work · Lesson 4 of 15 · 2 min read · 6 min video
The Transformer, step by step
Follow one sentence through tokens, attention, layers and the next-token guess.
📄 Download slides (PDF)· free sign-upThe transformer is the architecture behind ChatGPT, Gemini, Claude and Llama. It's attention plus a few supporting parts, stacked many times. Let's follow one input, "Tell me about Vizag", all the way to the next word.
Step 1: Tokens
The text is split into tokens: whole words, word pieces or characters. "Tell me about Vizag" might become "Tell", " me", " about", " Viz", "ag". Each token has an ID number from a fixed vocabulary of around 50,000 to 200,000 entries.
Step 2: Embeddings and position
Each token ID is swapped for its embedding vector. Because attention by itself has no sense of order, the model also adds position information, so "dog bites man" differs from "man bites dog". Modern models usually encode position by rotating the vectors (RoPE) rather than adding a fixed pattern.
Step 3: The transformer block
Each block does two things, each wrapped in safety rails:
- Masked multi-head attention: every token gathers information from earlier tokens
- Feed-forward network: each token's vector goes through a small neural network on its own. This is where much of the model's stored knowledge lives
The safety rails:
- Residual connections: each step adds its result to the input rather than replacing it, so information and learning signals flow easily through many layers
- Layer normalisation: keeps the numbers in a stable range
Step 4: Stack it
One block makes each token's vector a little more informed. Large models stack dozens of blocks, often 30 to 100 or more. Early layers handle spelling and grammar. Later layers handle meaning, facts and reasoning patterns.
Step 5: Predict the next token
Take the final vector of the last token. Multiply it against every vocabulary entry to get one score per possible next token, then turn scores into probabilities:
- " is": 31%
- " (": 12%
- ",": 9%
- … thousands more, each tiny
Step 6: Pick, append, repeat
The model picks a token (how it picks is the sampling lesson), adds it to the input, and runs again. One token at a time, a full answer appears. Systems save the attention keys and values from earlier tokens (the KV cache) so each new token doesn't redo all the work.
Encoder, decoder, decoder-only
The original 2017 transformer had an encoder (reads input) and a decoder (writes output), designed for translation. Today's chat models are mostly decoder-only: one stack that reads and writes the same stream. Encoder-only models like BERT are still widely used for classification and embeddings.
In one line
A transformer turns tokens into vectors, refines them through many blocks of attention and feed-forward layers, then predicts the next token, over and over.
Share with a friend