Why LLM Token Usage Varies So Much

    Token consumption for the same coding task can differ by several times across large language models. This article explains the three main causes—repeated context billing, hidden reasoning overhead, and tokenizer granularity—and presents practical cost controls such as context governance, task-based model routing, output constraints, and observability with budget guardrails. It also outlines how to build a cost-controlled invocation pipeline on the cloud using Baidu AI Cloud capabilities.

    1. The Problem: Same Task, Very Different Bills

    When developers connect different large language models to AI coding tools, they often run into a counterintuitive result: the same code generation or refactoring task consumes far more tokens than expected on some models, and the bill grows faster than anticipated. This is not simply because a model is “more verbose.” It is a systemic outcome of how context is assembled, the overhead of the reasoning stage, and the invocation strategy. Understanding these causes is the prerequisite for cost governance.

    2. Three Root Causes of Token Usage Differences

    2.1 Repeated Billing of the Context Window

    Mainstream LLM APIs are typically stateless: every request must resend the system prompt, the conversation history, and the current code snippet. When a tool uses a “concatenate everything” strategy, input tokens grow roughly quadratically as the conversation gets longer.

    Illustrative pseudocode:

    1. messages = [system_prompt]
    2. for turn in history: # the longer the history, the more repeated input
    3. messages.append(turn)
    4. messages.append(current_task)
    5. resp = client.chat(messages) # every turn pays for the old context again

    2.2 Hidden Overhead from the Reasoning Mode

    Models with extended chain-of-thought capabilities generate a large amount of intermediate reasoning content before producing the final output. Under many billing schemes, this content is also counted as tokens. For lightweight tasks such as “rename a variable,” deep reasoning adds very little marginal value but brings a definite increase in cost.

    2.3 Tokenization Granularity and Language Fit

    Different models use tokenizers that split Chinese text, comments, and symbol-dense code at different granularities. The same content with the same meaning can produce noticeably different token counts. Heavy indentation, parentheses, and English identifiers in code amplify this difference.

    3. Practical Cost Optimization

    3.1 Context Governance: From Full Context to On-Demand Context

    • Use a sliding window that keeps only the turns relevant to the current task.
    • Retrieve relevant content first, and inject only the files and snippets that are truly needed.
    • Summarize long conversations and replace the original long text with short summaries.

    3.2 Task Tiering and Model Routing

    Group tasks by complexity: route formatting and completion tasks to lightweight models, and architecture design and complex debugging to strong reasoning models. Use a routing layer to dispatch them uniformly and avoid using a heavyweight model for a trivial job.

    1. if task.complexity == "low":
    2. model = lightweight
    3. else:
    4. model = reasoning_capable

    3.3 Parameter and Output Constraints

    Set a reasonable maximum output length, and explicitly require “return only the changed snippet, not the whole file” to reduce useless output. Lower randomness parameters for deterministic tasks to reduce the repeated consumption caused by repeated retries.

    3.4 Observability and Budget Guardrails

    Connect logging services and monitoring alerts, track the distribution of input and output tokens by project and by session, and set budget thresholds and circuit-breaker policies. Also make good use of caching to reuse results for identical or similar requests.

    4. Building a Cost-Controlled Invocation Pipeline on the Cloud

    If you use Baidu AI Cloud capabilities, you can combine model invocation with general cloud products into a complete pipeline: use a container platform to host routing and caching services, use object storage to archive request and result data, use a message queue to smooth out traffic peaks and valleys, and use logging services and monitoring alerts to close the cost loop. This architecture does not depend on any single proprietary interface, making it easy to replace and migrate.

    5. Conclusion

    The essence of token usage differences is the combined effect of context strategy, reasoning depth, and tokenization mechanisms. Developers should treat cost as an engineering metric that can be designed. Through four main lines—context governance, task tiering, parameter constraints, and an observability system—they can achieve a stable balance between quality and cost.