Sub topic 2
How text is split into tokens
This chapter assumes you can read basic Python and know that an LLM receives tokens, not raw characters. It explains how a tokenizer turns text into those tokens and why the result is hard to guess by eye.
From text to token IDs
A tokenizer is the component that converts a string into a list of integers before the model sees it. It uses a fixed vocabulary, which is a table of text pieces the model knows, each with an ID. The model reads the IDs. It never sees the original characters.
Subword vocabularies
Most modern tokenizers use subword units. A subword is a piece that can be a whole word, part of a word, a single character, or punctuation. Common words are usually one token. Rare words are split into several pieces.
Many tokenizers are built with byte pair encoding (BPE). BPE starts with single bytes or characters and repeatedly merges the pair that occurs most often in a large training text. Each merge adds one entry to the vocabulary. Training stops when the vocabulary reaches a target size, often tens of thousands to a few hundred thousand entries.
This design has two useful effects:
- Frequent strings become short token sequences, which keeps requests small.
- Any input can still be encoded. Byte-level BPE falls back to individual bytes for text it has never seen, so there is no "unknown word" failure.
Seeing it in code
The tiktoken library implements the tokenizers used by several OpenAI models. This script shows the pieces for a few strings.
import tiktoken
enc = tiktoken.get_encoding("cl100k_base")
samples = [
"Tokens are not the same as words.",
"internationalization",
" hello",
"hello",
"Hello",
"1234567890",
]
for text in samples:
ids = enc.encode(text)
pieces = [enc.decode([i]) for i in ids]
print(f"{text!r}: {len(text)} chars, {len(ids)} tokens")
print(" ", pieces)Run it and compare the pieces for " hello", "hello", and "Hello". They are usually different tokens with different IDs.
What affects the split
| Factor | Effect on tokens |
|---|---|
| Leading space | A space is usually attached to the start of the following word, so `" hello"` and `"hello"` differ. |
| Capitalization | `Hello` and `hello` are separate vocabulary entries, or split differently. |
| Rare or long words | Split into several subword pieces. |
| Numbers | Often cut into short digit groups, so long numbers cost more than they look. |
| Code and markup | Indentation, brackets, and symbols can each take tokens. |
| Non-English text | Languages that were rare in the training text often need more tokens per word. |
For ordinary English prose, a common rule of thumb is about four characters, or three quarters of a word, per token. Treat it as a rough estimate only. It fails for code, numbers, and many other languages.
Common errors
- Equating tokens with words.
len(text.split())and the token count differ, and the gap changes with the text. - Using the wrong encoding. Picking
cl100k_basefor a model that uses a different encoding gives a close but wrong count. - Tokenizing pieces separately. Encoding two strings apart and adding the counts can differ from encoding the joined string, because spaces and merges cross the boundary.
- Assuming IDs are portable. The ID for a piece in one vocabulary means nothing in another.
- Ignoring the format around the text. Chat requests add role markers and separators, so the billed count is slightly larger than the raw text count.
Exercise
You encode "cat" and " cat" with the same tokenizer. Why might they give different token IDs, and why does it matter when you count tokens for a prompt?
Show answer
The tokenizer usually attaches a leading space to the word that follows it, so " cat" is a separate vocabulary entry from "cat". It matters because the count depends on exact whitespace and boundaries. Counting pieces separately or ignoring spaces can give a different total than encoding the real prompt as one string.