A machine that reads pieces
Why can a fluent model stumble when counting letters in “strawberry”? Its input arrives as numbered text pieces, not a row of separately labeled letters. We'll build the program that makes those pieces and follow a word through it.
The Goldilocks problem: letters, words, or something between
Computers already encode text as numbers. The design question is which units the language model should process. A token may be a word, part of a word, punctuation, or a byte.
| Unit | Advantage | Cost |
|---|---|---|
| Character | Reusable small pieces. | Long sequences; the model must combine many pieces. All Unicode characters require a large inventory. |
| Whole word | Shorter sequences of familiar units. | An enormous menu; new names and spellings need a fallback. |
| Subword | Common strings become one piece; rare ones share parts. | Boundaries need not match syllables. |
A tokenizer applies the chosen segmentation rules. About three-quarters of an English word per token is a rough guide, not a rule for every language or passage.
The merging game
Byte-pair encoding, or BPE, learns reusable pieces from a corpus. The procedure runs like this:
- Start with small base pieces.
- Count adjacent pairs, weighted by their occurrences.
- Merge the most frequent pair and recount.
- Stop at the chosen vocabulary size; retain the merge order.
Our invented corpus has hug ten times, pug five times, hugs five times. Start with h, u, g, p, s.
Worked example: Round 1
Count each adjacent pair across all three words, weighting by how often the word appears:
h+ushows up in hug (10) and hugs (5) = 15u+gshows up in all three: hug (10), pug (5), hugs (5) = 20p+ushows up in pug only = 5g+sshows up in hugs only = 5
The winner is u+g with 20. Glue it: a new piece ug joins
the menu, and every u-g in the corpus fuses at once. The words
are now h·ug (×10), p·ug (×5), and h·ug·s
(×5).
Worked example: Round 2
Recount the pairs, now on the new pieces. This is the last round I'll count in full; after this we summarize.
h+ug: h·ug (10) and h·ug·s (5) = 15p+ug: p·ug (5) = 5ug+s: h·ug·s (5) = 5
Winner: h+ug with 15. Glue it, and the piece hug exists. The
corpus is now hug (×10), p·ug (×5), and hug·s
(×5). (Round 3 would be a 5-vs-5 tie between p+ug and hug+s;
real tokenizers break ties by a fixed rule and keep going for tens of thousands of
rounds. We stop here.)
| Stage | pugs | hugs |
|---|---|---|
| Start | p · u · g · s | h · u · g · s |
| u + g → ug | p · ug · s | h · ug · s |
| h + ug → hug | p · ug · s | hug · s |
For this toy tokenizer, the ordered merge list determines the split. Production tokenizers also specify preprocessing, special tokens, and tie-breaking. BPE is common; other tokenization methods exist.
Byte-level BPE starts from bytes. That provides a fallback for emoji, accented letters, and unfamiliar scripts without needing a whole-word entry for each.
Catalog numbers: IDs and the vocabulary
The vocabulary contains base pieces and learned pieces. Its size is V. A token ID identifies one entry.
Our V=7 menu assigns h=1, u=2, g=3, p=4, s=5, ug=6, hug=7. Therefore hugs becomes [7,5], and pugs becomes [4,6,5]. To decode, read the same table backward.
A larger vocabulary might have 50,257 entries or hundreds of thousands. It changes the menu's size, not what an ID means. The learned vector associated with an ID is a separate object.
See it happen
Watch the two merges, then pause before pugs becomes IDs. The first merged piece, ug, has ID 6.
u+g wins
20-to-15, then hug forms. At the end the never-seen word pugs is
chopped by replaying the merges into [4, 6, 5], the numbers from this
chapter.Quirks you can feel in the chatbot
Tokenization can make character tasks less direct, but it does not make them impossible. Models can learn spelling relationships, and different tokenizers split “strawberry” differently. The figure's [straw][berry] split and IDs are teaching examples.
| Case | What to expect |
|---|---|
| Leading spaces | “ the” and “the” may use different tokens and IDs. |
| Numbers | 12345 might split as [123][45], with boundaries unrelated to place value. |
| Languages | The same message can need 20 tokens in one language and 40 in another, depending on the learned menu. |
| Capacity and usage | Context and generation are counted in tokens. At the rough English ratio, 200,000 tokens correspond to about 150,000 words. |
These are tendencies to check with the particular tokenizer, not universal splits. Shared pieces let the system handle novel strings, while unequal segmentation makes some texts more expensive to process.
Why the LLM cares
$$ p(\text{next} \mid \text{context}) $$In words: the probability of each possible next token, given the tokens so far.
For the text path we are studying, token IDs enter and leave the model. The output distribution has one share per vocabulary entry: V shares. An ID selects a piece; it does not encode similarity to another piece.
We need one more conversion before the vector tools can help: replace each ID with a learned arrow.
What you now know
- Tokens are reusable text units.
- BPE repeatedly merges frequent adjacent pieces.
- Our merges produce hug and ug, allowing unseen pugs to be segmented.
- A token ID identifies a vocabulary entry without measuring meaning.
- Spaces, spelling, numbers, and language affect token counts.
Where we're headed
We have catalog numbers. Next we'll give each token a vector so geometry can express relationships between pieces of language.