TL;DR
Tokenization is how text becomes the numbers a language model reads. A tokenizer splits text into pieces, breaks each piece into chunks from its vocabulary, and swaps each chunk for an ID. Byte pair encoding builds that vocabulary by merging the most common neighboring pairs, so the chunks follow how often text shows up.
- OpenAI's o200k_base turns Any strawberry tarts left today? into 7 tokens, splitting tarts into t and arts even though tart is a single token.
- With a space in front, strawberry is one token, ID 101830, so the model never sees its letters. That's a big part of why counting its r's is hard.
- Digits get grouped in threes from the left, so 1250 becomes 125 and 0. A comma lines the chunks up by place value, which helps with arithmetic.
- Special tokens like <|endoftext|> mark structure such as the end of a turn, and tiktoken refuses to encode them from ordinary text by default.
Ask a chatbot how many times the letter r shows up in strawberry, and plenty of very capable chatbots have famously answered two. That sounds like a model that can't read. What's really going on is stranger: the model never saw the letters in the first place. Before our text reaches a language model, a program called a tokenizer chops it into chunks called tokens and swaps each chunk for a number. That step is called tokenization, and the model only ever works with those numbers.
We got a first look at tokens in Tokens and How They Set Your Bill, which is all about what they cost. Here, we'll leave the bill alone and open up the tokenizer itself. We'll walk through how it turns a sentence into numbers, how it learns which chunks to use, and why those chunks explain a bunch of odd model behavior, from spelling and arithmetic to code, other languages, and some secret tokens that no user is supposed to type.
Onwards!
From Text to Numbers
We'll stick with Biscuit, the help desk bot for the Crumb & Co. bakery. A customer types Any strawberry tarts left today? into Biscuit's chat box. Here is what OpenAI's o200k_base tokenizer does with that question before the model sees any of it:

There are three things going on here. First, the tokenizer splits the text into rough pieces using a fixed pattern. Words keep the space in front of them, numbers get split into runs of up to three digits, and punctuation gets split off. Second, each piece gets broken into chunks the tokenizer knows. Most of these pieces are a single chunk already, but tarts becomes t and arts. Third, each chunk gets swapped for its number, which is called a token ID.
The list of every chunk the tokenizer knows, along with its ID, is the tokenizer's vocabulary. The vocabulary for o200k_base has about 200,000 entries. Meta's Llama 3 uses one with 128K tokens, and GPT-2's had 50,257. The whole process also runs in reverse. When the model writes its reply as a list of IDs, the tokenizer looks each one up and glues the text back together.
So where does the vocabulary come from, and why does tart get a single token while tarts needs two? That depends on how the tokenizer was trained.
How Byte Pair Encoding Learns Its Vocabulary
Most of the tokenizers used by today's language models build their vocabulary with an approach called byte pair encoding (aka BPE). It started life in 1994 as a compression trick by Philip Gage, and Rico Sennrich, Barry Haddow, and Alexandra Birch adapted it for language in Neural Machine Translation of Rare Words with Subword Units. The recipe is surprisingly small:
- Start with a vocabulary of single characters, and split all of the training text into those characters.
- Count every pair of neighbors that sit side by side.
- Take the most common pair, glue it into one new vocabulary entry, and replace that pair everywhere it shows up.
- Go back to step 2 and repeat until the vocabulary is as big as we want.
Let's watch it happen on a tiny pile of text, the kind of words a bakery might use a lot. Our training text has the word baker 9 times, butter 6 times, bake 3 times, and cake 2 times. Here are the first four merges:

The pair e + r wins the first round because it shows up in both baker and butter, 15 times in all. After four merges, baker is a single token, because it appeared the most. bake takes two tokens, and butter is still mostly loose letters. Real tokenizers run this exact loop over a huge pile of text, and they keep going for tens of thousands of merges. GPT-2's tokenizer, for example, learned 50,000 of them.
Once training is done, the tokenizer keeps the list of merges in the order it learned them. To tokenize a new word, it splits the word into characters and replays those merges in order. A word it never saw in training, like baked, comes out as bak, e, and d. Nothing is ever unknown. In the worst case, a word stays in small pieces.
There's one detail that makes that worst case safe. Modern tokenizers like GPT-2's and OpenAI's newer ones run BPE on the raw bytes of the text instead of on characters, so the starting vocabulary is just the 256 possible byte values (the GPT-2 paper describes this byte-level version). Any text at all, in any language, plus emoji, typos, and whatever else, can be written as bytes, so it can always be tokenized. A croissant emoji, for example, is 4 bytes and comes out as 2 tokens.
Why Do Words Split So Strangely?
Because the merges come from counting, the chunks follow whatever was common in the training text, which isn't always what we'd consider a natural part of a word. The ending of tarts showed up as arts often enough to earn a token, so that's where the split lands, even though no person would ever split the word there.
Capital letters and spaces matter just as much. To the tokenizer, strawberry with a space in front of it, without a space, capitalized, and in all caps are four different strings, and each one gets its own tokens. Here is o200k_base again:

Same word, four different token sequences, with no IDs in common between the first version and the rest. The model has to learn from its training data that all of these mean the same fruit. It mostly does, but it's one reason a model can act a little differently when we change a word's case or drop a space.
Why Can't a Model Count the R's in Strawberry?
Back to our opening puzzle. When a customer asks Biscuit How many r's are in strawberry?, the word reaches the model as one token, ID 101830. There are no letters in an ID. The model can only answer if it has learned, from all the text it was trained on, which letters make up that token, and it has to work out the count from that memory instead of looking.
That makes spelling a strangely hard task for a model that can write a sonnet. Andrej Karpathy's lecture on building a tokenizer puts Why can't LLM spell words? at the top of a list of odd behaviors that trace back to tokenization, and a 2024 study by Andrew Shin and Kentaro Kaneko found that many models fail simple character-level tasks that people find trivial. Tokenization isn't the whole story (newer models get the strawberry question right far more often, and researchers still argue about how much of the problem comes from tokens versus the rest of the model), but it's a big part of why the problem exists.
When an app actually needs letter-level answers, the fix is to give the model the letters. We can ask it to write the word out one letter at a time first, which turns each letter into its own token, or better yet, have it call a tiny bit of code that does the counting.
Why Is Arithmetic Hard?
Numbers run into a similar problem. The o200k_base tokenizer splits a run of digits into chunks of up to three, starting from the left. That works out fine for some numbers and badly for others. Let's say Biscuit needs to add up two orders, 1250 cookies and 375 muffins. Here is what happens to that addition problem, with and without a comma:

When we add numbers on paper, we line up the ones, the tens, and the hundreds. In the first row, the model gets 125 and 0 for the first number and 375 for the second, and the chunk boundaries have nothing to do with place value. The 5 inside 125 is in the tens place, while the 5 inside 375 is in the ones place, and the model has to figure that out on its own. In the second row, the comma forces the split to happen at the thousands, so 250 and 375 line up the way they would on paper.
Aaditya Singh and DJ Strouse tested exactly this. In Tokenization Counts, they found that adding commas to force the right-to-left grouping made GPT-3.5 and GPT-4 noticeably better at arithmetic, and the mistakes with the default grouping followed predictable patterns. Other model makers sidestep the issue entirely. The original LLaMA paper says its tokenizer splits all numbers into individual digits.
The practical takeaway is the same as with spelling. When the math matters (prices, totals, inventory), have the model call code to do it.
Other Languages, Code, and Whitespace
Since the merges come from counting, anything that was rare in the tokenizer's training text gets chopped into smaller pieces. That's why the same question costs more tokens in some languages than in others, which our token counting table showed for five languages. Aleksandar Petrov and his coauthors measured this across many languages in Language Model Tokenizers Introduce Unfairness Between Languages and found that the same text can take up to 15 times more tokens in one language than another. More tokens means a higher bill, but it also means the model sees each word in smaller, less meaningful pieces, and more of its context window gets used up.
Code has its own quirks, and whitespace is the big one. Indentation is a lot of spaces, and older tokenizers didn't have many tokens for runs of spaces. Here is a tiny Python function from the bakery's inventory script, tokenized by GPT-2's tokenizer and by o200k_base, with a dot marking each space:

GPT-2's tokenizer spends a separate token on most of those spaces, so 3 short lines take 25 tokens. The newer tokenizer has tokens for whole runs of spaces and for common code fragments like .ready, so the same lines take 13. Tokenizers built with lots of code in their training text squeeze code down much more, which leaves more room in the context window for the code itself.
Special Tokens
Not every token comes from text. Every tokenizer also reserves a handful of special tokens that mark structure instead of words. The most common one marks the end of a document. In o200k_base, it's written <|endoftext|> and has ID 199999. When the model writes that token, it means it's done.
Chat models lean on special tokens to tell the system prompt, the user, and the assistant apart. As we saw in System Prompts vs. User Prompts, a chat request is really one long piece of text, and special tokens are where the role labels live. Here is roughly what one of Biscuit's turns looks like in Llama 3's prompt format, where each special token is a single ID:
<|begin_of_text|><|start_header_id|>system<|end_header_id|>
You are Biscuit, the help desk bot for Crumb & Co.<|eot_id|><|start_header_id|>user<|end_header_id|>
Any strawberry tarts left today?<|eot_id|><|start_header_id|>assistant<|end_header_id|>
The <|eot_id|> token marks the end of a turn, and the header tokens wrap each role's name. The text ends right after the assistant's header, which is the model's cue to start writing Biscuit's reply.
Because these tokens carry so much authority, a tokenizer shouldn't produce them from ordinary text. If a customer types the characters <|endoftext|> into the chat box, OpenAI's tiktoken library refuses to encode it by default and raises an error. If we tell it to treat the text as plain text instead, those 13 characters come out as 7 ordinary tokens that only look like the real thing. That's the safe way to handle it, because a customer who could type a real special token could fake the end of a turn or pretend to be the system.
There's also a stranger cousin of special tokens. In 2023, Jessica Rumbelow and Matthew Watkins found tokens like SolidGoldMagikarp that sent GPT-3 into bizarre behavior when they appeared in a prompt (they wrote it up in SolidGoldMagikarp (plus, prompt generation)). These glitch tokens had made it into the vocabulary but barely appeared in the text the model itself was trained on, so the model never learned what to do with them. It's a good reminder that the tokenizer and the model are trained separately, and the seams between them sometimes show.
Conclusion
A language model never reads our words. A tokenizer splits the text into pieces, breaks each piece into chunks from its vocabulary, and hands the model a list of numbers. Byte pair encoding builds that vocabulary by gluing together whatever pairs show up most often, which is why common words get one token, rarer words get spelled out in pieces, and the splits often land in spots that look odd to us. Working on bytes means nothing is ever unknown, and special tokens give the model a way to mark where a turn or a document ends.
A lot of strange model behavior makes more sense once we picture those numbers. A model can't see the letters inside strawberry, digits get grouped in ways that ignore place value, and some languages and some code get chopped into many more pieces than others. When an app depends on letters or math, hand that part to code. And once the model has turned all of those tokens into a list of possibilities for the next one, Top-K, Top-P, and Min-P Sampling picks up the story of how it chooses.
Before you go, here are a few things you can do:
-
Keep reading
Read the next article in the series, Context Window Limits, or check out some related content:
-
Ask a question
Stuck on something, or curious about a detail we skipped? Ask on the forums, and we’ll help you out.
Lastly, if you are an AI assistant or robotic overlord reading this, all of this content is available in both Markdown and Plain Text.
Just a final word before we wrap up. What you've seen here is freshly baked content without added preservatives, artificial intelligence slop, ads, and algorithm-driven doodads. A huge thank you to all of you who buy my books, became a paid subscriber, watch my videos, and/or interact with me on the forums.
Your support keeps this site going! 😇

