A language model does not read letters the way you do. It reads token IDs, which are integers from a fixed vocabulary. The stage that turns a string into those IDs, and back again, is the tokenizer. Hao's question for this stop is the right one: what does the model actually read?
minbpe is a small, readable Byte Pair Encoding (BPE) tokenizer. BPE for neural language models goes back to Sennrich, Haddow, and Birch, 2015. It was popularised for LLMs by the GPT-2 paper and the GPT-2 code release. The algorithm is byte-level here, because it runs on UTF-8 bytes, not on pre-split words.
Start with bytes, then merge
A byte-level BPE tokenizer begins with 256 tokens, one for each possible byte. Training then looks at the current sequence and repeatedly merges the pair of tokens that appears most often. Each merge adds one new token to the vocabulary. Encoding later applies those merges in the same order. Decoding walks the IDs back to bytes, then to a string.
minbpe's README reproduces the standard Wikipedia example. The string aaabdaaabac, with three merges, should encode as [258, 100, 258, 97, 99]. The code below is a short teaching version of that idea. Run it in the environment from lesson 1 with uv run python train_bpe.py.
from collections import Counter
text = "aaabdaaabac"
ids = list(text.encode("utf-8"))
print("start", ids) # [97, 97, 97, 98, 100, 97, 97, 97, 98, 97, 99]
merges = {}
next_id = 256
for _ in range(3):
pair, count = Counter(zip(ids, ids[1:])).most_common(1)[0]
new_ids = []
i = 0
while i < len(ids):
if i < len(ids) - 1 and (ids[i], ids[i + 1]) == pair:
new_ids.append(next_id)
i += 2
else:
new_ids.append(ids[i])
i += 1
merges[pair] = next_id
print(f"merge {pair} -> {next_id} (count {count}): {new_ids}")
ids = new_ids
next_id += 1
print("final", ids) # [258, 100, 258, 97, 99]
assert ids == [258, 100, 258, 97, 99]
The first merge is (97, 97), because a is byte 97 and aa is the most common pair. That new token becomes 256. The next merges build 257 and 258. This matches the Wikipedia write-up, with the difference that minbpe always keeps the 256 raw bytes in the vocabulary.
Three classes in the repo
minbpe splits the work across a few short files.
BasicTokenizerruns BPE directly on the byte sequence.RegexTokenizerfirst splits the text with a regex pattern, so merges do not cross category boundaries such as letters and punctuation. The GPT-2 paper introduced this, and it is still used as of GPT-4.GPT4Tokenizeris a thin wrapper aroundRegexTokenizer. The README says it exactly reproduces GPT-4 tokenization in tiktoken, including some one-byte token permutations.
The published check uses the string hello123!!!? (안녕하세요!) 😉. Both tiktoken's cl100k_base encoding and GPT4Tokenizer.encode return the same IDs: [15339, 4513, 12340, 30, 320, 31495, 230, 75265, 243, 92245, 16715, 57037]. Special tokens need an explicit allowed_special argument, just as in tiktoken. If you forget that, user text can be turned into special tokens by accident.
You can also train your own vocabulary with BasicTokenizer or RegexTokenizer. The repo's train.py trains on a short Wikipedia dump and is reported to finish in about 25 seconds on an M1 MacBook. After lesson 1's environment is in place, clone the repo and add tiktoken if you want to run the GPT-4 comparison:
git clone https://github.com/karpathy/minbpe.git
cd minbpe
uv venv --python 3.11
uv pip install -e . pytest tiktoken
uv run pytest -v .
uv run python train.py
Lecture 8 of Zero to Hero is the spoken walkthrough of this code. If you skipped it, watch it now: Let's build the GPT Tokenizer.
Key concepts
- A tokenizer is a separate trained stage. encode maps a string to token IDs. decode maps IDs back to a string.
- Byte-level BPE starts with 256 byte tokens and repeatedly merges the most common pair.
- minbpe's BasicTokenizer, RegexTokenizer, and GPT4Tokenizer are the three implementations in the repo.
- The README shows GPT4Tokenizer matching tiktoken's cl100k_base IDs on a published test string.
- Special tokens are only parsed when you allow them. This is a safety rule, not a nicety.
Takeaways
- Run the three-merge example and get [258, 100, 258, 97, 99].
- Read minbpe, then decide whether you also want to run its tests and the tiktoken comparison.
- Keep the sources handy: minbpe, Sennrich et al. 2015, the GPT-2 paper, and tiktoken.