How to Get More Done With Fewer Tokens in AI Coding

    A practical guide to reducing token costs in AI coding tools. Learn how input, cache creation, cache read, and output tokens differ in price, why cache reads dominate long sessions, and ten actionable strategies—context budgeting, retrieval over full-file reads, small edits, constrained tool output, and cache reuse—to keep context lean and costs under control.

    1. Opening: A Bill That Made Me Uncomfortable

    Last week I used an AI coding tool to push through an engineering task that had been iterating all day. When I closed the terminal and opened the usage panel, the number stopped me cold: a single session had consumed roughly 9.7 million tokens—9,721,744 to be exact.

    My first reaction was panic. But after pulling up a detailed usage report, I realized this was not simply about “burning money.” The distribution of those 9.7 million tokens was extremely uneven:

    Type Count Share
    Input (direct input) 29,710 0.03%
    Cache creation 790,452 8.1%
    Cache read 8,913,330 91.7%
    Model output 14,991 0.15%

    Cache reads accounted for 91.7%. Under the prompt caching billing model widely used across the industry, cache reads are typically priced at only about one-tenth of direct input. In other words, despite the apparent “9.7 million tokens burned,” the actual billed cost was a fraction of that headline number.

    But that is only half the story. The other half is where the real warning lies: the 8.1% cache creation, the 0.03% direct input, and why the context expanded to the point of being cached repeatedly—these are the root causes of runaway costs.

    2. Four Identities of a Token, Four Very Different Costs

    To optimize tokens, you first need to understand that tokens are not a homogeneous commodity in the billing system. Mainstream large model APIs generally divide tokens into four categories:

    1. Input tokens: The context actually sent to the model on each request. Standard pricing, and the most expensive.
    2. Cache creation tokens: When the context exceeds the platform’s minimum cacheable length (commonly on the order of 1024 tokens), the system can write the prefix into cache. The write itself is billed, usually at around 1.25 times the input price.
    3. Cache read tokens: Subsequent requests that hit the cached prefix are billed at only about 1/10 of the input price. This is the underlying reason “longer sessions are more cost-effective.”
    4. Output tokens: Content generated by the model, usually 3–5 times the input price; but in coding scenarios, output often makes up a very small share.

    Core rule: The higher the share of cache reads, the lower the cost per unit of work. Frequent cache invalidation and repeated context changes will rapidly worsen the bill.

    3. What the Real Data Reveals: What Is Inflating the Context

    Reviewing every “cache creation peak” in that session, the sources of expansion concentrated in four categories:

    • Full-repository re-reads: A single task pulled dozens of files into context in full, most of which were unrelated to the current change.
    • Unrestrained tool output: Directory trees, dependency lists, and complete logs were dumped in verbatim, with a single output often running into tens of thousands of tokens.
    • Repeated trial and error: The same problem went through multiple rounds of “guess—execute—fail—guess again,” each round carrying the full history.
    • Cache expiration: When the idle interval exceeded the cache lifetime (commonly a minute-level TTL), the next request was forced to rebuild the cache. The “23% of quota vanishing in an instant” mentioned in the original text was essentially the result of a long interval causing caches to collectively expire and all convert to cache creation.

    4. Ten Actionable Token Optimization Strategies

    Strategy 1: Classify First, Then Optimize

    Use session logs and usage statistics to categorize tokens along three dimensions—task / tool calls / context re-reads—and identify the two largest categories before taking action. No measurement, no optimization.

    Strategy 2: Give the Context a Budget

    Set an upper limit on context for each request (for example, no more than 40% of the window), and include only three types of content: the task description, directly relevant files, and acceptance criteria. Everything else stays out of context.

    Strategy 3: Retrieval First, Not Full-Repository Re-reads

    Locate with retrieval first, then read the exact fragments. A sample flow:

    1. Search keywords -> matching files and line numbers -> read only matched fragments (±30 lines) -> start editing

    Strategy 4: Small Edits, Fast Verification

    Break a large requirement into small, independently verifiable changes, each carrying only the necessary context. Small steps not only save tokens but also surface errors earlier.

    Strategy 5: Constrain Tool Output

    Limit the number of returned lines, take only error summaries, and aggregate logs rather than returning them in full. Tool output is the most common hidden source of context expansion.

    Strategy 6: Reuse Project Notes and Cache

    Write stable project conventions into a persistent notes file so it stays steadily in the context prefix and hits the cache over the long term; avoid re-explaining the project background in every session.

    Strategy 7: Keep the Prefix Stable to Extend Cache Lifetime

    Caching matches by prefix. Move volatile content (timestamps, random IDs, temporary paths) out of the prefix to avoid invalidating the cache on every request round.

    Strategy 8: Split Requirements by Priority and Verify One by One

    Order the requirements and close them out one at a time, avoiding “doing everything at once,” which leads to linear context growth and repeated rollbacks.

    Strategy 9: Define Acceptance Criteria Clearly

    State the “definition of done” in the task description to reduce back-and-forth confirmation and ineffective trial-and-error rounds.

    Strategy 10: Divide and Conquer Sessions, Close Out Regularly

    Split long tasks into sessions by milestone, and produce a short conclusion file before each session ends; the next session reads only the conclusion instead of replaying the history.

    5. Turning the Strategies into a Workflow

    The recommended standard workflow is: measure → budget → retrieve → small edits → constrain output → reuse cache → verify item by item. Measurement and cache stability are the most easily overlooked, yet they determine the shape of the overall cost curve.

    6. Conclusion

    Token consumption in AI coding tools is essentially a context management problem. By understanding the cost differences among the four token types and combining budgeting, retrieval, small steps, constraints, and cache reuse, you can turn a “surface-level ten million tokens” into “controllable actual cost” in real engineering—solving enough problems with fewer tokens.