LLM Tokenization Explained: UTF-8, BPE and Token Counts

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.

Step 1 – Encoding text as bytes

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:

Charactertoken
UTF-8 byte116111107101110

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.

Step 2 – Byte Pair Encoding (BPE)

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:

  1. Start with your training corpus encoded as bytes. Vocabulary = 256 symbols (one per possible byte value).
  2. Count every adjacent pair of symbols across the entire corpus. Find the most frequently occurring pair.
  3. Merge that pair into a new single symbol and replace every occurrence of it in the corpus. For example, if t + h is the most common pair, create a new symbol th.
  4. Repeat from step 2. Each iteration the sequence gets shorter and the vocabulary grows by one. Repeat thousands of times.
  5. Stop when you reach your target vocabulary size.

A concrete trace

Suppose the corpus contains just two words repeated many times: low and lower. Start with individual bytes for each character, then follow the merges:

Step 1 l · o · w and l · o · w · e · r Initial characters separated Step 2 lo · w and lo · w · e · r Merge (l, o) into ‘lo’ Step 3 low and low · e · r Merge (lo, w) into ‘low’ Step 4 low and low · er Merge (e, r) into ‘er’

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.

Step 3 – What the vocabulary looks like

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 wordTokensToken count
tokenizationtoken · ization2
unbelievableun · belie · vable3
caféca · fé2
thethe1 (extremely common)
🚀Bytes F0 9F · 9A · 803

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.

The tradeoff – sequence length vs. vocabulary size

At the heart of BPE is a deliberate tension between two forces pulling in opposite directions:

  • More merges → shorter sequences, larger vocabulary. Fewer tokens per sentence means the model can attend to longer contexts with the same compute budget.
  • Fewer merges → longer sequences, smaller vocabulary. Each token carries less semantic meaning but the vocabulary is simpler to manage.

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.

TokenizerModelApproximate vocabularyEncoding note
r50k_baseGPT-350kByte-level BPE
cl100k_baseGPT-3.5 / GPT-4100kByte-level BPE
o200k_baseGPT-4o200kByte-level BPE
SentencePieceLLaMA 232kSentencePiece
tiktoken-based BPELlama 3128kByte-level BPE

Why this matters in practice

Tokenization is not just an implementation detail – it directly affects how you build with and pay for LLMs.

Pricing is counted in tokens, not words

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.

The model “sees” tokens, not characters

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.

Context limits are in tokens

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.

Putting it all together

Here is the full journey from your keyboard to the model:

Step 1 “Hello, world!” Raw text input from the user Step 2 [ 72 101 108 108 …] UTF-8 encoding → each character becomes a byte value (0–255) Step 3 “Hello” · “,” · ” world” · “!” BPE merges → bytes grouped into subword tokens Step 4 [ 9906 11 1917 0 ] Token IDs → integers looked up in the model’s embedding table → transformer

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.

See it for yourself

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.

Share
Edit Template
Newsletter

Get new engineering deep dives

Scala, JVM, AI, and backend systems. I send practical articles when there is something worth reading.

Join the newsletter