AI ENGINEERING: DEEP DIVE · MODULE 03 — TOKENIZATION & TRAINING MECHANICS · 07 A hands-on series · math + code

Module 3.1

A vocabulary nobody designed

Byte-Pair Encoding doesn't know English. It just counts which pair of symbols shows up next to each other most often, and glues that pair together — over and over, on real text, until common substrings become single tokens.

TOY CORPUS: "export" ×5, "exports" ×2, "exported" ×3
Step 0 of 7 — no merges yet
TOP PAIR COUNTS RIGHT NOW

The math

One rule, applied greedily

Pair frequency
count(a,b) = Σ freq(w) over words w where a,b adjacent
count(e, x) across all 3 words
= 10
Merge rule
merge = argmax₍a,b₎ count(a,b)
replace every occurrence of that pair with one new symbol
merge → "ex"

No linguistics anywhere in this rule. It's pure frequency — which is exactly why tokenizers trained on code split differently from ones trained on prose.

The code

BPE training, in full

bpe_train.py
from collections import Counter

def get_pair_counts(words):   # words: {tuple_of_symbols: freq}
    counts = Counter()
    for symbols, freq in words.items():
        for a, b in zip(symbols, symbols[1:]):
            counts[(a, b)] += freq
    return counts

def merge_pair(pair, words):
    new_words = {}
    for symbols, freq in words.items():
        merged, i = [], 0
        while i < len(symbols):
            if symbols[i:i+2] == pair:
                merged.append(pair[0] + pair[1]); i += 2
            else:
                merged.append(symbols[i]); i += 1
        new_words[tuple(merged)] = freq
    return new_words

for step in range(num_merges):
    pair_counts = get_pair_counts(words)
    best = max(pair_counts, key=pair_counts.get)
    words = merge_pair(best, words)
    vocab.append(best[0] + best[1])   # the new token

This is the whole algorithm — real tokenizer training just runs it on gigabytes of text instead of 10 words, for tens of thousands of merges instead of 7.

Go deeper (optional)

The full, real-world version of this

Another course-length build (2h 13m), not a quick clip — Andrej Karpathy builds a complete, GPT-4-style tokenizer from scratch, including the regex pre-splitting rules and byte-level fallback this toy example skips entirely.

Andrej Karpathy — "Let's build the GPT Tokenizer." Supplementary and optional.

The Nimbus thread

Why "Nimbus" itself might not be one token

If "Nimbus" appears often enough in a model's training data, BPE merges it into one or two tokens, same as "export" did here. If it's rare — a small company's product name almost certainly is, in a tokenizer trained mostly on the general web — it stays fragmented into odd pieces like Nim + bus. That fragmentation is invisible in a chat UI, but it's exactly why brand names, made-up words, and rare technical terms cost more tokens (and sometimes get subtly misunderstood) than common English words.

So what

A tokenizer's vocabulary is discovered from data, not designed — whatever repeats most in training becomes a token; everything else gets stuck spelling itself out in pieces.