A neural network only computes on numbers, never on words directly. Tokenization is the step that breaks text into pieces and maps each one to a number — and it's the direct, mechanical reason LLM pricing is per-token, context windows have hard limits, and certain prompts behave in surprising ways.
3 min read
Every lesson so far described a neural network operating purely on numeric inputs. Text obviously isn't numbers — tokenization is the step that converts it, breaking a string into a sequence of tokens (not necessarily whole words) and mapping each one to a numeric ID from the model's fixed vocabulary.
"unbelievable" -> ["un", "believ", "able"] (illustrative — real splits vary by tokenizer)
-> [1234, 5678, 91011] (numeric IDs from the model's vocabulary)
Splitting on whole words sounds simpler, but breaks badly for languages without spaces, made-up words, typos, and the sheer number of possible words in any language (a vocabulary would need to be enormous to cover every real and possible word directly). Modern tokenizers instead learn a vocabulary of common subword pieces from massive amounts of text — common whole words often do end up as single tokens, but rarer or unfamiliar words get split into smaller, more common fragments the model has actually seen often enough to have learned something about.
This directly explains several things that otherwise look arbitrary:
Every practical constraint of an LLM API is expressed in tokens because tokens are the model's actual unit of computation and memory — not an arbitrary billing choice:
Because pricing and context limits are token-based, any real system calling an LLM API needs to estimate token count before sending a request — to predict cost, and to make sure a prompt plus its expected response will actually fit within the model's context window. Every major LLM provider publishes an official tokenizer library specifically so applications can count tokens locally, without an API call, before committing to one:
# Illustrative — the exact library differs per model provider
tokens = tokenizer.encode("How many tokens is this sentence?")
print(len(tokens)) # a real integer, used for cost/fit estimation before calling the APICheck your understanding
A quick comprehension check — not tracked, not graded, just for you.
1. Why do modern tokenizers split text into subword pieces rather than whole words?
2. Why is LLM API pricing and context window size measured in tokens rather than characters or words?
3. Why might the same sentence cost more tokens in one language than another?
AI & ML Fundamentals