Open the black box
From scattered tokens to surprisingly fluent sentences, follow the hidden steps behind an AI answer. Play with the pieces, watch the patterns emerge, and discover how language models really work.
Your route through the model
Start with Tokens and follow the lessons in order. You do not need to code or know calculus: a vector is a list of numbers, and a probability describes how much of a distribution belongs to an outcome. Each lesson includes an experiment, a learning goal, and a question with feedback.
How the demos fit together
These are separate experiments illustrating connected ideas. They use different vocabularies, vectors, and architectures; one demo's output is not passed into the next. Within a real model, the tokenizer, embedding table, transformer, and output vocabulary must match.
| Lesson | Demo | What it teaches |
|---|---|---|
| Tokens | o200k_base byte-pair tokenizer | Text → vocabulary IDs |
| Embeddings | 139 GloVe word vectors, 50 dimensions | Static word geometry |
| Attention | DistilBERT with WordPiece tokens | Bidirectional transformer attention |
| Backpropagation | Small network learning XOR | Gradients → weight updates |
| Training & scaling | Character MLP with 3 characters of context | Learning and held-out prediction |
| Generation | GPT-2 small with r50k_base tokens | Causal prediction and sampling |
| Run one yourself | Arithmetic from llama.cpp's quantization tables | Parameters and bits → memory |
A few words you will meet
- Parameter / weight
- A learned number that controls a model's computation.
- Representation / activation
- A value computed for the current input; it changes with context.
- Logit
- A raw score for a candidate, before conversion to probabilities.
- Loss
- A numerical penalty for a prediction; training tries to reduce its average.
- Gradient
- How loss changes with a small change in a parameter.
- Context
- The information supplied for this prediction, within the model's input limit.
Lesson 01 / 09
Tokens
Your goal: Explain why token IDs depend on a tokenizer and why token count differs from character count.
A language model predicts text using numerical computation. Before it can use a prompt, a tokenizer turns the text into vocabulary labels called token IDs. Start with the guided experiment below; predict before revealing the results.
Preparing your local learning session…
After the experiment: why tokens matter in practice
Tokens can contain multiple letters, one character, or only part of a character's UTF-8 bytes. The model receives IDs, not a separate spelling/counting result. Counting letters can therefore require extra reasoning, but tokenization does not make counting impossible or establish the only cause of counting errors.
A context window limits the tokens available at once. Instructions, conversation history, retrieved text and generated output use token space. Many APIs charge for input and output tokens. The counts here cover only entered text; chat applications can add role markers and framing tokens.
This module uses the real o200k_base byte-pair encoding vocabulary. The GPT-2 demonstrations later use r50k_base. Exact counts and IDs depend on the tokenizer; the demonstrations are not one executable pipeline.
Further exploration: the same idea written different ways
These examples are counted live with the same tokenizer. Compare tokens and user-perceived characters. Language, script and formatting affect the entries used; these examples do not establish a universal ranking of languages.
Character counts include spaces and punctuation. Combined emoji and accented letters each count as one character.
The additional recognition check below is optional practice; it does not change the guided module's learning evidence.
Check your understanding
Sources & further reading
Lesson 02 / 09
Embeddings
Your goal: Distinguish a token's learned input vector from its later representation in context.
Tokenization left us with a list of ids: plain integers, one per chunk of text. But a model can't do math on “token #4826” as if the number meant anything. The first thing it really does is swap each id for a vector: a list of numbers that places the token at a specific point in space. Those input embeddings start the model's computation. Later layers produce new representations for each token using its position and context.
Why embeddings?
A token id is arbitrary. “cat” might be 9707 and “dog” 3899, but those numbers are just slots in a vocabulary, being close in id says nothing about being close in meaning. An embedding replaces that arbitrary label with a learned position in space, fitted during training so that words used in similar ways land near each other. Now distance and direction carry meaning: “cat” sits beside “dog”, both far from “Tuesday”, and the model has something it can actually compute with.
Explore a word's neighborhood
Pick a word, follow its neighbors, and compare the numbers behind their meaning.
Search this teaching collection of 139 words.
Word in focus
cat
AnimalsSix closest neighbors
Cosine similarityWithin this collection. Tap a word to explore from there.
Bars run from −1 to +1. The center mark is 0.
How close are two words?
Your word
cat
0.922cosine similarity
Closer to +1 means the vectors point in similar directions. This is not a probability. Words with opposite meanings can still score highly when used in similar contexts.
Word arithmetic
Take one vector, subtract a second, add a third, then rank every word by how closely it points the same way. Try a preset or pick your own words.
Classic demos quietly drop the three input words from the answers. Turn this on to see the trick. With it off, you see what the arithmetic actually lands nearest.
king − man + woman is closest to king
Cosine similarity- kinginput word0.886
- queen0.861
- daughter0.768
- prince0.764
- princess0.751
- father0.731
Bars run from −1 to +1. The center mark is 0. Purple bars are the words you typed in.
The gender direction is real: subtracting man and adding woman pulls the result toward female words. But the result often stays close to where it started, so the starting word can still come out on top.
The numbers behind the words
Each fingerprint shows all 50 learned values, 25 per row. Both words use the same color scale.
A single cell is not an “animal” or “emotion” switch. Meaning is encoded in the full pattern.
These are genuine pretrained GloVe embeddings from Stanford, using 50 numbers per word. The explorer searches a curated collection of 139 words. Neighbor rankings and comparisons use all 50 values, and the fingerprints show those same values directly. GloVe is an older word embedding model used here to explain the idea. A language model learns its own input embeddings and updates token representations through transformer blocks. These whole-word GloVe vectors are a separate teaching dataset; they are not the embeddings of the IDs from the Tokens lesson.
What do the numbers actually mean?
It's tempting to imagine each dimension stands for one human idea, an “animal-ness” axis, a “royalty” axis. It almost never works that way. Individual dimensions rarely line up with a single concept we'd name. Meaning lives in the combination of many dimensions at once: it's the overall pattern of the vector, and its position relative to every other vector, that encodes what a word is like, not any one number on its own. That's also why the interesting structure shows up as directions and clusters rather than as readable coordinates.
Similar usage does not mean identical meaning
Try comparing happy with sad. They describe opposite feelings, but both appear in similar sentences about emotions. Their vectors can therefore be close. An embedding captures patterns of use, not a dictionary definition or a guarantee that two words are interchangeable.
Cosine similarity measures the angle between the full vectors. A score of +1 means the same direction, 0 means perpendicular directions, and −1 means opposite directions. It is a geometric score, not a percentage of shared meaning.
Embeddings are everywhere, not just in LLMs
The same idea, turning something into a vector whose position encodes meaning, powers a huge amount of modern software. Semantic search finds documents whose embeddings sit near your query's. Recommendation systems place songs, products, or videos in a space and suggest nearby neighbors. Retrieval-augmented generation (RAG) can use a dedicated embedding model to compare a query with passages, then supply selected text to a language model. Document embeddings are trained for a different use from token input embeddings. Similarity helps find candidates; it does not guarantee that a passage answers the question. The Assistants lesson follows that process further.
Check your understanding
Sources & further reading
Lesson 03 / 09
Attention
Your goal: Trace queries, keys, values, masking, and the other operations inside a transformer block.
Embeddings gave every token a starting vector, but that vector is the same no matter what surrounds it. Position information and transformer blocks turn that starting vector into a representation of this occurrence in context. Attention computes weights over allowed positions and mixes information from them.
Why attention?
Without it, bank would carry the exact same vector in “river bank” and “savings bank”, since the embedding table has only one entry for the word. Attention is what lets the surrounding context influence the representation: “river” and “savings” supply different information. More precisely, attention mixes learned value vectors, then projects and adds the result to the representation. The rest of the block transforms that result further.
Try it: click a word and watch what it looks at.
Load the model, then click it in the default sentence, and compare the weights on animal and the other words. The brighter a word, the larger its displayed attention weight. Change the sentence or layer and observe how that pattern changes.
Kept to 14 words, since the model runs live in your browser, so short sentences stay snappy.
These weights are not a similarity heuristic: the demo downloads a real DistilBERT transformer with its own WordPiece tokenizer. The displayed weights average heads, combine subword pieces, remove framing tokens, and renormalize over the displayed words. They summarize real attention rather than showing an individual head's untouched distribution. A bright word shows a large mixing weight; it does not prove a grammatical link or explain the model's final decision.
One catch: this model looks both ways
Notice that it can attend forward to tired, a word that comes later in the sentence. That's because the demo runs a bidirectional model (DistilBERT), which sees the whole sentence at once, handy for showing the mechanism clearly. GPT-2 and other autoregressive decoder models use causal attention: each position is masked from future positions, so it can only look back at itself and the words before it, never ahead. Attention there flows in one direction only.
Many heads, many kinds of looking
A model doesn't run just one of these attention passes; it runs several in parallel, called heads. Each head has its own learned projections and can emphasize different patterns, such as nearby positions or particular relationships. Heads do not have fixed human-assigned jobs, and their behavior depends on the model, layer, and input. The demo averages them together, which can hide differences between individual heads.
And it happens layer after layer
One round of attention isn't the whole story either. The model stacks the same mechanism in layers (DistilBERT has six), and each layer refines the token representations before passing them up. Each block also contains a feed-forward network, residual connections, and normalization. Patterns across layers can differ; a later layer is not guaranteed to reveal a clearer grammatical relationship. Flip open step through the layers to watch the same sentence's attention shift as it moves up the stack.
Inside a transformer block
Step through a decoder like GPT-2. This uses its normalization-before-sublayer order; DistilBERT places normalization differently and allows attention in both directions.
1. Token + position
Look up each token's learned embedding. GPT-2 adds a learned vector for its position. This gives the same token different starting representations at different positions. Some newer architectures instead apply rotary position information to queries and keys.
Work through one attention head
These three words stand in for three tokens. The two-dimensional Q, K, and V vectors below are hand-chosen examples of what learned projections could produce. They come from a teaching calculation, independent of DistilBERT's weights.
Choose the position to update:
Q for bank = [1, 1]
| Token | Key K | Value V | Q · K | Score ÷ √2 | Weight |
|---|---|---|---|---|---|
| river | [1, 0] | [2, 0] | 1 | 0.707 | 50.0% |
| bank | [0, 1] | [0, 2] | 1 | 0.707 | 50.0% |
| rises | [1, 1] | [3, 1] | 2 | masked (−∞) | 0.0% |
weighted sum of V = [1.000, 1.000]
With bank selected and the mask on, river and bank receive 50% each, giving 0.5 × [2, 0] + 0.5 × [0, 2] = [1, 1]. Turn the mask off: rises can now contribute. These weights sum to 1 over the allowed positions; the result is a value-vector mixture, which later operations project and add to the running representation.
Optional math: the attention equation
Q = XWq, K = XWk, V = XWv; Attention = softmax(QKᵀ / √d + mask)V
X contains the input representations and the W matrices are learned parameters. A masked score of −∞ gets zero softmax weight. Attention changes activations for this input; the W matrices change during training.
Check your understanding
Sources & further reading
Lesson 04 / 09
Backpropagation
Your goal: Separate computing gradients with backpropagation from applying an optimizer update.
Attention, embeddings, all of it runs on weights, and at the start those weights are just random numbers. A forward pass produces an output, and training compares it to the correct answer to get a single number: the error, or loss. But one error number doesn't obviously tell you how to fix any particular weight. Backpropagation computes how that loss changes with each weight: its gradient. An optimizer uses those gradients to choose the weight updates.
Why backpropagation?
A real model can have billions of weights. You could imagine testing each one: nudge it, run the whole network again, see if the error went down, but that means billions of full forward passes per step, which is hopeless. Backprop gets the answer for every weight in a single backward pass. It uses the chain rule from calculus to share work across the network: it figures out how wrong the output was, then hands each layer its share of the blame, reusing the layer above instead of recomputing from scratch. That efficiency is the whole reason training large networks is even possible.
Try it: run one prediction, then send the error back.
Click Step forward to push an input through the network to a real prediction, then Step backward to watch the error compute a gradient for every weight and nudge each one. Then hit Train and watch the loss fall.
Ready. Step forward runs the current weights on the highlighted example below.
| x1 | x2 | target | prediction | |
|---|---|---|---|---|
| 0 | 0 | 0 | 0.52 | click to trace |
| 0 | 1 | 1 | 0.52 | click to trace |
| 1 | 0 | 1 | 0.51 | shown above |
| 1 | 1 | 0 | 0.51 | click to trace |
Every point is the network's real average error, recorded after an actual weight update. Compare the curve over many steps; individual updates are not guaranteed to reduce loss for every initialization.
This is a real network: two inputs, a hidden layer of three neurons, one output, learning XOR (output 1 only when the inputs differ), a task a single linear classifier cannot solve. The predictions, loss, gradients on each edge, and the weight updates are all computed live in your browser with actual chain-rule math. Nothing here is a pre-recorded animation; hit Reset and it learns again from new random weights.
Gradient descent: many small nudges
A gradient is a local rate of change of loss with respect to a weight. Plain gradient descent subtracts learning rate × gradient from each weight. With a sufficiently small step this moves downhill; a large step can overshoot. A single step doesn't fix the network; it just moves every weight a little bit downhill. Do that over and over and the errors shrink, which is exactly what the falling loss curve is showing you: not one clever fix, but thousands of tiny, honest corrections adding up.
The same thing, at unimaginable scale
Every weight, in every layer, gets its own gradient and its own nudge, all at once, every single training step. The toy above has around a dozen weights, so you can watch each one move. A transformer has parameters in embedding tables, attention projections, feed-forward networks, and normalization operations. The chain rule applies across those operations too. The XOR network illustrates differentiation and updates; it is a different architecture from an LLM.
One honest caveat: the backward pass computes gradients by the chain rule. The operations being differentiated depend on the architecture. The update rule is where real training differs. Instead of the plain fixed-size step the toy above takes, LLMs use smarter optimizers like Adam (and AdamW), which add momentum and give each weight its own adaptive step size. The gradients come from backprop; Adam just decides how to spend them.
Check your understanding
Sources & further reading
Lesson 05 / 09
Training
Your goal: Compute a next-token loss and distinguish learning the training data from generalizing to unseen data.
In the last lesson, one backward pass nudged every weight a tiny bit. That single update barely changes anything. Training is that exact loop: forward pass, measure the error, backprop, nudge, repeated millions of times over an enormous amount of text. Do it enough and a network of random numbers gradually turns into something that can write.
What does an LLM train on?
A common pretraining objective for causal language models is predict the next token. Show the model a stretch of text with the next piece hidden, let it predict a distribution, and score the probability assigned to the observed next token. Training text can include books, code, and web pages. Many useful patterns develop through prediction, while post-training can further teach instruction following and task-specific behavior. Other model families can use different objectives, such as masking tokens.
Watch a real model learn, live.
Below is a tiny language model training in your browser: no pretrained weights, no server, no canned animation. Press Start training and read the sample text as the loss curve falls: it goes from random characters, to strings with the right letter frequencies, to word-shaped fragments, to real words. It is a character-level feed-forward network (MLP) with three characters of context, rather than a transformer. The prediction, loss, and gradient-update loop illustrates the same general learning process.
Try it: press Start training and watch the model learn to write.
It starts knowing nothing: the sample text is pure random characters. As the loss curve falls, watch the samples turn into letter-frequency soup, then word-shaped fragments, then real words and phrases. Same predict-the-next-character task, repeated thousands of times.
,yl!g'!aatg.klyfqkqmwphbc.ymi!fnbbjbxpq'eey?g gk-cv'ul!:; y,?.vqyih'lqh?:imkoxn.ys,sy?a'h't fpt xp t gdxlaig,qqtv?wohq ba,-phdsylipohailb-r:o! k xcr;.anpky ;si:d v-lluip,; maybwdk
Generated live: the model predicts a next-character probability for the last 3 characters and one is sampled from it, repeatedly. Nothing here is pre-written; reset and it learns from a brand-new random network.
How the writing evolved
A saved sample from each stage of training, so the progression is visible at a glance.
kb:!u.yy:-ddxerogu!t tg-a.-iwn'qighwkcvvl?jiey:cgqdhphv'j jgkhpxdwhgmtoj?u;!stu?xjgms m;ftdh' ttnwx,elkavb -sm
,yl!g'!aatg.klyfqkqmwphbc.ymi!fnbbjbxpq'eey?g gk-cv'ul!:; y,?.vqyih'lqh?:imkoxn.ys,sy?a'h't fpt xp t gdxlaig,q
What you're actually training
A tiny character-level feed-forward language model (MLP), 4,155 parameters, a 35-character vocabulary, learning from 4.7 KB of text (Excerpt from Alice's Adventures in Wonderland by Lewis Carroll (1865, public domain).). It reads the last 3 characters and predicts the next one; gradient descent adjusts its parameters each step. It has no attention layers; the short context limits it to local character patterns. Its loss curve measures sampled training examples, rather than a held-out evaluation.
Advanced: the learning rate
The learning rate sets how big each downhill step is. Too low and the loss crawls; too high and the updates overshoot and the loss explodes. Change it, hit Reset, and train again to feel the difference.
Applied on the next step; use Reset to compare cleanly from a fresh network.
One prediction, one loss
For the made-up training example The pet is a → cat, pretend the vocabulary contains just cat, dog, and fish. Raise cat's score and watch its probability rise and its loss fall. The scores here are chosen for teaching.
loss = −ln(0.4223) = 0.862 nats
Softmax exponentiates the logits and divides each by their sum, giving positive probabilities that sum to 1. Cross-entropy penalizes low probability on the observed token: 90% gives about 0.105 nats; 10% gives about 2.303. A batch's loss averages these penalties before the backward pass.
Optional math: softmax and perplexity
pᵢ = exp(zᵢ) / Σⱼ exp(zⱼ); perplexity = exp(mean loss)
Perplexity summarizes average predictive uncertainty; lower is better on the same evaluation data. Here exp(loss) = 2.37 for this one target. Scores from different tokenizers, vocabularies, or datasets are not directly comparable.
Where do the targets come from?
The text supplies its own labels. For a sequence such as the cat sat, each position is trained to predict the token at the next position. During training, the input contains the actual previous tokens, a setup called teacher forcing. A transformer's causal mask lets it compute predictions for many positions in one forward pass without using the future token as input to its prediction.
During generation, the next input includes tokens the model itself selected. Errors can therefore change the context for every later prediction. Training and generation share a prediction objective but use different sources for those previous tokens.
Epochs, batches, learning rate, in plain words
Each step above doesn't look at the whole text at once; it grabs a small random batch of examples, because averaging the error over a handful is far cheaper than over everything and works nearly as well. One full pass over all the training text is an epoch. Recipes can use one pass or reuse data; the random sampling here does not visit examples in a fixed epoch order. The learning rate is how big each update is. As the advanced control in the demo shows, too small can learn slowly, while too large can make the updates diverge.
Learning the examples versus generalizing
This demo plots loss on sampled training examples. A falling curve shows a better fit to those examples. To test generalization, keep other text out of training. The Scaling lesson measures loss on an unseen portion of its corpus.
- Training set
- Examples that produce gradients and change the weights.
- Validation set
- Held-out examples used to compare settings or decide when to stop.
- Test set
- A separate set reserved for the final assessment after those choices.
If training loss falls while validation loss rises, investigate overfitting: fitting the training examples too closely to transfer well. If test examples or near-duplicates enter training, data leakage can make the score look better than it should. Prediction loss is useful, but it still does not measure every task an assistant must perform.
The dataset is part of the model's education
Preparation includes selecting sources, cleaning text, removing duplicates, filtering unsuitable content, and balancing useful examples. Data quality and coverage influence what gets learned, including errors and social biases. This demo uses a small public-domain text sample cleaned to a limited character set; it cannot demonstrate broad language competence.
This is only pretraining: chatbots need more
What you trained here is a raw next-token predictor: it continues text, but it doesn't know it's supposed to be helpful, follow instructions, or answer a question rather than ramble on. That first stage is called pretraining. Turning a pretrained model into an assistant takes further stages: fine-tuning on curated examples of good responses, and learning from human and AI feedback about which answers people prefer. The model you meet in a chat window often uses some combination of these stages; the demo illustrates pretraining. The final Assistants lesson explains the stages and the chat system that uses the trained model.
The gap to a frontier model
The demo model has a few thousand parameters and learns from a few kilobytes of text. Larger language models use much larger datasets and computation budgets, often with transformer architectures, subword vocabularies, adaptive optimizers, and distributed training. They share the predict, measure, backprop, update loop, but the architecture and training recipe differ substantially from this three-character MLP.
Check your understanding
Sources & further reading
Lesson 06 / 09
Scaling
Your goal: Interpret a controlled size experiment without confusing parameters, compute, and useful capability.
In the last lesson you trained a real model with a few thousand parameters. Now keep that toy architecture and objective fixed while increasing its width. Does its prediction on unseen text improve? This small experiment introduces the broader study of how model size, data, and computation affect prediction loss.
Why scale works: scaling laws
Researchers have found approximate power-law relationships between prediction loss and model size, data, or compute within studied regimes. After accounting for a loss floor, a power law appears straight on logarithmic axes for both variables. Those empirical fits can help forecast loss within their assumptions; they do not guarantee every downstream skill. The Chinchilla study examined how to allocate a fixed training-compute budget between model parameters and data. It found that training a smaller model on more tokens could outperform a larger, less-trained model.
Run a small size experiment.
The demo below trains the same character-level network from the Training lesson at six sizes, from a few hundred parameters to tens of thousands, each on the same text for the same number of steps. It then plots the held-out loss of each. This is a genuine experiment running on your machine. Observe the trend and any exceptions; six points from one run do not establish a general scaling law.
Try it: press Run the experiment and compare held-out loss as the models get bigger.
Six models are trained one after another, smallest to largest, each on the same text for the same number of steps. The only thing that changes is embedding and hidden width; initializations and sampled batches also vary. A point drops onto the chart as each one finishes: do they land on a smooth downward curve?
Press Run to generate a sample.
Press Run to generate a sample.
Both samples are generated live by the two trained models after the run: same task, text, and training steps, with different widths and random initialization.
This is a real experiment, run in your browser
Every model here is the exact same character-level network from the Training lesson: reused code, not a copy, trained by real gradient descent on 137 KB of text (The complete text of Alice's Adventures in Wonderland by Lewis Carroll (1865, public domain, via Project Gutenberg).). For each size only the embedding and hidden widths grow. Every model sees the same text for the same 1,500 steps, and the loss you see is measured on a held-out 10% slice the model never trained on. Lower loss means better average prediction of those held-out characters. Equal steps do not mean equal compute; wider models do more work per step. Nothing is precomputed; press Run again and six brand-new networks train from scratch.
One variable at a time: read this carefully
This experiment deliberately changes only the embedding and hidden widths, holding the data, context length, batch size, learning rate, and number of steps fixed. Wider models generally require more arithmetic per step, so this is not an equal-compute comparison. Each size also has a different random initialization and sampled batches. Repeat runs to judge variability. The fixed three-character context and finite dataset can limit gains; larger models can also overfit. Undertraining and overfitting are different problems: a model can need more training without having memorized its data.
Selected milestones: size and efficiency
The timeline follows selected published model sizes from 2018 through February 2026. It is a history of different architectures and training recipes, rather than a capability leaderboard. Every entry links to its original report or model card. Some later models use a mixture of experts (MoE): they store many parameters but activate only a subset for each token.
Before the timeline: how we got to transformers
Language models existed long before the modern giants, but each step introduced new ways to represent and process language. The transformer became a foundation for many later models, including newer hybrids.
- ~1990sn-gram models: Predict the next word from raw counts of the last few, with no learning of meaning.
- 2013word2vec: Learned word vectors where meaning became geometry. The famous king − man + woman ≈ queen only works once the three input words are excluded from the answers. Otherwise the nearest word is usually king itself, nudged toward queen.
- 2014RNNs / LSTMs & seq2seq: Networks that read a sequence one step at a time; powered the first neural translation.
- 2017“Attention Is All You Need”: The transformer combines attention with position information and feed-forward layers, using no recurrence. Later architectures also explore hybrids.
Selected published sizes, 2018–2026. Hover or tap a point, or choose a model below.
Few-shot learning from the prompt alone, with no fine-tuning needed.
Read the original report or model card (opens in a new tab) ↗Every plotted count is from its creators' report or model card. This is a selected history, updated September 30, 2026, with entries through February 2026; it is not exhaustive. Models without a sourced public parameter count are omitted, and each point refers to the specified variant. The connecting line follows releases, not a fitted scaling law or ranking. Chinchilla was smaller than Gopher yet performed better after training on more data.
“Emergent” abilities, and the argument about them
Something odd shows up as models grow: certain skills, like multi-step arithmetic, following an unusual instruction, translating a rare language, seem largely absent in smaller models and then appear fairly suddenly past some size, rather than improving gradually. These are often called emergent abilities. Their interpretation is debated: some of the “sudden” jumps are partly an artifact of how the ability is measured : a strict all-or-nothing score can flip from zero to one abruptly even when the underlying skill was improving smoothly all along. Evaluate the actual task and metric, rather than assuming a particular parameter threshold guarantees an ability. Both the emergence study and the critique are linked in the sources below.
Scale isn't free
Larger training runs require more arithmetic, memory, and energy. Data quality, architecture, and optimization can change what that budget achieves. Deployment adds another tradeoff: a smaller model trained for longer may cost less to run for many users. MoE can reduce active computation relative to total size, but storing weights and routing between experts still have costs. Parameter count alone describes neither quality nor operating cost.
Check your understanding
Sources & further reading
Lesson 07 / 09
Generation
Your goal: Separate next-token probability, token selection, and factual correctness.
Every lesson so far has been building to this. Text is split into tokens, each token becomes an embedding, attention lets those vectors mix in context, and training tuned every weight along the way. And it all ends at one thing: a probability for every possible next token. Generation is what happens when you act on that number, over and over.
From a vector to a vocabulary distribution
After the transformer blocks and final normalization, the last token's representation is mapped to one raw score, or logit, per vocabulary entry. This output projection is sometimes called unembedding. Softmax turns the logits into probabilities that sum to 1; the worked loss example in Training uses the same conversion.
This demo uses GPT-2's own embeddings, transformer blocks, and 50,257-entry r50k_base vocabulary together. Its IDs differ from those in the Tokens lesson, and its vectors differ from the GloVe explorer.
Inspect predictions across layers
Token representations pass through repeated transformer blocks. The logit lens applies the final output readout to intermediate representations, letting us inspect how those representations project into vocabulary scores. Compare the readings across layers; they need not improve smoothly or monotonically.
What am I looking at?
A layer is one processing step inside the model. GPT-2 small has 12 of them stacked in a row. Each layer reads the running representation of your text, mixes in a bit more context, and passes it up to the next. The model only commits to a next-token probability after the final layer, but there is a clever trick, called the logit lens, that lets us peek early: take the half-finished representation at any layer and run it through the same output step the model uses at the end. This produces a diagnostic prediction from that representation.
A guess at a middle layer is not the model's real answer. It is a probe reading, not a literal record of thoughts. The output readout was trained for the final representation, so applying it earlier can give poorly calibrated or unrelated predictions. A change in a bar does not establish exactly when the model learned or retrieved a fact.
What the bars measure: probability assigned by the readout to a token. The final layer gives GPT-2's actual next-token prediction at temperature 1; the earlier readings are diagnostic. Token probability describes a continuation under the model, not confidence that a statement is true.
Try this: compare readouts across the layers.
Load GPT-2, then read the stack from the bottom up. Each row is one of the model's 12 layers, with the final vocabulary readout applied at that depth. Look for changes in the leading token and its probability. Intermediate readouts are probes; they need not approach the final prediction smoothly.
Compare a bare question with a prompt containing a worked example. The example can change the model's continuation without changing any weights: this is in-context learning. GPT-2 can still produce an incorrect or incomplete answer in either case. Observe what it predicts rather than assuming the prompt succeeds.
One token at a time
How a sentence gets built
In standard autoregressive decoding, the model predicts a single next token, one is chosen, that token is appended to the input, and the model predicts again, now conditioned on that added token. This demo recomputes the full sequence each time. Other systems can reuse intermediate computations. A model can generate plans or intermediate steps in its context, but this demo exposes next-token predictions rather than a complete description of planning.
Below is the real thing: GPT-2, the original 124-million-parameter model, downloaded and run entirely in your browser. The bars are the actual softmax probabilities it computes for the next token, not a mock-up. Click a bar to commit that token and see the distribution for what follows.
Try it: build a sentence one token at a time.
Load the model, then click any bar to append that token to the text, and the chart instantly recomputes what GPT-2 thinks comes next. Or hit Generate 20 tokens and watch the loop run itself.
Temperature & sampling, in plain terms
Once you have a probability for every token, you still have to pick one. Greedy decoding always takes the single highest bar: fully deterministic, and quick to fall into repetitive loops. Sampling instead rolls a weighted die, so a token with 40% probability is picked about 40% of the time, and even long-shot tokens occasionally come up.
Temperature is the knob that reshapes those odds before the die is rolled. Turn it down and the probabilities sharpen toward the top token, concentrating mass on the leading candidates. Turn it up and they flatten out, handing rare tokens a real chance: more creative, but more likely to select unlikely continuations. Sampling is one reason repeated prompts can produce different output. Chat applications can also retrieve information or call tools before generating. Temperature alone does not guarantee creativity, correctness, or safety. With fixed logits, positive temperature does not change which token greedy decoding selects.
Some decoding configurations trim the distribution before rolling. Top-k keeps only the k most likely tokens; top-p (nucleus) keeps the smallest set of tokens whose probabilities add up to some threshold, say 0.9, and throws the rest away. A repetition penalty can also adjust scores of previously used tokens. The surviving probabilities are renormalized before sampling. These are optional strategies; their order and implementation vary. This demo uses greedy selection or temperature sampling from the full vocabulary, without top-k, top-p, or a repetition penalty.
Why “predict the next token” can sound so capable
It's fair to wonder how something this simple produces essays, code, and arguments. Part of the answer is scale: the model you just ran is tiny and often clumsy, but the same next-token objective, trained on far more text with vastly more parameters, absorbs an enormous amount of the structure of language and the world. To predict the next token well across billions of examples, a model has to pick up grammar, facts, styles, and patterns of reasoning, not because anyone programmed them in, but because they help lower the prediction error.
The other part is post-training. A raw predictor will happily continue your text in any direction; the helpful, instruction-following assistant you talk to is that predictor after extra rounds of fine-tuning and human feedback that steer it toward being useful and on-topic. It's worth being clear-eyed, though: because the core act is predicting plausible text rather than checking truth, a model can state false things with complete fluency. The widely-used term is hallucination. Sounding confident and being correct are simply not the same operation here.
Context, caching, and stopping
The prompt and selected tokens must fit within a context limit. This browser demo uses a short limit for responsiveness. Generation stops at an end-of-text token, a length limit, or your Stop action; chat systems may use other end-of-turn markers.
Many decoder implementations cache previously computed keys and values (the KV cache) so each new token can attend to them without recomputing earlier positions. Longer contexts still use memory and attention computation. This cache stores activations for the current sequence, rather than changing the learned weights.
Check your understanding
Sources & further reading
- GPT-2: Language Models are Unsupervised Multitask Learners (opens in a new tab)
- The Curious Case of Neural Text Degeneration (nucleus sampling) (opens in a new tab)
- Eliciting Latent Predictions from Transformers with the Tuned Lens (opens in a new tab)
- How the key-value cache works (opens in a new tab)
Lesson 08 / 09
Assistants
Your goal: Explain what post-training changes and how retrieval, tools, and chat context support an assistant.
GPT-2 completes a piece of text. An assistant is a model trained for conversation, wrapped in a system that supplies instructions, history, and sometimes outside evidence or tools. Understanding that surrounding system explains much of the difference between this course's generation demo and a chat application.
How training changes the behavior
- 1. Pretraining learns broad patterns. A causal model learns from text by predicting next tokens. This creates a base model that can continue many kinds of text, with no guarantee that it will follow a user's request.
- 2. Supervised fine-tuning demonstrates responses. Curated prompt-and-response examples show how an assistant should answer. The model trains to assign higher probability to these responses. Training changes its weights; writing an example into a prompt alone does not.
- 3. Preference learning compares responses. People or other systems can judge which of two answers is better. RLHF can train a reward model from those comparisons and use reinforcement learning to update the assistant. DPO uses preference pairs directly to optimize the model. Recipes vary, and some also reward verifiable outcomes, such as passing a coding test.
These stages steer behavior according to the examples and feedback supplied. A preference for a fluent or pleasing answer does not automatically make that answer correct. Post-training can improve usefulness while leaving errors and bias to measure.
What enters a chat model
A chat template turns structured messages into the token sequence the model expects. Role markers distinguish instructions, user messages, assistant replies, and tool results. For illustration:
- System / application instruction
- Answer from the provided library document and cite the relevant line.
- User message
- When does the library close?
- Retrieved context
- Library hours: 9am–6pm, Monday to Friday.
- Assistant response begins
- The library closes at …
The exact formatting and instruction roles vary by model. The application chooses which messages and passages fit in the context window. Long conversations may be shortened or summarized. A product can save memories and retrieve them later, but the model's context is limited and those memories are supplied by the application.
Retrieval adds evidence; tools perform operations
In retrieval-augmented generation (RAG), a system searches a collection, selects relevant passages, and inserts them into the model's context. A dedicated embedding model can rank passages by similarity, often combined with keyword search or reranking. These document embeddings serve a different task from the token input vectors in the Embeddings lesson.
A tool flow lets the model request an operation, such as a calculation or database lookup. The application executes the request and returns its result; the model then continues with that result in context. For example: question → retrieve the hours document → supply the passage → generate an answer with a citation.
Retrieval and tool calls usually happen with fixed model weights. Fine-tuning changes weights. In-context learning changes how an existing model responds to the current examples. Retrieval can miss the right document, and tools can fail, so outside information still needs checking.
Reasoning uses a budget too
Some systems spend extra inference compute generating intermediate steps, testing candidates, or using tools before responding. That can help on difficult tasks, with a cost in time and tokens. A visible explanation is another generated output; it does not provide a complete record of the model's internal computation.
Evaluate the answer and the system
A hallucination is an unsupported or incorrect generated claim. Neither fluent wording nor high next-token probability establishes truth. Evaluate the behavior you need on examples that were held out from training and development:
- Correctness: use known answers, executable tests, or qualified review. Check whether citations actually support the claims.
- Retrieval quality: did the system find the relevant passage, and did the answer use it accurately?
- Coverage and bias: compare performance across languages, groups, and unfamiliar cases; an average can hide substantial failures.
- Reliability and cost: repeat important cases when sampling varies, record failure rates, and measure time and token use.
Human and model judges can disagree or favor particular styles. Document the rubric and inspect failures. Also distinguish the model from the application's data handling: this site's demos run locally after downloading model files, while a hosted assistant may send prompts and documents to its service. Data quality, permissions, and privacy are choices throughout the system.
Check your understanding
Sources & further reading
- Training language models to follow instructions with human feedback (opens in a new tab)
- Direct Preference Optimization (opens in a new tab)
- Chat templates and role markers (opens in a new tab)
- Retrieval-Augmented Generation for Knowledge-Intensive NLP Tasks (opens in a new tab)
- Toolformer: Language Models Can Teach Themselves to Use Tools (opens in a new tab)
- Judging LLM-as-a-Judge with MT-Bench and Chatbot Arena (opens in a new tab)
Final exercise: trace one answer
Use the library-hours example above. Explain how the question and retrieved passage become an answer. Include the tokenizer, positions and attention, output probabilities, token selection, whether the weights change, and how you would check the result.
Reveal a worked answer
- The application retrieves the hours document and formats instructions, the question, and the passage using the model's chat template. Its tokenizer encodes that context into vocabulary IDs.
- The model looks up matching input embeddings and incorporates position information. Its decoder blocks use causal attention to mix earlier information, plus feed-forward transformations, normalization, and residual connections.
- The final representation at the current last position is projected into vocabulary logits. Softmax and the decoding settings produce a next-token distribution; greedy decoding or sampling selects a token.
- The selected token is appended and generation continues until a stop condition. A system may reuse cached keys and values. The passage affects the activations, while the trained weights stay fixed during this inference.
- Check that the answer says 6pm on Monday to Friday, cites the supplied hours, and does not invent weekend hours. An answer of 6pm without the weekday qualification may be incomplete, even if every individual token had high probability.
Lesson 09 / 09
Run one yourself
Your goal: Read a model filename like Qwen2.5-14B-Instruct-Q4_K_M.gguf and estimate whether that model fits in your GPU memory.
Many models publish their weights, so you can download one and run it on your own computer with an app such as Ollama or LM Studio. The catch is choosing a file. Names like 14B and Q4_K_M tell you how big and how compressed a model is, and that decides whether it fits on your graphics card.
14B: how many parameters
14B means about 14 billion parameters. Parameters are the numbers the model learned during training (see Backpropagation), and together they hold everything it knows. More parameters usually means a more capable model, but also a bigger file and more memory to run it. Rough rules of thumb:
- 3 to 4B: fast, fine for simple tasks like short summaries or rewording.
- 7 to 9B: good all-rounders.
- 12 to 14B: noticeably more capable. With 4-bit compression, about the largest that fits on a 12 GB card.
- 70B and up: much more capable, but needs far more memory than a single consumer graphics card has.
Size is not everything. Newer models are often better than older ones of the same size, because training data and methods keep improving. Compare recent benchmarks or reviews for the tasks you care about.
Q4_K_M: how it was compressed
The original weights are usually stored as 16-bit numbers. Quantization rounds them to fewer bits so the file shrinks, at some cost in quality.
- Q4 means most weights are stored in about 4 bits. Each small block of weights also stores a scale, and some parts are kept at higher precision, so a Q4_K_M file averages about 4.9 bits per weight. That makes it roughly a third the size of the 16-bit original, not a quarter.
- K means a K-quant, llama.cpp's newer method. It splits weights into blocks of a few dozen, gives each block its own scale, and stores those scales compactly too. At about the same file size it keeps quality better than older types without the K, such as Q4_0.
- M means Medium. A model is made of many weight matrices (llama.cpp calls them tensors), and some matter more than others. In an M file, some of those sensitive tensors get a higher-precision type, such as 6 bits instead of 4. S (Small) uses the base type almost everywhere; L (Large), where offered, upgrades more. Bigger means a slightly larger file and slightly better quality. M is usually the best balance.
Try this: hover over or tap each part of the filename below, then switch examples. Which name leaves out the parameter count, and where would you find it?
Filename: Qwen2.5-14B-Instruct-Q4_K_M.gguf. Select a part to explain it.
Parameter count: 14B
About 14 billion parameters (14.7 billion, to be exact).
How many parameters (learned numbers) the model has, in billions. More usually means more capable, and always means more memory.
Will it fit?
For the model to run quickly, everything should fit in your graphics card's memory (VRAM). That means more than the file:
- The weights: parameters × bits per weight ÷ 8 gives bytes. 14 billion × 4.9 bits ÷ 8 is about 8.6 GB.
- The context: the KV cache stores keys and values for every token in the conversation, so it grows as the context gets longer.
- The runtime: working buffers for the calculation, plus whatever your screen and other apps already use.
If a model is too big, apps can keep part of it in regular RAM and run that part on the CPU. It still works, but much more slowly. On Macs with Apple silicon, the GPU shares the computer's main memory instead.
Try this: pick your GPU memory, start at 14B with Q4_K_M, then try 32B. Can any quant make 32B fit on your card, and what would it cost in quality? Then raise the context to 32K and watch the faded part grow.
Model size
Your GPU memory
Quantization
Context length (tokens)
Fits
about 9.87 GB needed, 12 GB available
Should fit in GPU memory with some room to spare.
Solid: model file. Faded: context and overhead. White line: your GPU memory.
- Model file (estimate)
- 8.57 GB
- Context memory at 4K
- 0.81 GB
- Runtime allowance
- 0.50 GB
File: 14 billion × 4.89 bits ÷ 8 bits per byte = 8.57 GB
Q4_K_M quality: A small loss. The usual balance of size and quality.
All figures are estimates. Real files differ a little by model, and apps differ in how much extra memory they use.
How the estimate is computed
- Bits per weight are llama.cpp's measured averages for Llama 3.1 8B: F16 16.0, Q8_0 8.50, Q6_K 6.56, Q5_K_M 5.70, Q4_K_M 4.89, Q3_K_M 4.00, Q2_K 3.16. Even Q2_K averages over 3 bits, because important tensors stay at higher precision. Sizes are in GB of a billion bytes; some apps show GiB, which reads about 7% smaller.
- Context memory is 2 (a key and a value) × layers × key/value heads × head size × 2 bytes × tokens. Each size preset uses the shape of a typical model: Llama 3.2 3B (28 layers), Llama 3.1 8B (32), Qwen2.5 14B (48), Qwen2.5 32B (64) and Llama 3.1 70B (80), all with 8 key/value heads of size 128 and a 16-bit cache. Other models can differ a lot, and some apps can compress the cache.
- Runtime allowance is a flat 0.5 GB guess for working buffers. The verdict says “fits” up to 90% of your GPU memory, “tight” up to 100%.
- Quality notes follow llama.cpp's perplexity tests on Llama 3 8B. Perplexity measures how surprised a model is by test text; lower is better. Compared with 16-bit, Q8_0 adds almost nothing, Q6_K about 0.02, Q5_K_M about 0.06, Q4_K_M about 0.18, Q3_K_M about 0.66 and Q2_K about 3.5. For comparison, the older Q4_0 adds about 0.47 at a slightly smaller size than Q4_K_M.
Check your understanding
Sources & further reading
Course complete
Revisit the pieces of the pipeline
The demos illustrate different parts of the same set of ideas. Training sets the weights; inference uses those weights with the current context. A chat application supplies the conversation and any retrieval or tools, and a local app does the same on your own computer.
- 1TokensHow raw text is chopped into the units a model actually reads.
- 2EmbeddingsExplore word neighborhoods and compare the vectors behind meaning.
- 3AttentionHow attention and transformer blocks build representations in context.
- 4BackpropagationHow the chain rule computes gradients and an optimizer updates weights.
- 5TrainingWatching loss curves fall as a model learns over time.
- 6ScalingHow size, data, and compute affect prediction loss and cost.
- 7GenerationSampling the next token from a probability distribution.
- 8AssistantsHow post-training, chat context, retrieval, and tools turn a predictor into an assistant.
- 9Run one yourselfWhat names like 14B and Q4_K_M mean, and whether an open model fits on your computer.