Tokenization
Tokenization cuts your text into the pieces a model actually reads. Frequent words stay whole while rare ones split, so five words can cost six tokens or thirty-two characters depending on the cut.
Why Does This Exist?
Your model never sees your sentence. It sees a row of integers, because everything after this step, from how an LLM reads tokens to the final prediction, is arithmetic, and arithmetic needs numbers. Tokenization is the step that turns text into those numbers: cut the string into pieces, then look each piece up in a fixed list.
The lazy cut is splitting on spaces, one piece per word. It feels right until real text shows up. Names you have never seen, typos, fresh slang, code, and whole languages your training data barely covered all produce words outside your fixed list, and a word-level list has nowhere to put them. Each stranger becomes the same blank placeholder. Meaning drains out.
The strict cut goes the other way: split into single characters. Nothing is ever unseen, since the character set is small and closed. But a five-word sentence is suddenly thirty-two positions long, and attention work grows with the square of that length. You pay for full coverage with extra compute on every step.
So this page covers the cut itself: the three granularities, why the middle one won, and what the choice costs you in tokens. The algorithm that builds the middle vocabulary gets its own page in byte-pair encoding, and what the resulting integers become lives in embeddings.
Think of It Like This
A print shop with three stamp sets
You run a print shop and every order has to be stamped out. With a whole-word stamp set, common orders fly: one press per word. Then someone orders a name you have no stamp for, and you are stuck carving a new stamp for a word you may never see again.
With a single-letter set, you are never stuck. Every order is printable. But each word now takes a press per letter, so a short sentence takes thirty-two presses and the queue backs up behind you.
So you build a third set from your order history: whole stamps for the words you print daily, plus fragment stamps for everything else. Most orders still take a press or two per word, and the strange name takes three fragment presses instead of failing outright or taking ten letter presses.
It stops here: real vocabularies are learned from raw frequency, not from meaning or neat syllables, and stamps say nothing about the numbering step, where each piece becomes an ID, or about the context bill, which counts pieces instead of words.
How It Actually Works
Call the raw string , the fixed piece list the vocabulary , the size of that list , and the cut itself , where each is one piece and is how many pieces the string produced. Tokenization is two moves. First, segment: cut into . Second, look up: replace each piece with its row number in . The model never sees letters after this. It sees those integers. Once you have IDs, next-token prediction is the training job that consumes them, one position at a time.
From text to token IDs
A token is one entry of as it appears in your text: a whole common word, a fragment like izing, or a single character. Detokenization runs the lookup in reverse at the end, gluing pieces back into text, which is why generated output sometimes shows the seams of the split, like a space that went missing before punctuation. The quirks that fall out of frequency-learned pieces get their own treatment in tokenization artifacts.
Why the middle granularity won
A word-level is fixed at training time, so any new word maps to one placeholder. Coverage can never close. A character-level is tiny and complete, but runs several times longer than the word count for the same , and every extra position costs attention work. Subword keeps a middle of frequent fragments: common words keep whole entries, rare or unseen words split into familiar parts, so nothing is ever unrepresentable and common text stays compact.
Name the trade-off plainly. A bigger buys a shorter but a larger embedding table, including rare entries that barely get trained. A smaller buys a compact table but a longer and a longer context bill. And know when the default loses: if your task is characters themselves, spelling or letter counting, subword pieces hide the very letters you need, so pay for characters there. The whole scheme assumes your text looks like the corpus the merges were learned from. When it does not, another script or a JSON payload, the same meaning costs more pieces, and context windows count pieces, not words.
Worked example
Take as tokenizing rare words costs more and cut it three ways.
Word cut: split on spaces for 5 pieces. Character cut: count letters, , plus 4 word spaces, for positions. Subword cut, one reasonable split with a leading mark for spaces: token, izing, _rare, _words, _costs, _more, for 6 pieces. Only the genuinely uncommon word pays extra, and it pays one extra piece, not ten extra characters.
So the same is 5, 6, or 32 long depending on the cut. Those three numbers are the diagram above and the snippet below.
Code
Word and character cuts in plain Python, plus the illustrative subword split from the worked example, so the 5, 6, 32 counts are checkable rather than asserted.
def word_tokenize(text: str) -> list[str]: return text.split() # crude: whitespace only, no punctuation handling
def char_tokenize(text: str) -> list[str]: return list(text.replace(" ", "_")) # one piece per letter or space
text: str = "tokenizing rare words costs more"words: list[str] = word_tokenize(text)chars: list[str] = char_tokenize(text)subwords: list[str] = ["token", "izing", "_rare", "_words", "_costs", "_more"]
print(words) # -> ['tokenizing', 'rare', 'words', 'costs', 'more']print(len(words), len(subwords), len(chars)) # -> 5 6 32print("".join(chars)) # -> tokenizing_rare_words_costs_moreFive word pieces, six subword pieces, thirty-two character pieces. The subword list is the hand split from the worked example, not any released tokenizer's output, which is exactly why you must recount with the real tokenizer before budgeting anything.
Watch Out For
Budgeting context or money in words
Symptom: your estimate says a document fits, then production overflows the context or the bill lands heavier than quoted. The cause is counting words. Limits and prices are denominated in tokens, and the word-to-piece ratio shifts with language, punctuation, and code, so a word estimate silently understates both.
Fix: never budget context or money in words. Run the text through the model's own tokenizer and budget the count it returns, with headroom for the reply you have not generated yet.
Counting with a different tokenizer than the model uses
Symptom: two counts for the same text disagree, and the one you trusted was the smaller. Each tokenizer carries its own and its own merge rules, so swapping tokenizers between pipeline steps, or across a version bump that changed normalization, segments the same string differently.
Fix: pin the exact tokenizer version alongside the model it belongs to. Count, truncate, and budget with that tokenizer only, and recheck the counts whenever either one is upgraded.
The Quick Version
- Tokenization turns text into numbered pieces before a model reads anything, and everything downstream counts those pieces, not words.
- Word pieces are shortest per sentence but break on any unseen word, while character pieces never break and make sequences many times longer.
- Subword vocabularies keep frequent fragments whole and split only the rare stuff, so five words can cost six pieces instead of thirty-two characters.
- Token count is not word count, and the gap shifts with language and format, so another script or a JSON payload can cost more for the same meaning.
- Bigger vocabularies buy shorter sequences and larger tables, smaller ones buy compact tables and longer sequences, and character cuts win only when the letters themselves are the task.