Skip to content
← All work

Personal · learning tool · 2026Live

Token by Token

A field guide to how language models work, as eight stations you can run in your browser: tokenising, embedding, attention, sampling, training, alignment, reasoning and retrieval. The models are tiny but real: a tokenizer, a trigram model, a network trained live and a reward model fitted to your own clicks.

Who wrote whatClaude built the first version of the site with me in one go. I then worked on it the way an engineer works on code they have to maintain. I decided which logic had to come out of the page scripts into tested modules (the character model, the decoder and the feedback maths), had that work done in pairing sessions with Claude, and reviewed every change. I also added a test for the one thing that silently breaks this kind of site, script order, and recorded why it has no build step. Each of those seven commits is mine and co-authored with Claude.

The eight stations
  1. Tokenise
  2. Embed
  3. Attend
  4. Predict & sample
  5. Pretrain
  6. Align
  7. Reason
  8. Build

The loop it's built around

Every chat model runs the same loop: read the text so far, score every possible next token, pick one, append it, repeat. The page opens with that loop running on a tiny model, so you can press Next token and watch each stage before the stations take it apart.

Token by Token's opening screen: the prompt “the king and the” passing through tokenize, read context, logits, softmax and sample panels
The generation loop on the opening screen, one stage per panel.

The stations

  • Tokenize. Drag a slider to replay the byte-pair-encoding merges the page learned from its own training text, and watch “strawberry” split into chunks. You learn why models fumble letter-counting questions. Real: the BPE is trained in your tab.
  • Embed. Click words on a map and try king − man + woman. You learn that meaning becomes geometry. Real: the vector maths. Illustrative: the 10 named dimensions are hand-built so you can read them.
  • Attend. Inspect query·key scores, the causal mask and the softmax weights for three heads. You learn how each token decides what to look at. Real: the attention maths. Illustrative: the heads are hand-designed; training would discover them.
  • Predict & sample. Change temperature, top-k and top-p and roll the dice once or twenty times. You learn why the same prompt gives different answers. Real: a trigram model trained on the corpus, and the decoder that applies the settings.
  • Pretrain. Train a small network live, a 26×26 weight matrix that predicts the next character, and watch the loss fall and the text go from noise to word-like. Push the learning rate too high and it diverges. Real: gradient descent on cross-entropy.
  • Align. Compare one prompt at the base, fine-tuned, RLHF and reasoning stages, then act as the human labeller. You learn how a reward model picks up what you reward, including reward hacking: always prefer the longest answer and watch the “long” weight grow. Real: logistic regression fitted to your choices. Illustrative: the stage outputs are scripted.
  • Reason. On the bat-and-ball question, raise the number of reasoning paths and watch majority voting raise accuracy, then see GRPO's group-relative advantages. You learn what test-time compute buys, and when voting can't help. Real: Monte Carlo simulation and the GRPO arithmetic.
  • Build. Ask a small knowledge base a question, see which passages retrieval picks, see the full prompt a chat app actually sends, and step through a tool-use loop. You learn that a chatbot has no memory; it re-sends everything. Real: TF-IDF retrieval. Illustrative: the API messages are examples; examples/chat.py and examples/agent.py are the same ideas as runnable Python against a real model.
  • Plus a timeline from 1950, a research-frontier map with primary sources, and a reading list.
The Tokenize station with the merge slider part-way, showing words split into byte-pair chunks
Tokenize: replay the merges the page learned in your tab.
The Pretrain station after 600 steps: loss fallen from 3.26 to 2.03, a 26×26 prediction grid, and word-like generated text
Pretrain after 600 steps: loss from random guessing (3.26) down to 2.03, near the best this model can do.

How it's built

Plain scripts that share one window.LLM namespace, with no bundler and no ES modules, so the page opens straight from a file (browsers block module imports from file://). I reconsidered ES modules in an October architecture review and rejected them again; the reason is written down as an architecture decision record in the repo.

The cost of that choice is that script order in index.html becomes a contract between files. So a page-contract test checks that every element a script looks up exists, every id is unique, every station the app starts is defined, and every script tag points at a real file.

The model code is tested with Node's built-in runner, with nothing to install: an untrained model scores close to random guessing; a sensible learning rate approaches the best possible loss but never goes below it; a learning rate that's too high diverges; temperature 0 is greedy; top-k keeps exactly k tokens; preferring flattery is learned too; and voting can't help when the right answer isn't the most likely one. A glossary fixes the words used in the code: station, corpus, CharModel, decoder, feedback.

What's real and what isn't

The tokenizer, trigram model, training loop, reward model, voting and retrieval all compute in your tab. The attention heads and the alignment-stage answers are hand-made, so each idea stays visible. I'd rather say that on the page than let a tiny demo pretend to be a real model.

Next

Put the “real versus illustrative” note inside each station (today it's only in the README), and give the wide charts a proper mobile layout.

NextMYENGINEER→

07 · Contact

Write to me.

I'm open to software engineering and data roles, and to freelance work. If you have a process that still runs on copy and paste, I'd like to hear about it.