Token Cost Optimization Across Languages and Structured Outputs

    Why does the same content cost more tokens in some languages than others? This article explains how tokenizers create a "non-English tax," how to measure it scientifically, and how structured formats like JSON and HTML silently inflate token usage — plus practical engineering strategies to reduce consumption.

    1. The Starting Point: Tokens Are Not Characters

    When a large language model processes text, it does not read characters or words directly. Instead, a tokenizer splits the text into tokens. Tokenizers are typically built on subword algorithms: high-frequency segments become standalone tokens, while low-frequency content is broken into smaller pieces. As a result, semantically identical content produces different token counts in different languages.

    English dominates general-purpose training corpora, so common words and roots are usually already in the vocabulary — a word often maps to one or just a few tokens. Chinese has a large character set with many combinations, and the vocabulary cannot cover every character. Common words may be a single token, but rare characters or proper nouns may consume several. Languages written in Devanagari such as Hindi, with complex glyph structures and rich inflection, tokenize even less efficiently.

    This extra overhead is often called the “non-English tax.” It mainly comes from the mismatch between tokenization behavior and billing units, not from any inherent complexity of the language itself.

    2. Key Factors That Affect Token Consumption

    1. Vocabulary coverage: The lower a language’s share of the training corpus, the fewer complete entries it has in the vocabulary, and the more segmentation steps are required.
    2. Character encoding: Non-Latin characters typically take more bytes in UTF-8. In byte-level tokenization, uncovered characters get split into multiple byte-level tokens, increasing consumption.
    3. Morphology and writing systems: Inflected and agglutinative languages have many word forms; each variant may require its own vocabulary entry.
    4. Mixed-language boundaries: Mixing Chinese and English increases segmentation points. English identifiers such as paths, function names, and variable names usually tokenize efficiently, but boundaries adjacent to Chinese text are prone to fragmentation.
    5. Model and version differences: Token counts for the same language can vary significantly across models and versions. Results from one model should not be generalized to others.

    3. How to Measure the Language Tax Scientifically

    Whether measurements are comparable depends on methodology. Follow these principles:

    • Use parallel corpora: Translate the same semantic content into multiple languages so content differences don’t skew results.
    • Use a consistent counting method: When comparing models, state each model’s counting rules, or use the token-counting capability each model provides.
    • Report relative values: Use English as the baseline, compute the ratio of each language’s token count to English, and also report tokens per character or per word.
    • Note dates and versions: Tokenizers change with model versions, so conclusions should include the test date and version information.

    One cross-language benchmark found that Chinese text consumed roughly 60% more tokens than English, and Hindi reached about 3x English on some models. These figures come from specific corpora and specific models — they indicate magnitude only and cannot be applied directly to every scenario.

    Here is a general measurement sketch, where the counting function is provided by the caller:

    1. def language_tax(tokens_target: int, tokens_baseline: int) -> float:
    2. """Return the token increase of the target language relative to the baseline."""
    3. if tokens_baseline == 0:
    4. raise ValueError("Baseline token count must not be 0")
    5. return tokens_target / tokens_baseline - 1
    6. def measure_parallel_corpus(samples: dict, count_tokens) -> dict:
    7. """
    8. samples: {language_code: text with the same meaning}, must include "en" as the baseline
    9. count_tokens: counting function provided by the caller; all languages must use the same method
    10. """
    11. baseline = count_tokens(samples["en"])
    12. report = {}
    13. for lang, text in samples.items():
    14. n = count_tokens(text)
    15. report[lang] = {
    16. "tokens": n,
    17. "tax": round(language_tax(n, baseline), 3),
    18. }
    19. return report

    4. Practical Impact of Mixed Chinese-English Writing

    Many developers write instructions in Chinese and code, file paths, and identifiers in English. This hybrid approach is reasonable in most scenarios, but you need to understand its cost structure.

    • The instruction portion is the main source of the language tax. The longer and more repetitive the Chinese instructions, the higher the extra cost. Compress repeated background explanations into key points first.
    • English identifiers tokenize efficiently. Keep paths, function names, variable names, and command arguments as-is — rewriting them breaks consistency with the codebase and costs more than it saves.
    • Validate language switches with quality metrics. If you want to rewrite key rules in English to save tokens, compare output quality on real tasks rather than relying on intuition.

    5. Structured Output: An Easily Overlooked Token Amplifier

    In programming scenarios, structured formats like JSON and HTML often consume more tokens than natural language. These formats contain many high-frequency symbols that carry no semantics — quotes, brackets, commas, colons, indentation, and escape characters.

    For example, the following two JSON snippets are semantically identical:

    1. {"name": "server-01", "status": "running", "cpu": 0.42}
    1. {
    2. "name": "server-01",
    3. "status": "running",
    4. "cpu": 0.42
    5. }

    The extra newlines and spaces in the indented version also count as tokens. The problem is more pronounced with arrays, because every object repeats the keys and structural symbols:

    1. [
    2. {"id": 1, "name": "a", "enabled": true},
    3. {"id": 2, "name": "b", "enabled": false},
    4. {"id": 3, "name": "c", "enabled": true}
    5. ]

    Here, the keys id, name, and enabled each appear three times. Switching to a columnar layout — a single key list followed by rows of values — significantly reduces this repetition overhead.

    HTML is similar. Closing tags, class and style attributes, nested indentation, and comments all consume substantial tokens. If a task only needs a paragraph of text or a few fields but the model outputs an entire page, a large amount of waste results.

    For structured output, consider these practices:

    • Request only the necessary fields, and provide precise field definitions in the prompt.
    • Prefer columnar or delimiter-based structures for arrays so keys appear only once.
    • When modifying code, output the changed snippet or specified line ranges instead of the whole file.
    • When generating HTML, remove irrelevant inline styles and comments, keeping only the structure the task requires.

    6. Engineering Strategies to Reduce Token Consumption

    1. Streamline prompts: Remove repeated rules, compress background information into bullet points, and avoid injecting the same instructions into every request.
    2. Control context: Inject only code snippets relevant to the current task. When conversation history grows too long, periodically generate summaries to replace raw records.
    3. Constrain output length: Limit output with explicit format requirements and length specifications to prevent the model from expanding on its own.
    4. Monitor usage: Log input and output tokens for each request, aggregate by functional module, and identify the highest-cost paths.
    5. Set budget thresholds: When usage exceeds a threshold, prompt users to compress context or switch to a more compact output mode.

    Budget checking can be a simple function. The limit below is only an example; actual values should be set according to your business scenario:

    ```python
    def check_budget(input_tokens: int, output_tokens: int, limit: int = 4000):
    total = input_tokens + output_tokens
    if total > limit:
    return (
    f”Request usage {total} exceeds the limit {limit}; “
    “consider compressing context or constraining the output format”
    )
    return None