Beginner friendly
Chat LLM.
Follow a sentence through the machinery, then meet the training that shapes an assistant.
A language model generates tokens from a learned distribution. Post-training can encourage instruction following, but cannot guarantee a correct answer.
Or jump to the hands-on bit ↗Before you poke the machinery
Try this: Type something into the tokenizer. Try a word, an emoji, and a bit of code.
The honest boundary: This is a simplified decoder-transformer path. A deployed chat product can also use search, tools, multimodal inputs, and other systems.
Your pace: Scroll, use the chapter links, or jump around. Reading works without JavaScript; interactive controls need it. Motion preferences are respected.
From next-token prediction to a conversation
The shared language-model machinery, plus training that encourages assistant behavior.
A chatbot can feel like a conversational partner. To understand its mechanics, start with a less mysterious object: a learned distribution over possible next tokens, conditioned on the conversation.
We’ll follow tokenization, embeddings, attention, and sampling. Then we’ll look at post-training: examples and feedback that encourage responses to follow instructions and meet other goals.
A pretrained model can already answer some questions. Post-training makes the assistant behavior more consistent; it does not install a truth detector or make every answer helpful, safe, and correct.
Try the controls as you go. A punctuation mark is a perfectly good experimental subject. If you’ve visited another language-model track, familiar scenes are reused so you can spend your curiosity on the new part.
3 tokens·10 characters
The arithmetic is real; the model and examples are deliberately small. Change a control and watch the result.
Step 1 — Tokenize
Text becomes tokens first. Their boundaries depend on the tokenizer.
The model can’t read characters the way you do. Before anything else, your text is split into tokens — chunks that are often whole words, sometimes word-pieces, sometimes single characters.
This is done with byte-pair encoding (BPE): start from raw bytes and
repeatedly merge the most common adjacent pair into a new token. Frequent
sequences like " the" become a single token; rare ones get fragmented.
Try it → type in the panel. A few things worth discovering:
- Try “strawberry”, with and without a leading space. The split depends on the tokenizer. Tokens do not usually correspond one-to-one with letters; that can make character-level tasks awkward, but it is not a complete explanation of every counting error.
- A leading space is part of the token:
" Paris"and"Paris"are different tokens. - Numbers, code, emoji, and other languages tokenize very differently. Toggle GPT-4 (~100k vocab) vs GPT-4o (~200k) and compare the counts. A larger vocabulary need not use fewer tokens for every string, and token count alone does not determine capability or cost.
Each token is really just an integer — its row number in the model’s dictionary. These IDs select token embeddings; the model also uses positional information and, in some systems, other inputs.
Nearest to king: prince, princess, queen
This panel uses authored values or a schematic to explain the idea. It is not a live measurement or a performance benchmark.
Step 2 — Embed
Each token becomes a vector — a point in space where meaning lives in directions.
A bare token ID like 2746 means nothing on its own. So the model looks it up in
a giant table and swaps it for a long list of numbers — a vector. For GPT-2
small that’s 768 numbers per token; modern models use thousands.
These vectors are learned (in the gradient-descent sense — the
How Models Learn track shows
the machinery). Relationships can appear in learned representations, but distance depends on
the model, layer, and comparison. In this illustrative panel,
each dot is a word; click one to light up its nearest neighbors — dog sits
by puppy and kitten, far from algorithm.
Because meaning lives in directions, you can sometimes do arithmetic on words. Toggle the analogy: king − man + woman points toward queen in the authored example.
Honest asterisk: this famous example is hand-picked. It only works cleanly when you exclude the input words, and it describes static word vectors — a real LLM’s internal representations are contextual and messier. It’s a beautiful hint that meaning is geometric, not a hard law.
One thing embeddings don’t carry yet is order — “dog bites man” and “man bites dog” would look the same. Models incorporate positional information; some add position embeddings, while others encode relative positions within attention.
One attention head, causal-masked (each token attends only to itself and earlier tokens). Row i = how much token i attends to each token.
This panel uses authored values or a schematic to explain the idea. It is not a live measurement or a performance benchmark.
Step 3 — Attention
Each token looks at the others and pulls in the context it needs. This is the engine, repeated dozens of times.
To understand a word you need context. In “The animal didn’t cross the street because it was too tired,” what does “it” refer to? Self-attention lets each token look at the other tokens and decide how much to pull from each.
Mechanically, every token produces three vectors: a Query (“what am I looking for?”), a Key (“what do I offer?”), and a Value (“what I’ll hand over”). A token’s query is compared against every key; the scores go through a softmax into weights that sum to 1; the output is that weighted blend of Values.
Do it for every pair and you get the attention matrix in the panel — row i is how much token i attends to each other token. Notice the greyed upper triangle: a token may only look backward, never at words it hasn’t generated yet. That causal mask is what makes the model a left-to-right predictor. Hover any cell to read the weight.
Two things to keep in mind as we simplify:
- Real models run many attention heads in parallel (GPT-2 small: 12), each learning a different kind of relationship, plus a feed-forward layer that applies further learned transformations. Knowledge is not neatly isolated in just one component.
- This whole block is stacked many times (12 in GPT-2 small; a model-dependent number in larger systems). The token’s vector gets refined at every layer.
After the last block, the model is ready to turn that vector back into a word.
The capital of France is ▍
~50,249 other tokens share the rest of the probability mass.
greedy pick: ·Paris
The probability math and sampling are live. The starting logits are authored teaching values, not a measured model prediction.
Step 4 — Sample the next token
The final vector becomes a probability for every token. How you pick from it is the temperature knob.
After the last block, the model takes the final token’s vector and scores every
token in its vocabulary — about 50,000 scores in GPT-2, and a different count in other models. Those
scores are called logits. A
softmax squashes them into probabilities that sum to 100%. The panel computes a distribution from authored example scores, not a live
model prediction: “87% chance the next token is Paris, 3% the, …”
How do we actually pick one? That’s what the controls in the panel do:
- Greedy — always take the top token. Deterministic for fixed scores, but not guaranteed correct or safe.
- Temperature — rescales the logits. Drag it down and one bar dominates (conservative, near-deterministic at 0). Drag it up and the bars flatten (more diverse, riskier). Temperature 0 does not mean “more true” — just more predictable.
- Top-p (nucleus) — keep only the smallest set of tokens whose probability adds up to p, then sample from those. The struck-through bars are the tail it’s ignoring.
Press Sample! a few times to feel the randomness — then set temperature to 0 and watch it become deterministic.
Then the crucial move: whatever token we pick gets appended to the sequence, and the whole pipeline runs again to produce the next one. This autoregressive loop — one token at a time — is how a whole paragraph gets written.
And it’s exactly this loop that a reasoning model turns into something more.
How do I make pizza? How do I make bread? How do I make pasta at home? 10 recipes you can make tonight. How do I make pizza dough from scratch? How do I make pizza without an oven? Subscribe for more...
A pretrained model learns text patterns and may follow instructions already. This authored example illustrates an unhelpful continuation.
You be the labeler
Same question, two candidate answers. Which is more helpful?
This panel uses authored values or a schematic to explain the idea. It is not a live measurement or a performance benchmark.
Step 5 — From autocomplete to assistant
Examples and preference feedback shape assistant behavior. There is more than one training recipe.
A pretrained language model learns continuation patterns. It may answer a question, continue a list of questions, or imitate some other text format. Post-training encourages more consistent assistant behavior.
Flip through the panel’s written examples. They illustrate a training progression; they are not before-and-after outputs from an actual experiment.
- Pretraining: optimize a text-prediction objective over a training corpus. This builds many capabilities, without guaranteeing instruction following.
- Supervised fine-tuning (SFT): update parameters using examples of desired responses. It’s still parameter learning—the foundations track supplies the mechanics—with a different data mixture and objective.
- Preference training: encourage responses preferred by raters or other feedback systems. In the InstructGPT pipeline, a reward model was trained on human comparisons, then used for reinforcement learning with PPO.
DPO is a different route. It optimizes a preference objective directly, without separately fitting that reward model and running the same PPO stage. Neither method guarantees honesty, helpfulness, or safety in every situation.
Try “you be the labeler.” Your choice stays in this local illustration; it does not train or provide feedback to an external model. Notice how the choice of examples and evaluation criteria helps define what “better” means.
One rabbit hole
leads to another.
No quiz at the door. Pick whatever made you curious.