↑↓ move Enter open ⌘K find from any page 256 of 256 tabs
Loading the recording…

What o200k is

o200k_base is the tokenizer of GPT-4o, published in OpenAI's tiktoken library. It works like GPT-2's: the text's UTF-8 bytes are cut into pieces by a regular expression, merged by byte-pair encoding (BPE) inside each piece, and decoded back exactly, with no unknown token. Three things differ, and the page shows each. The vocabulary holds 199,998 ranked byte strings, 4.0× GPT-2's 50,257. The pre-tokenizer has seven alternatives instead of six. And there is no separate list of merges: a byte string's rank is its token id.

Two more ids, 199,999 for <|endoftext|> and 200,018 for <|endofprompt|>, are special tokens that never come from text. The toolbar follows one text through the stages and puts GPT-2's count beside o200k's: the English sentence is 37 characters, 37 UTF-8 bytes, 6 pieces and 7 tokens, as many as GPT-2 needs.

The pre-tokenizer

The Pre-tokenizer column lists the seven alternatives, tried left to right, the first match winning:

  • word: at most one leading space or symbol, any upper-case letters, then lower-case ones, with a contraction such as 's kept on it
  • upper-case word: the same with the upper-case letters required and the lower-case ones optional, for a word that ends in upper case, such as GPT or HELLO'S
  • digits: \p{N}{1,3}, at most three digits and never a space
  • symbols: punctuation with at most one leading space, and any line breaks or slashes after it
  • line breaks, spaces before a word and spaces

The o200k's pieces and GPT-2's pieces of the same text rows show what changed. A word is cut where lower case turns back to upper, so CamelCase becomes Camel and Case. A contraction stays on its word in any case instead of being a piece of its own. Digits come in groups of at most three, read from the left, and never take the space before them: in Spaces and a number GPT-2 cuts " 12345" as one piece, while o200k cuts a lone space, 123 and 45. Letters without case, such as the Japanese of 東京は晴れ, count as both upper and lower case, so a run of them stays one piece.

Splitting digits from the left is a choice with consequences. Singh and Strouse found that GPT-3.5 and GPT-4, whose tokenizer groups digits the same way, did arithmetic clearly better when commas forced the groups to be counted from the right instead.

Ranks instead of a merge list

The file o200k_base.tiktoken has one line per token: its bytes in base64 and its rank, from 0 to 199,997. There is no merges.txt. The rank of an adjacent pair of parts is the rank of their bytes joined, if the vocabulary holds them, and the pair with the lowest rank merges next:

Here is the -th part of the piece, a byte string, and joins two of them. Only that one occurrence merges, the leftmost on a tie, and the loop repeats until no two neighbouring parts join into a vocabulary entry. This is tiktoken's own educational encoder. Because BPE training adds each token after the ones it is built from, the order of the ranks is the order of the merges, so a lower id was merged earlier in training. The first 256 ranks are the single bytes in GPT-2's order, which is why a full stop is id 13 and a space id 220 in both vocabularies.

The Byte-level BPE column shows one state of the loop with the parts as bytes; a part such as ⟨E6⟩ is a byte that is only part of a character. The BPE loop strip plays the film of the selected piece. "unbelievably" ends as un, bel and ievably after 9 merges, one token fewer than GPT-2's; 東京は晴れ ends as 東京, は, 晴 and れ after 11 merges, where GPT-2 needs 9 tokens.

Ids and decoding

In the Tokens → ids column the id is the rank itself. Clicking a token opens its vocabulary entry and the entries around it; GPT-4o's weights are not public, so there is no embedding row to draw. Decoding joins the tokens' bytes and reads them as UTF-8, and the Decode panel shows every example coming back exactly. A token may still end inside a character: " 🤖" becomes one token holding the space and the emoji's first three bytes, and one holding its last byte.

The recording ran the drawer's plain-Python code on the o200k_base.tiktoken file that tiktoken downloads and checks by its SHA-256. Its ids equal tiktoken's own on all 325 check strings.

Fewer tokens for the same text

A tokenizer's compression is the number of bytes per token, . Fewer tokens for the same text means the model reads more per step and fits more into its context, at the price of larger embedding and output layers, one row per vocabulary entry. The o200k vs GPT-2 column counts both tokenizers on the five examples and on all 325 check strings, 12,473 bytes: o200k needs 3,252 tokens and GPT-2 4,212, 3.84 against 2.96 bytes per token.

The gain is uneven. On the 247 strings in pure ASCII, 10,968 bytes, o200k needs 2,842 tokens against GPT-2's 3,441, 17 % fewer. On the 78 strings in other scripts, 1,505 bytes, it needs 410 against 771, 47 % fewer. GPT-2's vocabulary has whole tokens for few characters outside ASCII, so such text falls back to byte pieces, as 東京は晴れ does; o200k's has whole tokens for many more. Petrov et al. measured how far apart languages can be: the same text can take up to 15 times as many tokens in one language as in another.

Where it came from

Byte-level BPE comes from GPT-2 (see the GPT-2 tab), and BPE itself from Sennrich et al.'s subword vocabularies for translation. tiktoken holds OpenAI's encodings in one format: r50k_base, GPT-2's vocabulary, then cl100k_base for GPT-3.5 and GPT-4, which already grouped digits in threes, then o200k_base for GPT-4o, added in May 2024. Dagan et al. showed with code models that the vocabulary size, the pre-tokenizer regex and the tokenizer's training data each change generation speed, usable context and accuracy.

BERT and T5 build their vocabularies another way, with WordPiece and a Unigram model. ByT5 reads raw bytes with no vocabulary at all, and BLT groups bytes into patches by how hard the next byte is to predict.

Papers

  1. Neural Machine Translation of Rare Words with Subword Units Rico Sennrich, Barry Haddow, Alexandra Birch · ACL 2016 Byte-pair encoding as a subword vocabulary: merge the most frequent adjacent pair, again and again, and replay the merges to encode.
  2. tiktoken OpenAI · 2022 The library this tab records: the o200k_base file and pattern, and the educational encoder the drawer follows.
  3. GPT-4o System Card OpenAI · arXiv 2024 The model o200k_base was made for; the card covers its training, evaluations and safety work, not the tokenizer itself.
  4. Language Models are Unsupervised Multitask Learners Alec Radford, Jeffrey Wu, Rewon Child, David Luan, Dario Amodei, Ilya Sutskever · 2019 GPT-2, which introduced byte-level BPE with a category-splitting regex; the tokenizer this tab compares with.
  5. Tokenization counts: the impact of tokenization on arithmetic in frontier LLMs Aaditya K. Singh, DJ Strouse · arXiv 2024 Three-digit groups read from the left against groups forced from the right: the latter do arithmetic clearly better in GPT-3.5 and GPT-4.
  6. Language Model Tokenizers Introduce Unfairness Between Languages Aleksandar Petrov, Emanuele La Malfa, Philip H. S. Torr, Adel Bibi · NeurIPS 2023 The same text can take up to 15 times as many tokens in one language as in another, even with multilingual tokenizers.
  7. Getting the most out of your tokenizer for pre-training and domain adaptation Gautier Dagan, Gabriel Synnaeve, Baptiste Rozière · ICML 2024 How vocabulary size, the pre-tokenizer regex and the tokenizer's training data change speed, context and accuracy of code models.

See also