How LLM Token Counting Works: A Practical Guide

    Large language models bill and limit usage by tokens, not by characters or words. This guide explains what tokens are, how input and output counts add up, why languages and code tokenize unevenly, and how to estimate, budget, and avoid common undercounting mistakes.

    Large language models do not treat text as “one Chinese character equals one unit” or “one English word equals one unit.” They see a sequence of discrete IDs. The industry calls the smallest processing units those IDs represent tokens. When an application says a call “used N tokens,” it means: the full text sent into the model is split into tokens, plus the newly generated text is split by the same rules. The two counts together are the total usage of that call.

    For example, if a request is split into about 100 tokens before it reaches the model, and the reply is then split into about 200 tokens, the call totals about 300 tokens. Those numbers only illustrate addition; they are not a rate or a default length. What actually drives the count is the tokenizer, language, punctuation and whitespace, and whether you also send history and system instructions.

    Why tokens exist

    Natural-language writing units are not uniform. English has spaces; Chinese is usually written without them; Japanese mixes kana and kanji; code is full of symbols. Processing every character would make the vocabulary huge and training and inference expensive. Forcing “words” fails for Chinese, which has no natural separators. Tokens sit between characters and whole words: fragments the tokenizer cuts by statistical patterns. A token may be a common Chinese character, half an English word, a frequent stem, punctuation, a digit, or whitespace.

    So the answer to “does input/output go down to a single Chinese character?” is usually no. Tokenizers must cover major natural languages and code with one finite vocabulary. Frequent, stable fragments become shorter token sequences; rare combinations are split more finely and cost more tokens. That is why the same meaning can differ by several times in token count when you change language or wording.

    Counting happens before the model computes

    A call can be understood in three stages.

    Stage 1: Assemble context. Besides the current user message, this often includes role settings, format requirements, prior turns, and retrieved references. All of that exists before the model runs and all of it is tokenized.

    Stage 2: Encode. The tokenizer maps the string to a sequence of token IDs. Sequence length is the input token count. The context window limits this sequence plus tokens that will be generated—not the character count you see in an editor.

    Stage 3: Decode and generate. The model produces new token IDs one by one, and the same tokenizer turns them into text. Generation stops at a length cap, an end marker, or another stop condition. Tokens already produced count as output tokens, even if you display only part of them.

    On-screen reply length is therefore not a billing or truncation basis. Hidden prompts, multi-turn history, and tool returns also consume input quota.

    Tokens vs. characters and words

    There is no fixed ratio—only empirical ranges, not a formula.

    In typical English, one token often maps to several characters, sometimes a short word or stem; hyphenation, rare names, or irregular spaces split further. In Chinese, a common character may be close to one token, but phrases, punctuation, and rare characters are not one-to-one. Digits, URL-like strings, repeated symbols, and mixed full-width/half-width characters change the split. Indentation and long string literals in code usually cost more tokens than natural language of the same length.

    State the mapping this way: a token is an entry in the tokenizer vocabulary; a character is a writing-layer symbol. Several characters may form one token, or one character may become several tokens. “One Chinese character = one token” and “one English word = one token” are rough mental math, not precise rate limits.

    For numbers you can audit, use the same tokenizer as the target model offline. Vocabularies and rules differ across model families; estimating model B with model A’s tokenizer can be far off. In engineering, separate estimates (capacity planning before launch) from server-reported usage (billing checks and alerts).

    What is easy to undercount

    Developers most often underestimate silent context, not the user question itself.

    System instructions and output-format rules consume input every time. Longer conversations mean more history; without summarization or a sliding window, input tokens grow roughly linearly. Pasting a whole document into the prompt is usually more expensive—and more likely to fill the window—than retrieving relevant passages first. Function descriptions, JSON from tools, and text transcribed from images are tokenized as soon as they become model-readable strings. Repeated politeness, examples, and required full chain-of-thought significantly raise output tokens.

    Output is asymmetric: generation is token-by-token, and the length cap is a hard constraint. A very large cap does not make answers more accurate; it only raises worst-case usage and latency. A cap that is too small can cut mid-sentence; the application should inspect the finish reason and decide whether to continue.

    Practical estimation and control

    Before launch, sample typical corpora with the tokenizer. Cover at least: short Q&A, long-document Q&A, multi-turn support, code completion, and mixed Chinese/English. For each class, record input tokens, output tokens, character counts, and latency to get your character-to-token coefficients—not ratios from someone else’s blog.

    At runtime, use layered budgets: reserve a fixed quota for system instructions, cap retrieved material, and leave the rest for the user question and generation. When material exceeds the cap, truncate low-relevance passages first or summarize before insertion; avoid hard cuts in the middle that break meaning. For multi-turn chat, keep the last few turns verbatim and compress older content into a short state so total input stays below a fraction of the window and generation has headroom.

    Stop conditions should match the product. For structured results, use clear end markers and validation rather than inflating max generation length. For long writing, generate in segments and pass a written summary between segments so a single output cannot run away. Monitor input, output, and reject/truncate rates together: steadily rising input usually means history or retrieval is unbounded; output always hitting the cap usually means the length limit or prompt is wrong.

    Illustrative budget logic (control idea only; not a specific API):

    1. budget = context_limit - reserved_for_output
    2. prompt_tokens = count_tokens(system + history + user + references)
    3. if prompt_tokens > budget:
    4. references = shrink_to_fit(references, budget - count_tokens(system + history + user))
    5. prompt_tokens = count_tokens(system + history + user + references)
    6. call_model(prompt, max_output_tokens=reserved_for_output)
    7. total = prompt_tokens + actual_output_tokens

    count_tokens must match the production tokenizer; actual_output_tokens should follow server stats. If the two diverge for a long time, check for missed history, templates, or multimodal transcripts before quietly lowering an empirical coefficient.

    Common mistakes

    Treating tokens as characters or words undercounts Chinese and sometimes overcounts English. Counting only the user input box misses system prompts and history. Hard-blocking on character length times a fixed factor can kill valid long documents or allow over-window requests. Assuming “internal reasoning does not use tokens” is imprecise: if that content is actually generated or written back into the next turn, it counts. Different models do not share the same length assumptions; recapture samples after you switch models.

    Summary

    A token is the model’s smallest unit of text, defined by the tokenizer—not by naive character or word counts. Total usage for a call is input sequence plus newly generated sequence. Chinese, English, code, and symbols tokenize at different densities, so measure with the same tokenizer as the model. Applications should budget system instructions, history, and external material; plan with sampled coefficients; reconcile against server usage; and keep context and cost predictable with sliding windows, summaries, and generation caps.