Index

Free, with Google

Keep going, free.

A free account opens lessons 2 and 3 of every track, 33 more lessons, and keeps your progress and review cards on every device.

Start free with Google

A chalk plan

Open everything on chalk.

Every lesson, board, video, lab and paper, and the full review decks. Yearly is $39/year.

See plansStart free instead

Module 06 · AI · GPT, built from scratch · about 13 min to read

Play it as a journeyTutor comes with a plan

Build the tokenizer: BPE by hand

Tokens and Embeddings
Tokens and Embeddings · Open board

The Tokens and Embeddings board shows text cut into pieces before a model sees it. This lesson builds the cutting machine yourself.

A developer in India sends the two words “नमस्ते दुनिया” to a language model API. It is 13 characters. The bill counts 13 tokens. The English “Hello world” is 11 characters and the bill counts 2 tokens. Same meaning, six and a half times the pieces.

Nobody chose that on purpose. It comes from a small program, the tokenizer, and from the text it was built on. By the end you will be able to write that program in about 40 lines of Python. You will train it on text, use it to cut new text, and explain why numbers, Hindi and one strange word called SolidGoldMagikarp all behave the way they do.

The history (optional)

In 1994 Philip Gage described byte pair encoding in The C User Journal, as a way to shrink files. In August 2015 Rico Sennrich, Barry Haddow and Alexandra Birch at Edinburgh used the same idea to cut words into pieces for machine translation (published at ACL 2016). On 5 February 2023 Jessica Rumbelow and Matthew Watkins posted “SolidGoldMagikarp (plus, prompt generation)” on LessWrong. It described words that made GPT models behave strangely. Their follow-up posts traced those tokens back to Reddit usernames. Nothing later in this lesson depends on these dates.

Before you start. In Tokens & Embeddings you saw that a model reads numbers, so text is cut into tokens and each token gets a row of numbers. You also saw that common text is cheap and rare text is expensive. In What the Model Sees (one string, every turn, from the top) you saw that cost and context size are counted in tokens. Here you build the part that decides where the cuts go. You need only basic Python.

Lesson 59 of 83 in AI

The rest of this lesson is part of a plan.

A plan opens every lesson, board, video, lab and paper on chalk. Yearly is $39/year.

See plansAlready a member? Sign in

Or start free: a free account opens lessons 2 and 3 of every track.

In this lesson

  • A token is a piece of text with a number
  • A byte is the smallest piece, and there are 256 of them
  • A merge joins the most common neighbouring pair
  • Training is just repeating the merge, and the list of merges is the tokenizer
  • Encoding replays the merges in the order they were learned
  • Quirk: numbers are cut into groups of three digits
  • Quirk: a token that was never learned
  • The whole tokenizer in 40 lines
  • More merges, fewer tokens
  • Real tokenizers, measured with tiktoken on 2026-11-28

Terms it explains: Tokens and Embeddings, token, vocabulary, bytes, merge, Training, Encoding, one token, glitch token, Common mistakes, analogy arithmetic, BPE, cosine similarity, dense vector, dimension d, embedding, embedding matrix, positional encoding, semantic search, static vs contextual embedding, token ID, tokenizer, vector database, vector space.

Lab

One sentence, one tokenizer, a vocabulary you can grow. How many tokens does it cost?

Drag How many pieces it knows left to right, then open the tab Another script, and an emoji, and watch How many tokens.

Open Tokens and embeddings

Related papers

Review AI