# Token Pricing in the Agent Era: Why Usage-Based Billing Is Sound and How to Optimize Costs

    AI Agents can consume several times as many tokens as ordinary Q&A, so calculating an LLM bill by simply multiplying total tokens by a unit price leads to serious cost misjudgments. This article explains how modern LLM API billing works: input and output tokens are priced separately, and billing dimensions such as prompt caching, reasoning tokens, tool calls and retrieval may apply. It shows why multi-turn loops, context accumulation, tool stacking and retries amplify agent token usage, and provides a general cost estimation formula with an extended model as pseudocode. It argues that the pricing model itself is sound and that the real gap lies in cost governance. A three-layer optimization approach covering architecture, application and governance is then presented to make AI API spending predictable, attributable and enforceable.}

    1. Introduction: When Tokens Become a Cost Center

    In traditional single-turn Q&A, one request maps to one response, so token usage is relatively predictable. As applications such as AI agents, AI-assisted coding and multi-document analysis mature, one business goal may require a model to perform dozens or even hundreds of inference and API calls. Context keeps accumulating, tools are triggered repeatedly and intermediate results stack up. Tokens are no longer a linear, one-off consumable. Instead, they have become a cost center that needs to be managed carefully.

    A common misconception is that you only need to know how many tokens you use each month. Multiply that figure by the unit price, and you have your bill. In the agent era, this assumption leads to serious cost misjudgments. To estimate costs accurately, you first need to understand the structure of token-based billing.

    2. Why 10 Million Tokens Does Not Equal a Single Price

    2.1 Input and Output Tokens Are Fundamentally Different Billing Objects

    Most major LLM APIs price two types of tokens separately:

    • Input tokens: the content sent to the model, including prompts, system instructions, context, conversation history and retrieved documents.
    • Output tokens: the content the model actually generates, such as answers, code and reasoning.

    The key point is that output tokens usually cost significantly more per token than input tokens. The generation phase runs autoregressive computation token by token, which requires far more compute than encoding the input in a single pass. As a result, the same total token volume does not mean the same cost.

    2.2 Same Volume, Different Mix, Different Cost

    Suppose an application uses a fixed 10 million tokens per month and only the input-to-output ratio differs. The resulting bills can vary substantially:

    Scenario Input tokens Output tokens Profile
    Scenario A 8 million 2 million Input-heavy, e.g., long-document retrieval Q&A
    Scenario B 5 million 5 million Balanced
    Scenario C 2 million 8 million Output-heavy, e.g., code generation or long-form writing

    When output tokens carry a higher unit price, Scenario C often costs significantly more than Scenario A, even though all three use the same total number of tokens. In other words, the first step in estimating cost is not to look at the total volume. It is to establish the input-to-output ratio. For output-heavy applications such as code generation and agent orchestration, the cost structure is fundamentally different from that of conventional Q&A.

    2.3 Commonly Overlooked Line Items

    Beyond basic input and output, modern inference services may include several independently billed dimensions:

    • Cache writes and cache reads: repeated context can be served from a prompt cache, but creating and maintaining that cache may be billed separately.
    • Reasoning tokens: some models generate internal reasoning tokens before producing the final answer, and these may need to be accounted for separately.
    • Tool calls and function execution: the arguments and results of each tool call may each count as tokens.
    • Web search and retrieval augmentation: injecting external search results adds to input tokens.

    If these items are simply folded into the standard input/output price, the cost model becomes systematically distorted.

    3. Why Agents Amplify Token Consumption

    Once you understand the billing structure, it is easier to see why agent costs grow far beyond intuition. The amplification comes mainly from four sources:

    1. Multi-turn loops (ReAct, Plan-Execute): agents often follow a think–act–observe cycle. Each turn resends the full history, so input tokens grow roughly quadratically with the number of turns.
    2. Context accumulation: as the conversation and task progress, more historical information is appended to the context. Input costs in later turns are far higher than in the first one.
    3. Stacked tools and retrieval: every tool call and knowledge retrieval injects new content into the context, increasing input size further.
    4. Retries and branch exploration: self-correction and exploration of multiple solution paths generate extra calls. A model that handles each request in a single pass does not incur this kind of overhead.

    As a result, an agent that appears to give a one-sentence answer in a demo may have used several times as many tokens behind the scenes as an ordinary Q&A request.

    4. A General Framework for Estimating Costs

    4.1 The Basic Formula

    The most direct cost model is:

    1. Monthly API cost = input tokens × input price per token + output tokens × output price per token

    If the provider quotes prices per million tokens, the formula becomes:

    1. Monthly cost = (input tokens / 1,000,000) × input price per million
    2. + (output tokens / 1,000,000) × output price per million

    For an application with 8 million input tokens and 2 million output tokens per month:

    1. Monthly cost = 8 × input price per million + 2 × output price per million

    4.2 Extending the Model with Advanced Billing Items

    When features such as caching, batch processing and long context come into play, you should add the corresponding items to the basic formula. The following illustrative pseudocode shows how:

    1. def estimate_monthly_cost(
    2. input_tokens, # Regular input tokens
    3. output_tokens, # Output tokens
    4. cached_read_tokens, # Tokens served from cache (cache reads)
    5. reasoning_tokens, # Reasoning tokens
    6. price_input, # Input price per million tokens
    7. price_output, # Output price per million tokens
    8. price_cache_read, # Cache read price per million tokens
    9. price_reasoning, # Reasoning price per million tokens
    10. ):
    11. unit = 1_000_000
    12. cost = (
    13. input_tokens / unit * price_input
    14. + output_tokens / unit * price_output
    15. + cached_read_tokens / unit * price_cache_read
    16. + reasoning_tokens / unit * price_reasoning
    17. )
    18. return cost

    In practice, we recommend making the price and parameters of each billing item configurable. This makes it easy to compare costs and simulate budgets across providers and models. Note that actual prices and billing rules should always be verified against the provider’s official documentation. The code above only illustrates a general modeling approach.

    5. Is the Current Pricing Model Really “Unreasonable”?

    Strictly speaking, separate pricing for input and output is reasonable and necessary at the compute-cost level, because it accurately reflects the higher compute cost of the generation phase. The real problem is not the billing principle. It is that users’ understanding and management of cost have fallen behind the pace of agent technology:

    • Unpredictable cost: the number of agent loops and tool calls is uncertain, which makes it hard to estimate the cost of a single task.
    • Difficult attribution: rough calculations based only on total volume times unit price cannot show which stage is driving cost.
    • Lack of real-time guardrails: many applications do not cap tokens per task or per user, so abnormal loops can quickly lead to runaway spending.

    The goal, therefore, is not to replace the billing model. It is to close the gap in cost governance so that spending becomes observable, attributable and enforceable.

    6. A Three-Layer Approach to Cost Optimization

    6.1 Architecture Layer: Eliminate Wasteful Token Usage

    • Context compression and summarization: use rolling summaries of the conversation history so that the full history does not have to be resent every turn.
    • Cache reuse: cache stable content such as system prompts and long documents. Reading from the cache can then replace repeated regular input.
    • Task splitting and routing: send simple requests to a lightweight model and escalate only complex tasks to a more capable one. This avoids overkill, such as using a sledgehammer to crack a nut.

    6.2 Application Layer: Control the Pace of Consumption

    • Limit loop iterations: set a maximum number of iterations and a timeout for each agent to prevent endless retries.
    • Trim tool outputs: crop and structure tool results so that large blocks of redundant content do not enter the context.
    • Retrieve on demand: trigger retrieval only when necessary, and filter results for relevance.

    6.3 Governance Layer: Make Cost Visible and Controllable

    • Per-item metering: track input, output, cache and reasoning tokens separately instead of looking only at the total.
    • Cost attribution: tag usage by business, user and session to identify the biggest sources of consumption.
    • Budgets and alerts: use monitoring and alerting to set thresholds for abnormal usage. Combine these with logging and distributed tracing for investigation and postmortems.

    7. Conclusion

    Agents have not broken the fundamental logic of token-based billing, but they have transformed the scale and structure of cost. Developers should stop complaining that “tokens keep getting consumed” and instead build an understanding of three things: the rationale behind separate input/output pricing, the mechanisms that amplify agent token usage, and a governance framework that is attributable and enforceable. When costs can be calculated precisely, attributed step by step and constrained in real time, token billing stops being a black-box bill and becomes a manageable engineering metric. For organizations building AI applications on platforms such as Baidu AI Cloud, setting up this cost governance system early will give them more control when it comes to large-scale deployment.