TinyLanguageBuilder / The Tiny Textbook

Chapter 1: Tokens

A language model never sees letters or words. It sees a list of numbers. This chapter shows how text becomes those numbers, using the same tokenizer code that trains the real model.

1. Everything is bytes

A computer stores text as bytes: numbers from 0 to 255. The letter a is 97, a space is 32, and an emoji takes four bytes. The simplest tokenizer gives every byte its own token, so the model's vocabulary is just 256 entries and any text at all can be written down. There is no "unknown word".

The cost is length. A sentence of 40 letters becomes 40 tokens, and a model reads a fixed number of tokens at a time, so short tokens waste its attention on spelling. Try it: type below with the slider all the way to the left.

2. Tokenizer playground

Loading the engine and learning merges from the story sample…

Vocabulary size is 256 + merges. The real small model uses 512 (256 merges); the medium one uses 1024.

The text, cut into tokens (each coloured chip is one token)

Bytes of text
0
Tokens
0
Bytes per token
0
Vocabulary
256

The token numbers the model would actually see

3. Byte-pair encoding: learning to merge

To shorten the token list, we let the tokenizer learn from text. It looks for the pair of neighbouring tokens that appears most often, glues them into one new token, and repeats. Early merges are common letter pairs (h + e becomes he); later ones build up pieces and whole words such as ·the (the dot stands for a space). Each merge is one step of the slider above.

Merges are applied in the order they were learned, which is why shrinking the vocabulary is the same as keeping only the first merges. The widget below shows the most recent merge the slider has reached, and the whole recipe.

4. The merge recipe

Move the slider to learn the first merge.

Merge #Token idGlues togetherResult

Rows beyond the slider are dimmed: the tokenizer has not "learned" them yet in the playground.

5. Why not just use words?

Whole words need a huge vocabulary and fail on names, typos and new words. Single bytes never fail but make sequences long. Byte-pair encoding sits in between: common words become one token, rare words are spelled from pieces, and nothing is ever unknown. Drag the slider to 256 merges (the small model's setting) and compare bytes per token with the slider at 0: every extra byte a token covers lets the model's fixed 64-token window see more text.

Try it: paste a word that is not English, or an emoji, and watch it fall back to single bytes (shown as \xNN). Next chapter: turning these token numbers into vectors the model can learn from.