A token is a sub-word chunk of roughly 3 to 4 English characters. Tokenizers such as BPE (GPT, Llama 3) or SentencePiece (Llama 2, Gemma) split text into the integer IDs that the model consumes.
Merge tables and integer IDs
Tokenization uses a learned merge table that combines the most frequent byte pairs in a text corpus. Common words such as "the" or "and" become one token. Rarer words, technical jargon and non-English text split into several. "Tokenization" might become "token" and "ization" if both pieces are frequent enough.
Each token then maps to an integer ID. The model never sees text, only these IDs, which is why pricing, context limits and latency are counted in tokens and not in words. A 2048-token limit is roughly 1,500 English words, and the real count shifts with the content.
- Input text goes through the tokenization algorithm.
- Common words become single tokens; rare words split.
- Each token gets an integer ID.
- The model consumes the ID sequence.
Lexing with a learned vocabulary
Tokenization resembles lexing in a compiler, where source code is cut into tokens before parsing. A lexer follows a hand-written grammar. A tokenizer's vocabulary is learned statistically from a large corpus, so its boundaries are harder to predict and depend on the language and the data.
Character-level and word-level alternatives
| Scheme | Strength | Weakness |
|---|---|---|
| Character-level | Covers any input | Sequences up to four times longer, slower and costlier |
| Word-level | Short sequences | Fails on novel words, misspellings and jargon outside the training data |
| Sub-word | Fixed vocabulary that still encodes emojis and code | Boundaries depend on the learned table |
Counting tokens before you send
You rarely choose a tokenizer, because it ships with the model. You do need to budget for it. English text averages around 1.3 tokens per word, and the number rises for JSON, code and languages such as Chinese. Count with a tool like tiktoken before finalizing input, so you stay inside cost and context limits.
Example scenario: input 15% longer than expected
Suppose input length is 15% above the estimate (an illustrative figure). The tokenizer may be splitting your text into more pieces than you assumed, for example because of JSON, code or non-English text, and the extra tokens raise cost and use up context.
- Tokenize a sample input and compare the output with what you expected.
- Check the token frequency distribution for unusual sparsity or missing tokens.
- Log perplexity scores on subsets of validation data to find phrases that cause spikes.