How a Language Model Works
Train a tiny language model, test worked solutions, and explore a tool-using loop.
Explore How a Language Model Works; Interactive
Explore How a Language Model Works
Train a tiny transformer and inspect its predictions. Compare direct answers with worked arithmetic, then explore an agent loop with real tools and a clearly labeled scripted policy.
The activity runs in your browser without an AI service. Use its Guide button for instructions and Settings for presenter notes, presentation mode and reset. Open it full screen when you want more room.
Learning path
Learning path
Train a tiny transformer and inspect its predictions. Compare direct answers with worked arithmetic, then explore an agent loop with real tools and a clearly labeled scripted policy.
Presenter notes and learning guide
Presenter notes and learning guide
How a Language Model WorksDemo 2 of 17 · Part one · 20 minutes
You need: one browser and index.html.
It runs offline. For projection use Settings → Presentation mode.
Start fresh: the untrained model is useful evidence.
Learning goals
What a learner knows at the end of each stage — these are the three Takeaway cards, verbatim.
- LLM. Training changes the parameters; inference runs the frozen parameters forward to score every possible next token. A small model trained on a little text learns the shape of the language long before it learns to say anything true.
- Reasoning. Room to write intermediate steps lets the same machinery reach answers it cannot produce in one jump — a real, measurable gain on this task, and not the same thing as understanding arithmetic.
- Agents. Tools and a loop turn prediction into action, and the context window bounds what the loop can still see — a run can fail for want of memory, not for want of ability.
Where this sits on the path
Demo 1 showed models that play and handed forward one idea, in its own words: a model is a representation that can be trained, and architecture, training method and model type are three separate questions. Its last stage ended on the transformer; this demo trains one. It shows a model that predicts text, then what it takes to reason and to act. It hands forward two things: a model reads its context (Demo 5, Prompting) and a score is a measurement under conditions — the exam rows here — which is how Demo 3 asks you to read published benchmarks.
One question for the session
What changes when a text predictor is given more structure, more checking, or tools? This page shows three bounded mechanisms: a tiny language model learning next-character prediction, arithmetic experiments with written work and checking, and a tool loop with a scripted action selector. Use Details for the deeper architecture and implementation notes.
“Keep asking what the program learned, what it is calculating now, and what part is a teaching fixture.”
LLM · Language model — 6 minutes
Question
How can adjustable numbers turn recent text into a next-character prediction?
Predict prompt
After all four eras, can it do simple math, like 2 + 2? The answer is no: its vocabulary has no digits, so 2+2= reaches it as blank spaces. Have someone type 2+2= into the prompt box to see it. The reveal says so on the card once Era 4 finishes, and Stage 2 trains the same kind of model on sums.
Activity
Expected observation
Training changes the weights, loss declines, and the model’s next-character distribution changes. Its samples can become more patterned while still being limited by one short, original story.
Check-yourself answer key
Era 4 writes more like the story than Era 0. What changed? Its parameters. Not a lookup of stored passages (nothing is stored to retrieve — the whole model is 42,458 numbers) and not a bigger vocabulary (Tokenize text shows the same 26 characters at every era).
Teaching limit
This is a character-level transformer trained from scratch on a tiny corpus. It demonstrates a transformer learning mechanism, not general language ability or a claim about every frontier system.
Reasoning · Worked steps — 8 minutes
Question
How do written intermediate steps and checking change a small model’s behavior on addition?
Predict prompt
Same exam, which scores higher — answers-only or worked steps? Take the vote before pressing Run experiment; the card echoes each guess and settles it from the two scores on screen.
Activity
Expected observation
Written steps provide smaller targets for the model to predict. Sampling plus checking can improve the chosen answer even when weights stay fixed.
Check-yourself answer key
The worked-steps model scores higher. What does that show? That generating intermediate steps gives the same machinery more room to compute. The card’s two wrong options are wrong in opposite directions and both are common in a room: “it understands arithmetic” (too strong — both are next-token predictors, and the fine-tuning is supervised, not reinforcement learning) and “nothing changed, both just predict the next token” (too weak — the two exam scores on the page are the evidence against it). Give both corrections equal time.
Teaching limit
The checker validates column arithmetic from the addends. The optional accepted-example round also retains chains matching the generated answer, then uses ordinary next-token updates on those examples. That is rejection-selected supervised fine-tuning, not a policy-gradient reinforcement-learning update.
Agents · Tool-using loop — 5 minutes
Question
What does a system need in addition to text prediction to take actions and use results?
Predict prompt
Same task with Visible context set to Tiny — does the run still finish? Take the vote before the tiny run; the card settles it when the run ends.
Activity
Expected observation
Tool results become new text in the context. The small window changes what the selector can use, so the same task can fail when its goal is no longer visible.
Check-yourself answer key
On Tiny the same policy stops without finishing. Why? The goal scrolled out of the visible context. Not a weak policy (it is the same hand-written policy and it finishes on the full context) and not the calculator error (that error is real, deliberate, and recovered from in both runs).
Teaching limit
The action selector in this activity is a scripted policy, including the calculator mistake and retry; it is not a learned model. The loop, local tools, observations, and context truncation are the genuine mechanisms on display.
Optional slow experiments and close
Let Train the model and Train and compare continue while you discuss their live displays; each can take a few minutes. Try more attempts and Fine-tune on accepted examples are optional extensions. Do not promise a particular score or curve: the page uses seeded, bounded demonstrations.
“A language model predicts. Reasoning experiments add written work, checking, or extra attempts. An agent adds a loop and tools; in this activity, its action choice is deliberately scripted.”
- Which claim came from a learned model, a fixed calculation, or a scripted policy?
- Why can extra attempts help without changing weights?
- What would need to change before this agent activity used a learned action selector?
Misconceptions to expect
Presenter controls
Guide opens from the header and on entry. Settings contains Open Presenter Notes, Presentation mode, and Reset. Reset puts the whole demonstration back to its fresh state in place, without reloading the page: the transformer is untrained again, every era is gone, the Reasoning models and exam tables are cleared, and the Agents run is back at turn one on the full context. Presentation mode stays as you set it. Open printable guide opens the rendered copy of this same guide.