Follow text from UTF-8 bytes through byte-level BPE to token IDs, then connect token counts to cost, context budgets, and model behavior.
Follow text from UTF-8 bytes through byte-level BPE to token IDs, then connect token counts to cost, context budgets, and model behavior.
Tokenization converts text into token IDs before a language model processes it. A token can represent a word fragment, punctuation, or bytes. This article follows byte-level Byte Pair Encoding (BPE), one common approach, from UTF-8 bytes to the IDs used by a model.
Follow the process step by step, with concrete examples.
Every character on your keyboard (and beyond) can be represented as a sequence of bytes using UTF-8 encoding. A byte is a value from 0 to 255 – giving exactly 256 possible symbols to start with.
Take the word “token”. Here is what its UTF-8 byte values look like:
| Character | t | o | k | e | n |
|---|---|---|---|---|---|
| UTF-8 byte | 116 | 111 | 107 | 101 | 110 |
Each byte is a number from 0 to 255. ASCII letters use one UTF-8 byte; “é” uses two bytes, 195 and 169. A Unicode code point can use up to four UTF-8 bytes, while an emoji sequence may contain several code points. Byte count and token count are different quantities.
This byte-level representation is universal – it works for any language, any script, any emoji. But feeding raw bytes directly to a model is inefficient. Common English words like the, and, or is would all be shredded into individual bytes, forcing the model to reconstruct meaning from tiny fragments. Common sequences can be grouped instead.
Why not just use characters? A character-level approach keeps the vocabulary small, but sequences become very long. The model must process more positions to understand the same text – which is expensive and makes long-range patterns harder to learn.
The solution is to find a smarter middle ground – chunks of text that are larger than single bytes but smaller than full words. This is what Byte Pair Encoding (BPE) does.
Originally a data compression algorithm, BPE was adapted for NLP by Sennrich et al. in 2016 and later adopted by OpenAI for the GPT series. The algorithm works like this:
t + h is the most common pair, create a new symbol th.Suppose the corpus contains just two words repeated many times: low and lower. Start with individual bytes for each character, then follow the merges:
After three illustrative merges, the two words low and lower go from eight byte symbols to three tokens: low, low, and er. Real merge rules are learned from a training corpus and then reused when encoding new text.
After enough merges, the vocabulary is rich and varied. Some entries are full common words. Others are subwords, prefixes, or suffixes. Rare words get split into smaller pieces but they are always representable.
These standalone strings were checked against the official tiktoken cl100k_base merge data. Spaces and surrounding text can change a split. Token bytes do not always decode to a complete Unicode character on their own.
| Input word | Tokens | Token count |
|---|---|---|
| tokenization | token · ization | 2 |
| unbelievable | un · belie · vable | 3 |
| café | ca · fé | 2 |
| the | the | 1 (extremely common) |
| 🚀 | Bytes F0 9F · 9A · 80 | 3 |
Notice the pattern: common English subwords and short words become single tokens. Rare combinations or non-ASCII characters get split further. The tokenizer is, in effect, a compression algorithm that has learned the statistical structure of language.
At the heart of BPE is a deliberate tension between two forces pulling in opposite directions:
Different tokenizers make different vocabulary and sequence-length trade-offs. The table gives approximate sizes for familiar examples, not a quality ranking. A byte-level tokenizer can represent Unicode text even when it uses several tokens for one character.
| Tokenizer | Model | Approximate vocabulary | Encoding note |
|---|---|---|---|
| r50k_base | GPT-3 | 50k | Byte-level BPE |
| cl100k_base | GPT-3.5 / GPT-4 | 100k | Byte-level BPE |
| o200k_base | GPT-4o | 200k | Byte-level BPE |
| SentencePiece | LLaMA 2 | 32k | SentencePiece |
| tiktoken-based BPE | Llama 3 | 128k | Byte-level BPE |
Tokenization is not just an implementation detail – it directly affects how you build with and pay for LLMs.
Many text-generation APIs account for input and output in tokens. Do not estimate a request’s cost from word count alone: code, JSON, spaces, and languages tokenize differently. Count with the chosen model’s supported tokenizer or counting endpoint, then apply that provider’s current pricing. Token count is one input to cost, not a substitute for the pricing contract.
Token boundaries need not align with individual letters. A task such as counting the letters in a word can require reasoning within a token or across several tokens. Use a character-counting function when exact counts matter rather than depending on a model to infer them.
A context-window limit is measured in tokens rather than words or characters. Whether a request fits also depends on instructions, conversation history, tool definitions, and the output allowance under the provider’s contract. The context-window guide shows how to budget the complete request.
Tokenization is the lens through which every language model sees the world. Understanding it gives you a sharper intuition for why models behave the way they do.
Here is the full journey from your keyboard to the model:
In the text-processing path described here, token IDs select learned token embeddings that pass through the network. These token embeddings are different from the sentence or document vectors used in semantic search. The transformer architecture guide follows the next processing steps.
Tokenization is the bridge between human language and machine arithmetic. It is a deceptively simple idea – compress frequent patterns into single symbols – that turns out to be one of the most consequential design decisions in the entire LLM stack.
Tiktokenizer is a free browser tool that lets you paste any text and see exactly how it gets split into tokens, with support for multiple models including GPT-4, GPT-4o, and LLaMA. Try pasting a code snippet, a sentence in Hindi, or a run of emoji – the differences in token count are immediately visible and genuinely surprising.
Scala, JVM, AI, and backend systems. I send practical articles when there is something worth reading.
Join the newsletterNecessary cookies keep the site working. With your permission, I use analytics cookies to understand which articles are useful.