Byte-level BPE · from scratch · no dependencies

o200k from scratch:
try the tokenizer

Verdict: I rebuilt GPT-4o's tokenizer from zero and verified it byte-identical to tiktoken. The real thing is running right below — type anything and watch it split into tokens. Then browse 50 examples of where the splits get weird.

live encoder 50 examples 5 diagrams byte-identical to tiktoken

October 4, 2026 · ai · tokenizers · ← all experiments

Interactive · the real tokenizerType anything.

loading 200,019-token vocabulary…

50 real examples — tap any card to load it into the box.

How it worksThe pipeline

A language model never sees letters — only integers. GPT-4o's tokenizer converts text in five stages: special tokens split first, a regex cuts the rest into pieces, each piece becomes raw UTF-8 bytes, and byte-pair encoding repeatedly merges the lowest-ranked adjacent pair. Merges never cross a regex boundary.

raw text
1 · split special tokens
<|endoftext|> #199999
2 · pre-tokenizer regex
7 branches, greedy
3 · UTF-8 bytes
256 single-byte tokens
4 · BPE merges
lowest rank first
5 · token ids

How it worksByte-pair encoding, watched

"token" starts as 5 raw bytes. Each step fuses the pair with the lowest merge rank — ranks learned from real text, so common letter pairs merge first. Four merges, one token (#15263). The algorithm never considers meaning, only frequency.

t o k e n
5 bytes
merge e + n → en
rank 262
merge o + k → ok
rank 525
merge ok + en → oken
rank 2488
merge t + oken → token
rank 10346
1 token: #15263

The best partThe possessive-quantifier detective story

OpenAI publishes the pre-tokenizer regex with possessive quantifiers (?+, *+, ++) — matchers that never give characters back. But tiktoken's core is Rust, and Rust's regex crate has no possessive quantifiers: it silently parses a++ as nested (a+)+, i.e. plain greedy. That changes real output:

published regex
possessive quantifiers
a++ ?+ *+
Python regex module
true possessive semantics
tiktoken Rust core
regex crate has no possessive
a++ parsed as (a+)+ = greedy
a b becomes: a / double-space / b
a b becomes: a / space / space-b
tiktoken output

Type a b (a, two spaces, b) into the playground and see for yourself: tiktoken gives a | space | space-b. A truly possessive regex would give a | double-space | b. The published pattern and the running code disagree — and the code wins. Footnote: tiktoken's own pure-Python encode_ordinary path uses the regex module, which does implement possessive — so tiktoken disagrees with itself depending on which door you enter through.

How it worksThe 7 regex branches

Before any merging, the regex decides the maximum span a token may cover:

next position in text
word +
contraction?
don't
ALL-CAPS word
NASA
number, max 3 digits
123
punctuation run
!!!
newlines
newline newline
trailing whitespace
end of text
other whitespace
one piece: merges never cross it

How it worksVocabulary anatomy

200,019 ids
256
single bytes
199,998
learned merges
2
special tokens
19
unassigned

The 256 byte tokens are the escape hatch: every byte is in the vocabulary, so every Unicode string has exactly one encoding — even text the tokenizer never saw in training.

Go deeper