Token and Tokenization

Large language models do not read language the way humans do. When a model generates an answer, writes a story, or produces code, it is not really reading sentences for meaning in a human sense. It is processing numbers. The text a person sees is converted into representations that the model can understand and manipulate.

The central mechanism behind this conversion is the token. A token is the basic unit of text that a model processes. It can be a full word, a part of a word, a character, or even a punctuation mark. Tokenization is the process of splitting a raw text string into a sequence of tokens.

For instance, a tokenizer might split the string "Hello, world!" into three tokens: ["Hello", ",", " world!"]. Each token is then mapped to a token ID, which corresponds to a vector in the model’s embedding space.

It is important to note that a token is not always simply a word. A single word may be split into multiple tokens. A character in some languages may also become one or more tokens. The tokenizer determines how the text is divided.

Subword Tokenization

Early natural language processing systems often relied on word-level or character-level tokenization. Word-level tokenization had the problem of large vocabularies and unknown words. Character-level tokenization avoided unknown words but created long sequences that were difficult for models to handle.

Modern large language models commonly use subword tokenization. This approach tries to balance vocabulary size and sequence length.

Three main approaches are widely used:

  • BPE, or Byte-Pair Encoding, starts with basic units and repeatedly merges the most frequent adjacent pair. GPT-style models often use a variant of this method.
  • WordPiece chooses merges based on likelihood rather than raw frequency. BERT-style models frequently use it.
  • Unigram begins with a large set and removes tokens that contribute least to the probability. T5 and LLaMA models use SentencePiece, which supports this method.

Token Usage in Practice

Token usage affects more than how a model reads text. It influences cost, context limits, performance, and safety.

  • API pricing: Many commercial models charge based on token count. Developers can manage costs by shortening prompts, removing redundant text, and optimizing inputs.
  • Context window: Context limits are often measured in tokens rather than characters. When input exceeds the limit, truncation or failure may occur.
  • Safety and alignment: Token sequences can sometimes be manipulated to bypass filters. Robust tokenization and input validation are important parts of AI safety.

Different text types also differ in token consumption. English often uses fewer tokens for common words. Languages without spaces may require finer segmentation. Code contains punctuation, indentation, and naming patterns that can increase token count.

Text type Typical form Token cost Main challenge
English "Machine Learning" Low Spelling variants, compound words
No-space language "Natural Language Processing" High Segmentation quality
Code def calculate_sum(): High Indentation, symbols, camel case

Why Tokenization Matters

Tokenization is not only a preprocessing step. It runs through the full lifecycle of a large language model. It affects how context is measured, how prompts are priced, how outputs are filtered, and how multilingual systems behave.

A strong tokenization strategy can improve efficiency and stability. A weak one can lead to wasted tokens, higher costs, and weaker performance.

Conclusion

Tokens and tokenization are foundational to how large language models process human language. From BPE and subword methods to token limits and API pricing, the token is a central concept. Understanding tokenization helps people write better prompts, build more efficient systems, and create more reliable AI applications.