---
title: "What the model actually reads"
lesson_id: "03"
---

# What the model actually reads

- **Lesson ID:** 03
- **Goal:** Teach byte-level BPE, run the Wikipedia three-merge example, and explain how GPT4Tokenizer matches tiktoken.
- **Human lesson:** [03-what-the-model-reads.html](03-what-the-model-reads.html)

## Prerequisites

- Lesson 01 environment (`uv`, Python 3.11+). Lesson 02 is useful but not required if the micrograd engine can be rebuilt without looking.

## Inputs, outputs, and artifacts

- **Inputs:** The question of what a language model actually reads.
- **Outputs:** A short BPE script and a clear picture of minbpe's three tokenizer classes.
- **Artifacts:** `train_bpe.py` that encodes `aaabdaaabac` as `[258, 100, 258, 97, 99]` after three merges.

## Agent build steps

1. Define a tokenizer as a separate trained stage with `encode` and `decode`.
2. Point at [minbpe](https://github.com/karpathy/minbpe). Cite [Sennrich et al. 2015](https://arxiv.org/abs/1508.07909), the [GPT-2 paper](https://cdn.openai.com/better-language-models/language_models_are_unsupervised_multitask_learners.pdf), and [tiktoken](https://github.com/openai/tiktoken).
3. Teach byte-level BPE: start with 256 bytes, merge the most common pair, repeat.
4. Run the Wikipedia / minbpe README example and assert `[258, 100, 258, 97, 99]`.
5. Describe BasicTokenizer, RegexTokenizer, and GPT4Tokenizer.
6. Quote the README's identical tiktoken `cl100k_base` ID list for the published test string. Do not invent a new match unless you run the code.
7. Warn that special tokens need `allowed_special`.
8. Optional: clone minbpe, install with uv, run pytest, and run `train.py`.

## Constraints

Keep the language simple, friendly, and literal. No quizzes, no em dashes. Brand Edge FDE only. Do not paste the whole minbpe repo. The teaching BPE script should be short and original.

## 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](https://github.com/karpathy/minbpe), [Sennrich et al. 2015](https://arxiv.org/abs/1508.07909), the [GPT-2 paper](https://cdn.openai.com/better-language-models/language_models_are_unsupervised_multitask_learners.pdf), and [tiktoken](https://github.com/openai/tiktoken).

## Acceptance checks

- The three-merge example and the expected IDs are present.
- The tiktoken match is attributed to the minbpe README.
- Agent brief link is present.
- [ ] Proceed to [lesson 04 brief](04-a-full-training-loop.llms.md).
