Tokenization

Tokenization converts text into the units consumed by a model. It affects language model architecture, pretraining, prompt cost, chunking, truncation, latency, and generation boundaries. In production systems, “how many tokens?” is a cost, context, and reliability question, not only a preprocessing detail.

Subword vocabularies

Subword tokenizers learn a vocabulary of pieces so rare words can be represented as combinations. Byte-pair encoding repeatedly merges frequent adjacent symbols; other systems use unigram or byte-level variants. Context limits count tokens, not words, so context construction needs the model’s tokenizer.

Worked BPE Example

Starting with lowest as characters plus an end-of-word marker, learned byte-pair merges reduce the sequence:

Merge ruleTokens after applying the rule
Startl, o, w, e, s, t, </w>
l + o -> lolo, w, e, s, t, </w>
lo + w -> lowlow, e, s, t, </w>
e + s -> eslow, es, t, </w>
es + t -> estlow, est, </w>
low + est -> lowestlowest, </w>

Without the last merge, the same word would remain split as low and est. That affects context length, billing, prompt truncation, and the units available to language model architecture during generation.

Text typeWhy token counts can surprise
NumbersDigit grouping and separators may split into several tokens.
CodeSymbols, indentation, and rare identifiers can tokenize densely.
Non-English textCoverage depends on the tokenizer training mixture.
Tables or JSONRepeated punctuation can consume context quickly.

Why token counts matter

System concernTokenization effect
Context budgetlong prompts may evict evidence, examples, or instructions.
Costhosted APIs usually bill on input and output tokens.
Latencylong inputs increase prefill time; long outputs increase decode time.
Retrieval chunkschunk boundaries should target token counts, not character counts.
Structured outputJSON punctuation and repeated field names consume output budget.
Multilingual supportsome languages may require more tokens for the same meaning.

Realistic failure case

A support RAG system chunks documents by 2,000 characters and assumes each chunk fits comfortably. A policy table with many product IDs, currency values, and JSON-like snippets tokenizes far denser than prose. At runtime, the context packer silently drops the final chunk containing the actual approval rule. The final answer then looks like a model hallucination, but the root cause is token-budget accounting.

The fix is to measure chunks with the target model tokenizer, reserve budget for instructions and output, and log both input-token and output-token counts for cost and latency optimization.

Caveats

Code, tables, numbers, and non-English text can tokenize very differently from prose. Tokenization changes can break cached counts, chunk sizes, prompt templates, and latency estimates. Always count with the tokenizer of the model that will actually serve the request.

References