Understanding LLM Tokens: A Deep Dive into Tokenization Logic

    This article explains what tokens are in the context of Large Language Models (LLMs) and how tokenization works. It covers the differences between tokens, words, and characters, the reasons behind subword tokenization, context window limitations, and common engineering pitfalls to avoid when managing token budgets.

    Many developers first encounter “Token” and think of it as an arcade coin: insert it to make the model run. While intuitive, this isn’t entirely accurate. A common confusion is whether a token equals a word, a character, or a letter, and why API calls sometimes return an “exceeded maximum token limit” error. To answer these, we must look at the front end of LLM text processing: tokenization and ID mapping.

    1. Defining Token: The Minimum Unit for Model Read/Write

    LLMs do not process raw strings directly. Before text enters the model, a tokenizer splits the original text into chunks, mapping each chunk to an integer ID in the vocabulary. The model actually computes the vector sequence corresponding to these IDs. These “chunks” are tokens.

    Therefore, a token is neither a naturally occurring linguistic unit nor a fixed number of characters. It is the segmentation result derived by the tokenizer based on its vocabulary, merge rules, and training corpus statistics. In other words, a token is the “atom of text from the model’s perspective.” The token count and segmentation method for the same sentence can vary across different models and tokenizers.

    A typical workflow can be summarized as:

    1. Raw text
    2. -> Normalization (spaces, punctuation, case, etc., depending on the tokenizer)
    3. -> Split into a sequence of tokens
    4. -> Mapped to integer IDs in the vocabulary
    5. -> Fed into the model for computation

    This is why “token count” cannot simply equal “word count.” It is more like a compressed sequence length unit.

    2. Tokens Are Not Words, Nor Characters

    The most common misconception is “one word equals one token.” This isn’t always true in English. Using illustrative segmentations:

    1. "cat" -> might be ["cat"]
    2. "cats" -> might be ["cat", "s"]
    3. "tokenization" -> might be ["token", "ization"]

    The above are illustrative; actual results depend on the tokenizer. Common words might be captured as a single token; words with affixes might be split into “root + affix”; rare words, misspellings, or mixed cases might be fragmented further, degrading to character or byte levels in extreme cases.

    Similar phenomena exist in Chinese. Usually, a single Chinese character might correspond to a token, but high-frequency phrases, punctuation, whitespace, numbers, and special symbols are segmented differently. For example, a common phrase might be segmented close to one-character-one-token, while high-frequency combinations might be merged into a single token in some tokenizers. For rare characters, emojis, or mixed-language text, the token count can significantly exceed the visually perceived “character count.”

    You can use a simple table to understand the differences among the three:

    Perspective Unit Characteristics
    Character Letter, character, symbol Fine granularity, long sequence, sparse semantics
    Word Linguistic word Strong semantics, but vocabulary explosion and many out-of-vocabulary words
    Token Chunks split by tokenizer Balances vocabulary size, sequence length, and semantic combination ability

    Thus, a token is an engineering compromise: not as fragmented as characters, nor as rigid as whole words.

    3. Why Introduce the Token Layer?

    If processed directly with characters, the sequence becomes very long. Character-level sequences have a small vocabulary but require more computation steps for the same text. The model must spend more positional capacity remembering low-level combinations like “l, o, v, e”. Longer sequences exhaust context windows faster and increase attention computation and engineering transmission costs.

    If processed directly with words, the vocabulary expands rapidly. Natural language contains numerous proper nouns, morphological changes, spelling variants, neologisms, and mixed languages. A complete vocabulary can hardly cover all cases. When encountering out-of-vocabulary (OOV) words, the model falls into a “cannot represent” dilemma.

    The core idea of subword tokenization is to package high-frequency letter or character combinations into a single token, while splitting low-frequency combinations into smaller fragments. This allows common content to be represented with shorter sequences while enabling the representation of unseen words through combined fragments. Common Byte Pair Encoding (BPE) algorithms start from smaller units and iteratively merge high-frequency adjacent fragments to form the vocabulary. Because vocabulary sizes and merge rules differ across models, token counts cannot be directly compared across models.

    4. Context Windows and “Exceeded Maximum Token Limit”

    LLMs typically have a maximum context length, measured in tokens. This limit usually accommodates both input and output. The token budget for a request can be roughly written as:

    1. Total budget = System prompt tokens + User input tokens + History tokens
    2. + Tool returned or retrieved content tokens + Reserved output tokens

    When the sum of input, history, retrieved segments, and expected output exceeds the limit, the API might return an error like “exceeded maximum token limit”. Many developers only focus on the length of the current question, ignoring that multi-turn dialogues accumulate historical messages, Retrieval-Augmented Generation (RAG) injects multiple text segments into the prompt, and structured outputs also consume tokens.

    Common engineering strategies include: compressing redundant prompts; chunking long documents; summarizing before answering; limiting history turns; filtering retrieved content for relevance; reserving an explicit budget for output; and estimating and truncating tokens before concatenation. When truncating, ensure that semantically complete fragments are retained to avoid cutting key instructions or code blocks in half.

    5. Common Misconceptions and Engineering Recommendations

    First, do not equate token count with character count. Chinese, English, code, punctuation, and emojis have different segmentation efficiencies. Second, do not use empirical values from one model to accurately estimate another; you must estimate using the specific tokenizer. Third, do not calculate only input and forget output. Fourth, longer prompts are not always better; redundant content squeezes the context budget and dilutes key information.

    An illustrative budget check logic is as follows:

    1. input_tokens = tokenizer.count(system_prompt)
    2. + tokenizer.count(user_prompt)
    3. + tokenizer.count(history)
    4. + tokenizer.count(retrieved_context)
    5. if input_tokens + reserved_output_tokens > max_context:
    6. compress_or_split()

    Once you understand tokens, many issues become clear: why the “length feel” differs between Chinese and English, why long document Q&A requires chunking, why multi-turn dialogues gradually slow down or error out, and why prompt design emphasizes conciseness and structure. Tokens are not mysterious coins; they are the measurement and computation unit for model text processing. Mastering their segmentation logic and budgeting methods is the foundation for writing good prompts, managing context, and controlling API costs.