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.

Tokens & tokenizationDiagram with these labels, in reading order: text→tokens; tokens→IDs; IDs→model; Input Text; Tokenization; Algorithm; Token IDs; Model Input.text→tokenstokens→IDsIDs→modelInput TextTokenizationAlgorithmToken IDsModel Input
Tokenization Process Overview

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.

  1. Input text goes through the tokenization algorithm.
  2. Common words become single tokens; rare words split.
  3. Each token gets an integer ID.
  4. 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

SchemeStrengthWeakness
Character-levelCovers any inputSequences up to four times longer, slower and costlier
Word-levelShort sequencesFails on novel words, misspellings and jargon outside the training data
Sub-wordFixed vocabulary that still encodes emojis and codeBoundaries 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.

Tokenization trade-offs Sub-word tokenization balances vocabulary size against flexibility. The tokenizer that ships with the model sets how many tokens, and so how much money, your text uses.

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.

Tokens & tokenizationDiagram with these labels, in reading order: BPE issue; Formatting issue; Limit issue; Fix model; Fix formatting; Tokenization failure; Incorrect BPE model; Improper input text format; Mismatch in token limits; Update BPE model; Preprocess input text.BPE issueFormatting issueLimit issueFix modelFix formattingTokenization failureIncorrect BPE modelImproper input text formatMismatch in token limitsUpdate BPE modelPreprocess input text
Debugging tokenization issues
  • 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.
The call If you train the model yourself, retraining the tokenizer on a more representative dataset can help, at the cost of training time and resources. Track token counts and perplexity on a schedule.

Back to the knowledge baseRequest an assessment