← All Labs
IDEA #38 · HOW AN LLM LEARNS TO TALK

Predict the next token, very well, at scale

A language model looks like it understands you. Underneath, it is doing one thing over and over: guessing the next piece of text and getting slightly less wrong each time. Each tab below runs the real machinery in your browser — no faked plots. A live byte-pair tokenizer is trained on a built-in corpus and splits whatever you type; a tiny skip-gram model pushes words that share company toward each other in a 2-D map; scaled dot-product attention draws the arcs each token actually leans on; and a genuine softmax classifier trained by cross-entropy watches its own loss fall as its next-word guess sharpens. Press TRAIN ONE EPOCH up top and the embedding map and the next-token model both take a step. Watch the loss drop — that dropping number is the entire trick.

GLOBAL EPOCH
0
NEXT-TOKEN LOSS · bits/token
TAB 1 · INPUT
Tokenization
Type text; a real BPE tokenizer splits it into subword pieces. Fewer merges → letters; more merges → words.
TAB 2 · MEANING
Embeddings
A skip-gram model trains live; words that keep the same company drift together on a 2-D map.
TAB 3 · CONTEXT
Attention
Real softmax attention. Each token's arcs show which other tokens it reads. Switch heads, add a causal mask.
TAB 4 · LEARNING
Next-token loss
A softmax classifier trained by cross-entropy. The loss curve falls; the predicted distribution sharpens.
01

Drive it — watch your words shatter into subword tokens

A model never sees letters or whole words — it sees tokens. This tokenizer was trained by byte-pair encoding on the built-in corpus: it starts from single characters and repeatedly glues the most frequent adjacent pair into one token. Type anything and it re-tokenizes live. Drag the merges slider from 0 (pure characters) up to a few hundred (whole common words) and watch the same sentence collapse from many tiny tokens into a few fat ones.
DRIVE IT
characters · tokens · chars / token
Each coloured chip is one token the model would receive as a single integer ID. A leading · marks a space that belongs to the token (real tokenizers attach spaces to the word that follows).
MERGES · VOCAB SIZE110
VOCAB
unique tokens
LAST MERGE
most recent glue
LONGEST
biggest token
bytes → pairs → merges → tokens
The vocabulary is built once from the corpus; the slider just replays the first N merges the algorithm chose. Rare or made-up words fall back to smaller pieces — that is why a model can spell a word it has never seen.
02

Walkthrough

Glue the most common pair, repeat.
WALKTHROUGH
1
Set merges to 0. Every character is its own token — l·a·n·g·u·a·g·e. This is all a fresh model knows: an alphabet.
2
Drag merges up slowly. Watch common pairs fuse first — th, in, er, then whole endings like ing and tion. The LAST MERGE card names the newest glue.
3
Push it high. Frequent words in the corpus — the, token, model — become single tokens. The chars / token ratio climbs from 1 toward 4–5.
4
Now type a rare or invented word (try "antidisestablishment" or "zxqwv"). It shatters into small pieces — the tokenizer has no single token for it, so it spells it out.
5
Type numbers and symbols. Notice digits often stay separate — a reason models are famously shaky at arithmetic: "127" may arrive as 127.
THE INSIGHT
"A model doesn't read letters or words — it reads a fixed vocabulary of subword chunks. Byte-pair encoding earns that vocabulary from raw text alone: keep gluing the commonest neighbours, and useful pieces emerge for free — with the alphabet still underneath as a safety net for anything new."
03

Explanation

Why subwords, not words or letters.
EXPLANATION

A model must map text to a finite list of integer IDs. Two obvious choices both fail. Whole words give a vocabulary that is enormous, never complete, and helpless against typos or new words. Single characters give a tiny vocabulary but make every sequence painfully long, forcing the model to relearn spelling from scratch every time.

Byte-pair encoding (BPE) threads the needle. Start with characters. Count every adjacent pair in the corpus, glue the single most frequent pair into a new token, and add it to the vocabulary. Repeat a few thousand times. Common letter sequences, endings, and whole frequent words become single tokens, while rare strings stay decomposable.

The payoff is coverage with compression. Any string — including one never seen in training — can always be encoded, because the alphabet remains in the vocabulary as a fallback. Yet common text is short, because "the" is one token, not three.

This choice ripples through everything. Token boundaries decide how the model "sees" numbers (often digit by digit, hurting arithmetic), how many tokens a language costs (English is cheap; some scripts are expensive), and even how prompts are billed. The tokenizer is frozen before training — the model spends its whole life speaking in exactly these pieces.

04

Research note

The merge rule, exactly.
RESEARCH NOTE
At each step BPE picks the pair of adjacent tokens with the highest corpus count and merges it:
Repeating this for k steps yields a vocabulary of size |Σ| + k where Σ is the base alphabet. Encoding a new string greedily applies the learned merges in the order they were discovered.
Further reading: Sennrich, Haddow & Birch, "Neural Machine Translation of Rare Words with Subword Units" (2016) — the paper that brought BPE to language models. Modern variants: WordPiece (BERT), Unigram/SentencePiece (T5, Llama). See also OpenAI's tiktoken.
01

Drive it — watch meaning organise itself

Every word starts as a random dot. A real skip-gram model (word2vec, in 2-D so you can see it) then trains: for each word it pulls its true neighbours in the corpus closer and pushes random other words away. Press TRAIN up top — or auto-train — and watch animals, royalty, numbers and verbs migrate into their own clusters. No labels are given; the geometry is learned from company alone. Click any word to light up its nearest neighbours.
DRIVE IT
epoch0 · skip-gram loss · click a word
Distance ≈ dissimilarity. Two words end up near each other when they tend to appear in the same contexts — "you shall know a word by the company it keeps."
train a little, then query
LEARNING RATE0.10
WORDS
in vocabulary
PAIRS
skip-gram
DIM
2
for viewing
Real embeddings live in hundreds of dimensions; we train directly in 2-D so the map is the model, with nothing hidden by a projection.
02

Walkthrough

Company becomes coordinates.
WALKTHROUGH
1
Start from the reset cloud — pure noise. No word means anything yet; positions are random.
2
Press run. Within a few epochs, words that share neighbours in the corpus (cat / dog / animal) start to clump, while unrelated words drift apart.
3
Type a word into nearest neighbours. Early on the list is random; after training it becomes sensible — king sits near queen, throne, royal.
4
Watch the loss fall in the ledger. That single number is the model becoming better at predicting which words keep company.
5
Notice you never told it "these are animals." Categories emerge purely from co-occurrence statistics — the core surprise of distributional semantics.
THE INSIGHT
"Meaning, to a model, is a position. Words that appear in the same company are trained to sit in the same neighbourhood — so directions and distances in that space come to encode analogy and similarity, learned from nothing but which words tend to show up together."
03

Explanation

The distributional hypothesis, made numeric.
EXPLANATION

A token ID is meaningless — 4127 is no closer to 4128 than to 99. So the first thing a model does is look each ID up in an embedding table: a learned vector of a few hundred numbers. Training shapes that table so the geometry carries meaning.

The classic recipe is skip-gram with negative sampling (word2vec). For each word in the corpus, take its real context words as positive examples and a handful of random words as negatives. Nudge the word's vector to have a high dot product with its true neighbours and a low one with the random impostors.

Do this across millions of sentences and a startling structure appears. Similar words cluster; and famous directions emerge — the vector from king to queen roughly matches man to woman. Meaning has become arithmetic.

In a full transformer the embedding table is just the input layer, learned jointly with everything above it, and refined at every layer by attention. But the principle set here never leaves: a word is a point, and its neighbours define what it means. This map is the model's dictionary.

04

Research note

The skip-gram objective.
RESEARCH NOTE
For a centre word c and context word o, negative-sampling maximises agreement with true context and disagreement with k random negatives w:
σ is the logistic function and vectors are updated by gradient ascent. This applet trains that exact objective, with the embedding dimension set to 2 so the vectors themselves are what you see.
Further reading: Mikolov et al., "Distributed Representations of Words and Phrases" (2013, word2vec); Pennington et al., GloVe (2014). The distributional hypothesis is Firth's (1957): "You shall know a word by the company it keeps."
01

Drive it — see which words each token reads

This is scaled dot-product attention, computed live. Each token builds a query and every token a key; their dot products, softmaxed, say how much this token should read from each other token. Pick a query token below and the arcs show exactly where its attention flows — thicker = stronger. Switch heads to see different relationships, raise temperature to blur the focus, and toggle the causal mask that stops a token from peeking at the future (how GPT-style models are trained).
DRIVE IT
query · attends most to · head1
The grid below the sentence is the full attention matrix: row = query token, column = key token, brightness = weight. Each row sums to 1.
HEAD1 / 3
TEMPERATURE1.0
TOKENS
in sentence
HEADS
3
parallel
d_k
8
key dim
Q, K, V projections here are fixed random matrices (seeded), so the arcs are a genuine attention pattern — not learned semantics, but the exact arithmetic a trained head performs.
02

Walkthrough

Query, key, softmax, mix.
WALKTHROUGH
1
Pick a query token. Its arcs fan out to every token, thick where attention is strong. The weights on those arcs sum to 1 — attention is a weighted average.
2
Switch heads. Each head has its own Q/K projection, so it looks for a different relationship — one head might track the previous word, another a matching word elsewhere.
3
Raise temperature. The softmax flattens and attention spreads out; lower it and the token locks onto a single target. This is the same softmax temperature that controls sampling.
4
Turn on the causal mask. Now each token can only attend to itself and tokens to its left — the future is hidden. Watch the upper triangle of the matrix go dark.
5
Change the sentence. The matrix rebuilds instantly: attention is recomputed from scratch for every input — there is no fixed wiring between positions.
THE INSIGHT
"Attention lets every token look at every other token and decide, on the fly, how much to borrow from each. That's how a model resolves 'it', links a verb to its subject, and carries meaning across a paragraph — a learned, content-based lookup instead of fixed wiring."
03

Explanation

The one equation that runs modern AI.
EXPLANATION

Embeddings give each token a meaning in isolation, but words live in context: "bank" by a river is not "bank" with your money. Attention is how a token gathers context.

From each token's vector the model computes three projections: a query (what am I looking for?), a key (what do I offer?), and a value (what will I hand over?). A token's query is compared, by dot product, against every key. Big dot product means "relevant." A softmax turns those scores into weights that sum to one, and the token's new representation is that weighted mix of everyone's values.

Two refinements make it work at scale. Scores are divided by √d_k so they don't explode as vectors grow — the "scaled" in scaled dot-product. And the whole thing runs in several heads in parallel, each free to specialise, their outputs concatenated.

Stack this dozens of layers deep and information routes freely across a sequence, every token continuously re-reading every other. This single operation — introduced in "Attention Is All You Need" (2017) — is the beating heart of every large language model. The causal mask is the only twist for generation: hide the future so the model must truly predict it.

04

Research note

Scaled dot-product attention.
RESEARCH NOTE
The entire operation, for queries Q, keys K and values V, is one line:
The softmax is applied per row. A causal mask sets the upper-triangular scores to −∞ before the softmax so position i cannot attend to any j > i. Multi-head attention runs h copies with independent projections and concatenates the results.
Further reading: Vaswani et al., "Attention Is All You Need" (2017) — the transformer. See also Bahdanau et al. (2015) for the original additive attention, and the many "illustrated transformer" walkthroughs online.
01

Drive it — watch the loss fall and the guess sharpen

Here is the whole objective, running for real. A softmax classifier reads a context word and predicts a probability for every word that could come next, trained by cross-entropy on the corpus. Press TRAIN up top and two things happen together: the loss curve (left) slides down toward the corpus's true entropy, and the predicted distribution (right) for your chosen context word goes from flat guessing to a confident spike on the words that actually follow it.
DRIVE IT
epoch0 · loss · afterthe
Left: cross-entropy loss per epoch, in bits/token (lower = better prediction). Right: the model's full next-word probability bar chart for the context word you pick.
train, then read the top guesses
LEARNING RATE0.30
SAMPLING TEMPERATURE1.0
"Generate" feeds the model its own output word by word, sampling at the chosen temperature — exactly how a chatbot writes, one token at a time.
02

Walkthrough

One number to rule them all.
WALKTHROUGH
1
Reset. The distribution is flat — the untrained model thinks every word is equally likely to come next, so the loss is high (≈ log₂ of the vocabulary size).
2
Press run. The loss curve dives, steeply at first then levelling — the model is learning the corpus's real word-to-word statistics.
3
Type the as the context. After training, the bars spike on the nouns that actually follow "the" in the corpus. Try a, cat, is — each gets its own profile.
4
Press generate a sentence. The model writes by sampling its own prediction, feeding each word back as the next context. Higher temperature = wilder text; lower = safe and repetitive.
5
This toy sees only one previous word. A real LLM sees thousands and runs this same cross-entropy loss over trillions of tokens — but the loop is exactly this.
THE INSIGHT
"Everything an LLM can do — answer, summarise, translate, code — falls out of relentlessly minimising one number: the surprise of the next token. Make that prediction good enough, over enough of the internet, and fluency, facts and reasoning come along for the ride."
03

Explanation

Cross-entropy is just measured surprise.
EXPLANATION

Tokenization, embeddings and attention all feed one final step: a softmax over the whole vocabulary that outputs a probability for every possible next token. Training compares that predicted distribution to reality — where the "true" answer is simply the token that actually came next — and pushes the probability of the right token up.

The scorecard is cross-entropy loss: the negative log-probability the model assigned to the correct token. Guess it with probability 1 and the loss is 0. Guess it with probability near 0 and the loss is huge. Averaged over a corpus, this is exactly the model's surprise per token, measured in bits.

Gradient descent then does what it always does: nudge every weight — embeddings, attention projections, the output layer — a hair in the direction that lowers that surprise. Repeat over trillions of tokens.

Nothing in the objective mentions grammar, facts, or reasoning. Yet to predict the next token well across the whole internet, a model is forced to internalise all of it: syntax to finish sentences, world knowledge to complete facts, even arithmetic to continue a sum. Capability is a side effect of relentless next-token prediction — the single loop this tab runs in miniature.

04

Research note

The cross-entropy objective.
RESEARCH NOTE
Given context, the model outputs logits z, softmaxed to a distribution p; the loss is the negative log-probability of the true next token t, summed over the corpus:
Reported here in bits (log base 2); minimising it is equivalent to minimising the model's perplexity = 2loss. This applet trains a single-context softmax model by exact gradient descent on that objective.
Further reading: Shannon, "A Mathematical Theory of Communication" (1948) for entropy; Bengio et al., "A Neural Probabilistic Language Model" (2003); Radford et al., GPT (2018) and Kaplan et al., "Scaling Laws" (2020) — the same loss, more data and parameters.
Idea #38 from the 45-ideas visual-maths teaching set · every model computed live in the browser, no faked plots · DRIVE IT → WALKTHROUGH → AHA → EXPLANATION → RESEARCH NOTE