Ten Practical Strategies to Save Tokens with AI Coding Tools

    A full-day coding session can look catastrophic on a raw token counter while most of the volume is cheap cache reads. This article explains the four token categories behind usage-based billing, what actually inflates context, and ten practical ways to cut cost through tighter tasks, stable prefixes, fewer round trips, and visible cache metrics.

    1. A bill that looks worse than it is

    After a full day of iterative work on one project, I closed the terminal and checked usage. The screen showed a seven-digit figure: about 9.72 million tokens, all in a single session.

    The first reaction was panic. The second was doubt about how the number was counted. A deeper usage breakdown reversed the conclusion. Those 9.72 million tokens were extremely uneven:

    Type Approximate volume Share
    Input (sent directly) 29,700 0.03%
    Cache write (cache creation) 790,500 8.1%
    Cache read (cache hits) 8,913,300 91.7%
    Output (model generation) 15,000 0.15%

    More than nine-tenths was cache reads. In common usage-based pricing, a cache read is usually only a small fraction of the direct-input price (often on the order of one-tenth), while a cache write is slightly more expensive than direct input (often around 1.25x, because storage has a cost). Saying the session “burned 9.72 million tokens” is therefore misleading at the invoice level. Real cost is far below the headline number.

    That is only half the story. The other half is what beginners actually need to watch.

    2. Four different prices for “a token”

    On metered model APIs, a token is not one uniform product. It is four resources with very different prices. Every optimization starts from that distinction.

    1. Input tokens: the context actually sent with this request. Standard pricing, and the expensive baseline.
    2. Cache-write tokens: major providers offer prompt caching. Once context passes a minimum length (often on the order of a thousand tokens; the exact threshold varies by provider), the prefix can be written to a server-side cache. That write is billed separately, and the unit price is higher than input.
    3. Cache-read tokens: when a later request hits the cache, you pay only a very small fraction of the input price. This is why a longer session can become cheaper per turn.
    4. Output tokens: text the model actually generates. The unit price is often several times the input price, but in coding-agent work the share is tiny (0.15% in this example).

    The first rule follows directly: the higher the share of cache reads, the lower the cost per problem. Saving tokens is not mainly “talking less.” It is making what you already said cheap to reuse.

    A second implication matters just as much. Output is relatively cheap in volume, input is expensive, and cache writes are the most expensive. Optimization should focus on how the input is structured, not on endlessly shortening the model’s explanations.

    3. What actually inflates the context

    Reviewing each cache-write spike shows the same handful of causes. Almost every beginner hits them:

    • Reading an entire file: to change one function, a module of a thousand lines is dropped into context.
    • Reading the same file again: the same file is loaded repeatedly in one session, and each copy is stacked into the prefix.
    • Feeding full build and test logs back in: one full build can be tens of thousands of tokens, about 95% of it noise.
    • Letting chat history accumulate: after dozens of turns, failed attempts and rejected designs are still sitting in context in full.
    • Dumping a whole directory: the entire tree is handed to the model “so it can look around.” Hit rate is low and cost is high.

    4. Ten practical ways to spend fewer tokens

    Group A: Context budget and structure (this sets the cost ceiling)

    1. Narrow the task. Solve one problem at a time. Context size tracks how vague the request is. Change “optimize this project’s performance” into “reduce the nested loops in function B of module A from three levels to two, and keep the interface the same.” The context you need can drop by an order of magnitude. The more specific the task, the less exploratory reading the model has to do.

    2. Retrieve instead of dumping. Ask the tool for a summary, a symbol index, or a pointer to the relevant span first. Then read only the smallest range the current subtask needs. A useful rule: one read should not exceed the smallest contiguous region required for that subtask.

    3. Compress history with a rolling summary. In a long session, periodically replace raw history with a structured summary: goal, confirmed facts, rejected options, and open work. A sketch of the procedure:

    1. Every N turns, or when context use crosses a threshold:
    2. summary = structured digest(goal / constraints / files already changed / open issues / options explicitly ruled out)
    3. context = system rules + summary + the last K turns of raw text

    4. Close a phase, then start a new session. When a feature loop is done, write a short handoff and reset the session. Carry work across sessions in documents, not in an endlessly growing transcript.

    Group B: Cache-friendly design (this sets the unit price)

    5. Keep a stable prefix. Caches match on the prefix. Put stable material first: role, coding rules, directory conventions, and long-lived constraints. Put volatile material last: the current task, timestamps, random IDs, and temporary logs. Any dynamic text written early in the prefix invalidates everything after it.

    6. Do not keep rewriting the early context. Editing the system rules mid-session, or inserting a “one more note” at the top, is equivalent to paying for the whole prefix again. Append new constraints at the end.

    7. Respect cache lifetime and call rhythm. Caches live for a limited window, often measured in minutes. If you stop to think, look something up, or step into a meeting, the cache is often gone. The next request then triggers an expensive full write. The fix is simple: after a break longer than the window, send a small request to warm the cache before real work.

    Group C: Output and interaction (this sets the number of round trips)

    8. Constrain the output shape. Ask explicitly for the changed span and a minimal patch. Do not ask the model to restate the whole file or explain unchanged code. Output tokens are pricey per unit but small in share. The real gain is fewer round trips, because every turn bills the input again.

    9. Batch requests. Merge “read the file, change A, read again, change B” into one instruction that already contains the full constraints. Round trips are the main multiplier on a beginner’s bill.

    10. Push deterministic work into scripts. Formatting, bulk renames, dependency ordering, regex replacement, and log filtering have definite answers. Give those to a script or a built-in tool. Leave the model the parts that need judgment. This is the highest-leverage habit, and the one most often skipped.

    5. Case review: an expired cache burned about 23% of the quota at once

    Midway through that session I stopped to read an external spec. The gap was about forty minutes. When work resumed, the bill showed one abnormally large cache write.

    The prefix had already grown to hundreds of thousands of tokens, and the cache window had expired. The first request after the break rewrote the entire prefix. That single write was about 23% of the session’s total cost.

    Three lessons that transfer:

    • The cost of interrupting a long session is higher than it feels. Summarize and shrink the context before you stop.
    • On return, warm the cache with a minimal request, then start the real task.
    • Treat context size and cache-write spikes as first-class metrics, not only the total token count.

    6. Measurement: make the cost visible

    Optimization by feel does not work. At least three layers of visibility help:

    1. Split the stats. Count input, cache writes, cache reads, and output separately, and compute the cache hit rate.
    2. Alert on spikes. Set a threshold for a single cache write and alert when it is crossed. Generic cloud logging and monitoring can do this; where those platform features are available, including on Baidu AI Cloud, the same pattern can be reused rather than built from scratch.
    3. Make it a team rule. Put the stable-prefix template, the summary handoff format, and the per-read size limit into team conventions so the practice is engineering policy, not a personal trick.

    7. Common mistakes

    • Mistake 1: treating total tokens as cost. The mix of categories is the cost.
    • Mistake 2: only telling the model to “say less.” Output is a tiny share, so the savings are limited.
    • Mistake 3: treating a long session as an asset. Past a certain length it is a liability.
    • Mistake 4: switching to a cheaper model and stopping there. A badly structured context multiplies any unit price.

    8. A checklist you can run now

    Do four things first: write a clear task boundary; put stable rules at the front and freeze them; minimize each read; summarize before you stop. Then add three more: warm the cache, batch requests, and script deterministic work. Finally, add split statistics and spike alerts.

    The point of saving tokens is not stinginess. It is sending high information density on every request and reusing each stretch of context cheaply. When cache reads stay above about ninety percent, solving enough problems and spending as little as possible are not in conflict.