Practical Guide to Reducing Token Costs in AI Coding Tools

    This article analyzes the structure of token consumption in AI coding assistants, explaining the four token pricing categories and their impact on billing. It provides 10 actionable strategies to optimize token usage by maximizing cache hits, focusing context, and controlling session flow, helping developers minimize costs while maintaining coding efficiency.

    Burning 9.7 Million Tokens in a Single Session: What Happened?

    Last week, I used an AI coding tool to iterate on an engineering project continuously for a whole day. When I closed the terminal and pulled up the usage report, I was stunned: a single session had consumed approximately 9.7 million tokens.

    My first reaction was panic, but upon opening the detailed statistics, I realized things were not that simple. The distribution of these nearly 10 million tokens was highly uneven:

    Type Quantity (Approx.) Percentage
    Input (Direct Input) 29,700 0.03%
    Cache Write 790,000 8.1%
    Cache Read 8,913,000 91.7%
    Output (Model Generated) 15,000 0.15%

    Over 90% of the consumption was “Cache Read.” The unit price for cache reads is typically only about one-tenth of direct input. In other words, the “9.7 million tokens” on the surface only cost a fraction of what the number suggests. What truly determines the bill is never the absolute number of tokens, but their structure.

    Understanding the Four Token Categories

    Mainstream large language model platforms generally divide tokens into four categories for billing purposes, with vastly different prices (the following ratios are approximate representations of common industry mechanisms; refer to specific platform rules for exact details):

    1. Input Tokens: The context actually sent to the model with each request. Standard pricing, most expensive.
    2. Cache Write Tokens: When the context exceeds a certain threshold (commonly 1024 tokens), the platform caches it on the server side. This write operation incurs an extra fee, about 1.25 times the input price—more expensive than input because it occupies storage.
    3. Cache Read Tokens: Subsequent requests reuse the cached content, costing only about 1/10 of the input price. This is the underlying reason why “longer sessions are more cost-effective.”
    4. Output Tokens: The content generated by the model, usually 3–5 times the input price. However, in coding scenarios, the output ratio is often extremely low.

    Core principle: The more cache reads, the better the value for your money. The essence of optimizing tokens is not about using less, but making requests hit the cache as much as possible, reducing expensive input and write operations.

    What is Secretly Inflating the Context?

    Looking back at every “cache write peak” in that session, the sources of inflation were concentrated in three areas:

    • Blindly re-reading the entire repository: Stuffing the entire codebase into the context before every change. The cache is established only to be immediately displaced by new content.
    • Repeated trial and error: Modifying the same piece of code back and forth, carrying the full history each time, causing the input volume to snowball.
    • Unlimited tool outputs: Logs, search results, and command echoes easily reach thousands of lines, all being fed into the context in full.

    10 Actionable Strategies to Save Tokens

    1. Measure Before Optimizing

    Use session logs and usage statistics to categorize tokens by three dimensions: “task,” “tool invocation,” and “context re-read.” You cannot optimize what you do not measure. Identify the category with the highest proportion before taking action.

    2. Provide Only Necessary Context

    Clearly identify the files and fragments truly relevant to the current task, rather than the entire project. The more focused the context, the higher the cache hit rate and the lower the input cost.

    3. Retrieve First, Then Edit in Small Steps

    Use retrieval to locate the specific position first, then make small-scale modifications. Avoid the cycle of “read entire repo -> make large changes -> read entire repo again,” which is the primary cause of input token inflation.

    4. Constrain Tool Outputs

    Set line or range limits for logs, searches, and command echoes. For example:

    1. # Look at key errors instead of full logs
    2. grep -iE "error|exception" app.log | tail -n 50

    5. Reuse Project Instructions and Cache

    Write stable project conventions into a long-term instruction file so that it is cached and reused across multiple requests, rather than being re-entered every time.

    6. Split Requirements by Priority

    Break down large requirements into independently verifiable small items, completing and verifying them one by one. Smaller tasks have smaller contexts and more stable caches.

    7. Define Clear Acceptance Criteria

    Write down the “definition of done” in your prompts to reduce back-and-forth confirmations and rework, compressing repeated inputs caused by trial and error at the source.

    8. Avoid Unnecessary Cache Refreshes

    Place stable prefixes (instructions, conventions) at the front and volatile content at the back. If the prefix changes, the entire cache is invalidated—once, about 23% of the quota evaporated instantly due to cache expiration, exactly for this reason.

    9. Control Session Length and Pace

    Overly long sessions cause history to continuously accumulate. Wrapping up phases and starting new sessions is more economical than letting the context inflate infinitely.

    10. Build a Review Habit

    After each high-consumption session, review the category proportions and consolidate effective practices into team standards, forming a continuous improvement loop.

    Conclusion

    The key to saving tokens is not being stingy, but structuring your compute usage: let the cache hit more, let the input be more focused, let the output be more restrained, and let requirements be broken down for verification. By mastering the four identities of tokens and their cost rules, combined with the strategies above, you can use AI coding tools to solve sufficient problems with the least overhead.