LLMsLoading progress…

Characters, bytes and tokens

Trace a toy tokenizer and explain byte-pair merges, vocabulary IDs and token-budget limits.

Three different things people call "a letter"

A language model cannot read text directly; it reads numbers. The first question is: numbers for what? There are three different units, and mixing them up is the source of many surprises.

  • A character (more precisely a Unicode code point) is a symbol such as A, é or 😀.
  • A byte is a number from 0 to 255. Text is stored as bytes using an encoding; UTF-8 uses 1 byte for ASCII letters and up to 4 for other symbols.
  • A token is whatever piece the model's tokenizer decides to treat as one unit: a byte, a word piece, or a whole word.
charactercode pointUTF-8 bytescount
hU+0068681
éU+00E9C3 A92
lU+006C6C1
lU+006C6C1
oU+006F6F1
😀U+1F600F0 9F 98 804
6 characters (code points) → 10 bytes A byte-level tokenizer starts from these 10 numbers (each 0–255). Token count depends on the tokenizer on top.
Type text to see its characters and bytes. Try an accented letter, an emoji and a family emoji.

Predict first. Must an emoji occupy exactly one byte and one token?

"héllo😀".encode("utf-8")   # b'h\xc3\xa9llo\xf0\x9f\x98\x80' -> 10 bytes for 6 characters
Chapter context

A language model receives token IDs, not visible words. The tokenizer chooses how text becomes those IDs. Different tokenizers can represent the same string with different lengths, so character count is not a universal token count.

Chapter reference: math, worked example & code

This section belongs to the whole chapter, so it is the same on every lesson in it and may cover a different lesson's topic. The code for the experiment you just ran is inside the experiment itself.

Math and notation

In a toy merge, replacing m nonoverlapping adjacent pairs by one symbol reduces sequence length by m. A request budget must include formatted input plus requested output within the model's supported context; exact accounting is model-specific.

Worked example

Start with toy symbols ['a','b','a','b'] and a vocabulary containing a and b. The pair (a,b) occurs twice. A merge creates symbol ab and encodes the sequence as ['ab','ab'], reducing length from 4 to 2. This deliberately omits byte encoding and pre-tokenization so the merge operation is visible.

Reference code

The following example uses the Python standard library and does not call a remote service.

tokens = list('abab')
merged=[]
i=0
while i < len(tokens):
    if tokens[i:i+2] == ['a','b']:
        merged.append('ab'); i += 2
    else:
        merged.append(tokens[i]); i += 1
assert merged == ['ab','ab']
assert len(tokens)-len(merged) == 2

Python examples are provided for study and local execution; this page does not execute Python.

Common mistakes

A token is not necessarily a word. A vocabulary ID is a label, not a magnitude. A hand-built toy merge sequence is not a drop-in replacement for a production tokenizer.