How Large Language Models Count Tokens: Input, Output, Character Mapping, and Estimation

    This article explains what tokens are and why large language models use them instead of characters or words. It covers how input and output tokens combine, how token counts relate to English, Chinese, punctuation, and code, and why subword tokenization is used. It also presents practical methods for estimating costs, managing context windows, limiting output length, and handling multilingual token-density differences, with a simplified pseudocode example.

    1. What Exactly Is a Token?

    When working with large language models, many developers wonder why billing is based on a unit called a “token” rather than on word count or character count. To understand this, it helps to start with the basic granularity of natural language processing.

    A token is the smallest unit of text that a model processes. The model does not directly “read” Chinese characters or English letters. Instead, it first splits raw text into tokens, then maps each token to a numeric ID before feeding it into the neural network. In other words, the model input is a sequence of tokens, and the output is also a sequence of tokens. The text that humans see is simply the token sequence rendered back at the interface layer.

    Tokens are necessary because models need to handle not only Chinese, but also nearly all major natural languages, along with punctuation, numbers, and code symbols. A simple rule such as “one Chinese character equals one unit” or “one letter equals one unit” would neither unify multilingual processing nor efficiently represent how words are composed. Tokens are designed to solve this problem of unified representation across languages and symbols.

    2. Token Count Has Two Components

    The token consumption of a model call is the sum of two parts:

    • Input tokens: The content you submit to the model, including the system prompt, conversation history, the current question, and all other text.
    • Output tokens: The answer text generated by the model based on the input.

    A simple example makes this clear:

    1. Input (your question) = 100 tokens
    2. Output (model-generated answer) = 200 tokens
    3. ----------------------------------------------
    4. Total for this call = 300 tokens

    This means billing and context usage are “bidirectional.” Many people focus only on the length of their question and overlook the fact that longer model answers also consume more tokens. In multi-turn conversations, previous messages are usually included again in the next request, so cumulative input tokens grow with the number of turns. This is a major reason long conversations become more expensive.

    3. How Tokens Map to Characters

    Tokens and characters are not in a one-to-one relationship. Their conversion depends on the language and the specific tokenization algorithm. Here are common industry rules of thumb:

    • English: One token usually corresponds to about 4 characters, or roughly 0.75 English words. Common complete words are often encoded as one token, while longer or rarer words are split into multiple subwords.
    • Chinese: Because Chinese has high information density, one Chinese character often corresponds to one or more tokens, depending on how that character is encoded in the vocabulary.
    • Punctuation, spaces, and line breaks: These symbols also consume tokens and cannot be ignored.
    • Code: Indentation, brackets, operators, and similar elements are counted separately. Code text generally has a higher token density than natural language.

    As a result, “1,000 Chinese characters” and “1,000 English characters” can differ greatly in token count. When estimating cost, you should not simply convert character counts directly. The most reliable approach is to use an official or common encoding tool to tokenize the actual text and count the tokens.

    4. Why Subword Tokenization Is Used

    Modern large models generally use subword tokenization. A typical approach is based on byte- or character-level merge algorithms, such as byte pair encoding and related methods. Its core logic is:

    1. Start from the most basic characters or bytes and count frequently adjacent combinations in the corpus.
    2. Repeatedly merge high-frequency combinations into new subword units and add them to the vocabulary.
    3. Eventually produce a vocabulary that can cover common whole words while also composing rare words from subwords.

    This approach has three benefits:

    • Controlled vocabulary size: There is no need to create an entry for every complete word. A vocabulary of tens of thousands to more than one hundred thousand entries can cover a huge range of language phenomena.
    • No out-of-vocabulary problem: Even unseen new words, proper nouns, or spelling variants can be represented by combining existing subwords, so there is no “cannot encode” case.
    • Cross-language unification: Byte-based tokenization can bring any language and any symbol into the same encoding system.

    Because tokenization works at the subword level rather than the whole-character level, the same piece of text may produce different token counts under different models and vocabularies.

    5. Estimation and Optimization in Engineering Practice

    Once you understand the token mechanism, you can optimize actual development work in several ways:

    1. Estimate cost
    Before making a call, use a common encoding tool to count the tokens in the input text. Combined with the expected output length, you can estimate the total number of tokens for a single request and then estimate the overall budget for batch tasks.

    2. Control context length
    Models have a context window limit: the total number of input and output tokens cannot exceed that limit. In multi-turn conversations or long-document processing, you should:

    • Summarize and compress conversation history, keeping only key information.
    • Process very long documents in chunks, then summarize the chunk results.
    • Simplify system prompts and remove redundant instructions.

    3. Constrain output length
    Use parameters to limit the maximum number of generated tokens. This avoids extra cost from overly long answers and helps keep the output focused.

    4. Pay attention to multilingual differences
    If your business involves both Chinese and English, estimate them separately. For the same amount of information, Chinese often consumes more tokens than English, and this difference is amplified in batch processing.

    6. A Simplified Estimation Example

    The following pseudocode shows how to estimate the token scale of a request before making a call:

    1. def estimate_tokens(text, lang="zh"):
    2. # Heuristic estimate only; actual counts should come from an encoding tool
    3. if lang == "en":
    4. return len(text) / 4 # English: about 4 characters per token
    5. else:
    6. return len(text) # Chinese: approximately 1 character per token
    7. prompt = "Please help me summarize this technical document..."
    8. input_tokens = estimate_tokens(prompt, lang="zh")
    9. expected_output = 300 # Expected answer length
    10. total = input_tokens + expected_output
    11. print(f"Estimated consumption: about {total} tokens")

    It is important to emphasize that these numbers are only order-of-magnitude estimates. For an accurate result, you must use the encoder paired with the target model to tokenize the text in practice.

    7. Summary

    A token is the smallest granularity at which a large language model processes text. Both model input and output are measured in tokens, and the consumption of a single call equals input tokens plus output tokens. Tokens and characters are not one-to-one: Chinese, English, symbols, and code have different densities, and the underlying mechanism is subword tokenization. By understanding this mechanism, developers can estimate costs more accurately, plan context windows, and optimize prompts and output length in engineering work. Whether you deploy models locally or call model capabilities through a cloud platform, these principles are general and worth understanding first.