Why Do Large Language Models Measure Usage in Tokens? How Tokenization Really Works

    This article explains why large language models count usage in tokens rather than characters or words. It covers how tokenizers split text into integer IDs, why token boundaries do not align with characters or linguistic words, how embeddings turn those IDs into meaning, and how to estimate token counts for capacity and cost planning. You will also find practical guidance on context management, prompt design, and observability.

    1. A Counterintuitive Behavior: Why Models Miscount Letters

    A model that can write code and revise papers often fails when asked how many times the letter “e” appears in an English word. This is not a knowledge gap but a structural limitation: the model works with numbers from start to finish and never actually “sees” characters.

    When you type “Hello, assistant” into a chat box and hit send, the model receives a sequence of IDs, not text, not a list of individual characters, and not an image. Before text reaches the model, a program called a tokenizer splits it into small chunks and looks up each chunk in a table to get an integer ID. This numbered chunk of text is a token.

    Understanding this explains three things at once: what models are good at, which tasks they handle clumsily, and where your usage quota actually goes.

    2. Four Ways to Represent Text, and Only the Fourth Reaches the Model

    Take the three-character string “Hi!”. A program can represent it in at least four ways:

    1. String: "Hi!", the full text as humans read it;
    2. Character list: ['H', 'i', '!'], split into single characters, 3 items;
    3. Character encoding: [72, 105, 33], one standard Unicode number per character;
    4. Token sequence: also a list of integers, but not a fixed one-to-one mapping from character to number. Instead, text is chunked according to a vocabulary the model learned, then looked up.

    The first three are common in programming; the fourth is the input format of large language models. Schematically:

    1. Raw text : Hello, assistant
    2. Char view : H / e / l / l / o / , / a / s / s / i / s / t / a / n / t → 15 characters
    3. Token view: [Hello] [, ass] [istant] → 3 tokens
    4. ID view : [177519, 137380, 53480]

    The key difference: token boundaries align neither with character boundaries nor with “words” in the linguistic sense. Common words often become a single token each, punctuation may stick to the following character, and rare words, misspellings, and proper nouns get broken into multiple fragments.

    3. What a Tokenizer Actually Does

    The core output of a tokenizer is a vocabulary plus a set of splitting rules. Two approaches are common in engineering: one merges high-frequency substrings, starting from characters and repeatedly combining the most frequent adjacent pairs into larger units; the other searches, for a given vocabulary size, for the segmentation that compresses the corpus best. Both aim at the same goal: covering as much real text as possible with as few tokens as possible.

    Three practical conclusions follow:

    • Common expressions are “cheaper”. High-frequency words are usually one token, while obscure words split into several, so saying the same thing with common wording often costs less quota.
    • Language and writing style affect token counts. For the same amount of information, the language used, whether spaces are present, and how much punctuation appears all change the segmentation.
    • Out-of-bounds tasks are inherently disadvantaged. Counting characters, reversing a string, or comparing letter by letter all require operating inside tokens, which are the model’s invisible minimum unit.

    A simple self-check: feed a piece of text to a tokenizer, inspect the segments and IDs, and compare them with the character count. You will find the token count is usually smaller than the character count (especially for English), but rises sharply for random strings.

    4. How to Roughly Estimate Token Counts

    When you cannot call a tokenizer, use rule-of-thumb ratios for capacity planning: for English, roughly 4 characters per token or 0.75 words per token; for Chinese, roughly 1 token per Chinese character, with punctuation and digits counted separately. These ratios are only for estimating magnitude and should not be used as a billing basis.

    A more reliable approach is to make token counting part of the engineering work:

    1. def estimate_tokens(text: str, is_cjk_heavy: bool) -> int:
    2. # For capacity and cost magnitude estimation only; exact values depend on the actual tokenizer
    3. if is_cjk_heavy:
    4. return len(text) # Chinese approximation: 1 character ≈ 1 token
    5. return max(1, len(text) // 4) # English approximation: 4 characters ≈ 1 token

    In production systems, track input tokens and output tokens separately, and connect them to logging and alerting so you can build usage dashboards by business and by user. This helps locate abnormal consumption and assess whether the context length is sufficient before scaling up.

    5. From Tokens to Embedding Vectors: How IDs Carry Meaning

    IDs alone cannot tell the model whether “cat” is closer to “dog” or “cat” is closer to “car”. Embeddings solve this: each token ID maps to a high-dimensional vector, and tokens with similar meaning sit closer together in vector space.

    1. Token ID → Embedding vector (illustrative, dimensions truncated)
    2. 177519 → [ 0.021, -0.134, 0.887, ... ]
    3. 137380 → [-0.045, 0.201, 0.113, ... ]

    Vectorization brings two direct benefits: similarity becomes computable, which retrieval, deduplication, clustering, and recommendation all build on; and meaning becomes operable, giving analogy, rewriting, and cross-language alignment a shared numerical basis. In retrieval-augmented generation practice, documents are chunked and written into a vector store, the user’s question is vectorized the same way, and matching fragments are recalled by similarity and assembled into the context. The smallest unit of measurement along this chain is still the token.

    6. Turning This Understanding into Engineering Practice

    • Budgets and rate limits: call quotas and rate limits are counted in tokens, so capacity planning must account for both input and output and leave headroom for long-document scenarios.
    • Context management: segment and summarize very long text first, then recall on demand, avoiding irrelevant content that inflates cost.
    • Prompt design: keep instructions concise and examples tight, which lowers token consumption and reduces quality variance from diluted attention.
    • Observability: bring token usage, latency, and failure rates into unified monitoring, and build a stable call pipeline on general-purpose capabilities such as logging, alerting, object storage, and message queues.
    • Platform choice: if you use a cloud model service, look first for a clear usage view and elastic scaling; general-purpose capabilities from providers such as Baidu AI Cloud can serve as a reference point for evaluation.

    A token is not a mystical unit. It is “an ID obtained by looking up text after it has been chunked”. Once you understand the splitting rules, you can explain a model’s weak spots, estimate real costs, and put the path from IDs to meaning, via embedding vectors, to work in real systems for retrieval, clustering, and generation alike.