Tokenization
Read a little, play a little. No scary maths, and no rush.
Words are not the unit — pieces are
Models never see "words." They see tokens: subword pieces learned from data. Common words survive whole (great), rare ones shatter (Token + ization), and spaces, punctuation, and emoji each cost their own pieces:
"Tokenization is great!" → [Token][ization][ is][ great][!]
That leading space inside [ is] surprises everyone once — spaces count as content, and formatting quirks (double spaces, fancy quotes) silently eat your budget.
How BPE learns the pieces
Byte-Pair Encoding starts with single characters and repeatedly merges the most frequent adjacent pair — t+h → th, then th+e → the — until the vocabulary (often ~100K pieces) is full. Frequent words become single tokens; rare words decompose. This is also why models handle English smoothly and stumble on rare languages: fewer merges learned for them.
Why you must think in tokens
- Money: billed per 1K tokens (~750 English words). Verbose prompts are a subscription to yourself.
- Limits: context windows are token budgets — a 128K window is not 128K words.
- Chunking: RAG chunks must be sized in tokens, not characters; code and non-English text are token-dense.
# rough estimate for English prose (use tiktoken for real counts)
def est_tokens(s): return int(len(s) / 4)
Try /animations/tokenization and the homepage tokenizer to feel it.
Counting tokens for real
The divide-by-four estimate is fine for back-of-envelope math, but budgets and chunk sizes deserve exact counts. OpenAI publishes its tokenizer as the tiktoken library, and most providers offer a token-counting endpoint or library of their own.
import tiktoken
enc = tiktoken.get_encoding("o200k_base")
print(len(enc.encode("Tokenization is great!")))
Two details catch people out. First, chat requests carry overhead: every message adds a few tokens for its role and formatting. Second, the reply is counted too, and output tokens are usually priced higher than input tokens. A request is input plus output, both in tokens.
A budgeting example
Say your model has a 128K-token window and you are building a RAG assistant. One request might hold:
- a system prompt of 1,500 tokens,
- 6,000 tokens of earlier conversation,
- five retrieved chunks of 400 tokens each, so 2,000,
- the user's question, about 100,
- plus 1,000 tokens reserved for the answer.
That is roughly 10,600 tokens, comfortably inside the window. The bigger concern is cost and speed: the system prompt and history are resent on every turn, so trimming 1,000 tokens there saves money on every single request.
Surprises that cost tokens
- Other languages and scripts often need several times more tokens per word than English.
- Numbers split unpredictably (
2024may be one token,20241two), which is one reason models stumble at arithmetic. - Pretty-printed JSON spends tokens on indentation. Compact JSON says the same thing for less.
- Letters are invisible. The model sees
strawandberry, not individual letters, so counting characters is genuinely hard for it. - Repeated boilerplate such as long instructions or examples pasted into every request is billed every time. Short, reusable prompts compound into real savings.
Remember this
- Tokens, not words, are the model's native unit — and your billing unit.
- Spaces count, code is dense, rare words shatter. Chunk and budget accordingly.
Check your understanding
5 questions · first-try answers earn the most XP
My notes
Saved in this browser. Highlight a line above and save it, or write it in your own words.
Nothing saved yet. Your highlights will live here.
References
Finished reading?
Ticking it here also ticks the chapter in the sidebar, the section count and your streak — it is all one number.
Related chapters
Spotted a mistake or want a topic covered? Report an issue