10.3Tokenization
What the model actually sees. A language model never reads letters or words. Before it sees anything, your text is cut into tokens from a fixed vocabulary and each token is replaced by a number. Everything odd about how models handle spelling, arithmetic and other languages starts here.
- 1Type
Type or paste anything. The text is split by a real tokenizer running in your browser, the open source
gpt-tokenizerport of OpenAI's vocabularies. Each cell is one token; the number under it is the token ID, which is all the model receives. - 2Check
The same word, three ways. Written with a space before it, as it usually appears mid-sentence, it can be a single token: one number with no letters in it at all.
NoteAsked how many r's are in strawberry, the model is not looking at letters. It gets token IDs, and the spelling of each one has to have been learned separately from text that happened to spell it out. Counting letters is easy for you and indirect for it.
- 3Count
Long numbers are cut into chunks of digits. Turn the dial to make the number longer and compare the tokenizer's chunks with the way you group digits.
NoteThe tokenizer chunks digits from the left, while place value runs from the right, so the chunks rarely line up with thousands and millions. Add one digit and every chunk keeps its digits but changes what it is worth: the leading token “123” is 1,230,000 in 1234567 and 12,300,000 in 12345678. A model doing arithmetic has to work place value out from the IDs.
- 4Compare
To the model, a space and a capital letter are not small changes. Each version below is a different token, or several, with nothing in the IDs to say they are the same word. Try your own.
- 5Measure
The same sentence in five languages: Article 1 of the Universal Declaration of Human Rights, translated by people. Each count below was made by the real tokenizer when this page was built, and the figure after it is the multiple of the English count.
Tokens for Article 1 of the Universal Declaration of Human Rights, by language and tokenizer Language Characters o200k_base tokens cl100k_base tokens English 170 33 1.0× 33 1.0× Nepali 163 47 1.4× 169 5.1× Hindi 189 54 1.6× 182 5.5× Spanish 171 38 1.2× 44 1.3× Chinese (simplified) 43 35 1.1× 50 1.5× Show the five texts
- English
- All human beings are born free and equal in dignity and rights. They are endowed with reason and conscience and should act towards one another in a spirit of brotherhood.
- Nepali
- सबै व्यक्ति हरू जन्मजात स्वतन्त्र हुन ती सबैको समान अधिकार र महत्व छ। निजहरूमा विचार शक्ति र सद्धिचार भएकोले निजहरूले आपसमा भातृत्वको भावना बाट व्यवहार गर्नु पर्छ।
- Hindi
- सभी मनुष्यों को गौरव और अधिकारों के मामले में जन्मजात स्वतन्त्रता और समानता प्राप्त है । उन्हें बुद्धि और अन्तरात्मा की देन प्राप्त है और परस्पर उन्हें भाईचारे के भाव से बर्ताव करना चाहिए ।
- Spanish
- Todos los seres humanos nacen libres e iguales en dignidad y derechos y, dotados como están de razón y conciencia, deben comportarse fraternalmente los unos con los otros.
- Chinese (simplified)
- 人人生而自由,在尊严和权利上一律平等。他们赋有理性和良心,并应以兄弟关系的精神相对待。
NoteOn cl100k_base, the tokenizer behind GPT-4, Article 1 takes 33 tokens in English and 169 in Nepali: 5.1× as many for the same meaning. On o200k_base, its successor, the gap shrinks to 1.4× (47 against 33).
Tokens are what you pay for and what fills the context window, so a Nepali speaker on the older vocabulary paid several times more to say the same thing, and could fit several times less into one request. Petrov and colleagues found gaps of up to 15 times between languages across tokenizers in 2023. Newer vocabularies are fairer, not fair.
- 6Train
Byte-pair encoding, as Rico Sennrich, Barry Haddow and Alexandra Birch used it for translation in 2016. Each word starts as single letters with an end marker (
·). Every merge finds the pair of neighbours that occurs most often and joins it everywhere. Step through the merges and watch the vocabulary grow and the text shrink.low low low low low lower lower newest newest newest newest newest newest widest widest widest
- Vocabulary
- 11
- Tokens in the text
- 95
- Last merge
- none
- l
- o
- w
- ·
- l
- o
- w
- e
- r
- ·
- n
- e
- w
- e
- s
- t
- ·
- w
- i
- d
- e
- s
- t
- ·
NoteThis is the whole idea behind the vocabulary in step 1: start from single characters (bytes, in the real thing), and keep gluing together the pair that appears most often. Frequent words end up as one token; rare words, and languages that were rare in the training text, stay in small pieces. That is where step 5's gap comes from.
- 7Go
Tokens are the unit of cost and of context. Read on.
10.3.1Sources
- Rico Sennrich, Barry Haddow and Alexandra Birch, Neural Machine Translation of Rare Words with Subword Units, ACL 2016. Step 6 follows its Algorithm 1 and toy corpus.
- Aleksandar Petrov, Emanuele La Malfa, Philip H. S. Torr and Adel Bibi, Language Model Tokenizers Introduce Unfairness Between Languages, 2023.
- The Universal Declaration of Human Rights in Unicode, data/udhr (Unicode Consortium), which records OHCHR as the source of its translations. Article 1 in step 5 is copied from it unchanged.
- gpt-tokenizer 4.0.0 by Bazyli Brzoska (MIT licence), the JavaScript port of OpenAI's encodings that runs on this page.
- OpenAI, tiktoken, tiktoken/model.py: which models use o200k_base and cl100k_base.