SproutStackSproutStack home

Tokenization

5 min readBeginner-friendlyFree to read

Read a little, play a little. No scary maths, and no rush.

Share:

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 (2024 may be one token, 20241 two), 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 straw and berry, 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

1. A language model charges per token and has a context window measured in tokens. You are splitting a 50-page PDF so a retrieval system can find relevant passages. What should you size those chunks in?
2. A tokenizer splits the word "tokenization" into four pieces instead of one. Why would a vocabulary ever want that?
3. You paste a prompt into a model and it rejects the request saying the context is full. Which measurement explains the failure most directly?
4. Your RAG system returns a chunk that is semantically perfect but far too long to paste into the prompt. What is the cleanest fix?
5. Two different tokenizers split the same sentence into 6 and 9 pieces. Which statement is true?

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

📬 An occasional email when new lessons land

New lessons and showdowns, no more than a few times a month. No spam, unsubscribe anytime. We store your email only to send the newsletter.