Why AI Coding Costs Add Up: Breaking Down Input, Cache, and Output Tokens

    This article explains why a single AI coding task can consume many tokens across several model calls. It shows how to calculate input, cache, and output costs separately, when caching pays off, how long sessions grow more expensive, and how to reduce waste and track spending.

    1. Start with the basics: a call’s fee is not the same as the length of the answer you see

    When you use an AI coding tool, a common question is: why did a few dozen lines of code use up so much of my quota?

    The reason is that completing a coding task usually involves more than one model call. The system may first interpret the request, then search the codebase, read files, analyze dependencies, generate changes, run validation, and finally fix errors based on the results. Each step can create new inputs and outputs.

    So cost analysis needs to separate two levels:

    • Task level: the user asks to fix a bug, add an API endpoint, or complete a refactor.
    • Call level: a single inference request the model runs to complete part of the task.

    One task may involve many calls, and one user interaction does not necessarily map to one call. Counting only the messages the user sends can easily underestimate actual consumption.

    Billing methods also differ across services. Pay-as-you-go usage may be billed on the tokens actually processed, while subscription plans may use quotas, rate limits, or other restrictions. The discussion below covers a general token cost model. It does not reflect the current pricing or billing terms of any specific service.

    2. Build the cost model: calculate three components separately

    A token is the basic unit a model uses to process text. Tokens do not map exactly to Chinese characters, English words, or code characters. For actual accounting, use the usage records returned by the service rather than substituting character counts.

    For convenience, the cost of one call can be split into three parts: input, cache, and output. However, caching is usually a special pricing method for input processing, not a second charge for the same input.

    For one call, define:

    • Regular input volume: U
    • Cache write volume: W
    • Cache read volume: R
    • Billed output volume: O
    • The price per million tokens for each: Pu, Pw, Pr, Po

    Then the cost of the model call is:

    C = (U × Pu + W × Pw + R × Pr + O × Po) ÷ 1,000,000

    The most important requirement is that U, W, and R must follow the billing statement’s definitions. Tokens already counted as cache reads should not be counted again as regular input.

    If a service expresses a cache write as a base input fee plus a surcharge, convert it to the same unit basis before using the formula. Billing fields should not be added together directly without this conversion.

    1. Input: what the model actually receives

    Input usually includes more than the sentence the user just typed. It may also include:

    • System rules and task constraints
    • Conversation history
    • Source code that has been read
    • Search results and tool outputs
    • Test failure logs
    • The current task state and stage summaries

    Do not assume every tool sends the full history with every call. Some systems truncate context, some generate summaries, and some keep only selected relevant files. What determines the cost is the content actually submitted in each request and counted under the billing rules.

    From a cost management perspective, optimizing input does not simply mean saying less. It means reducing repeated, irrelevant, and outdated information while keeping the constraints needed to complete the task.

    2. Cache: whether repeated content is reused at a lower price

    Some inference services cache repeated input prefixes. When the conditions are met, later requests can bill the same content at the cache read price.

    Caches usually have constraints, such as a minimum cacheable length, an expiration time, supported scope, and prefix matching requirements. These conditions can vary by service, model, and version, so do not treat any fixed threshold as a universal rule.

    A cache also does not mean the model remembers the entire project. It does not automatically let later requests omit context, and it does not allow reading history across arbitrary users or sessions. The application still needs to build requests according to the service’s rules and meet the reuse conditions.

    3. Output: not just the amount of text shown

    Output usually includes generated code, explanations, patches, and tool call content. Some services may also bill for reasoning-related tokens that are not directly shown to the user. Whether these are billed and how they are counted should follow the actual rules of the service.

    Therefore, a short final answer does not directly imply low output consumption.

    In engineering practice, output should be limited by cutting repeated explanations, unnecessary code, and unneeded multi-option discussion, not by mechanically compressing all analysis. Overly tight output limits can leave the implementation incomplete, leading to more rework calls and raising the total cost.

    3. Why long sessions get more expensive: usually not exponential growth

    Suppose the input length of the i-th call is:

    Lᵢ = L₀ + (i − 1) × ΔL

    where L₀ is the initial context length and ΔL is the amount of new content added and retained each round.

    If all history keeps accumulating, with no caching and no summarization, the cumulative input for n calls is:

    ΣLᵢ = n × L₀ + ΔL × n × (n − 1) ÷ 2

    Under these assumptions, cumulative input grows quadratically with the number of calls, not exponentially.

    If the context length stays at a stable size, cumulative input grows roughly linearly. If the system also has parallel branches, many retries, or tool results that keep expanding, costs can rise faster. Those cases should be analyzed based on the actual call structure, not all labeled as exponential growth.

    This distinction matters in practice, because the growth mechanism determines the fix:

    • If context keeps expanding, control the length of history first.
    • If there are too many retries, improve the validation and stopping conditions first.
    • If there are too many parallel branches, adjust the scheduling