AI Coding: How to Solve More Problems with Fewer Tokens

    This article explains why large token totals in AI coding sessions do not always mean high cost. It breaks down input, cache creation, cache read, and output pricing, and offers practical strategies such as context layering, incremental updates, concise outputs, task isolation, and cache lifecycle management to reduce actual token expenses.

    A coding session that runs all day can consume millions of tokens. When you close the terminal and check usage, seeing nearly ten million tokens can trigger immediate panic. But when you look at detailed usage data, the picture is often more nuanced: the absolute number of tokens is not the same as actual cost, and it does not automatically mean waste.

    To optimize token usage in AI coding, developers need to understand how token costs are structured.

    1. The Four Types of Tokens

    When you call a large language model API, every character you send and receive is converted into tokens. For billing purposes, tokens are usually divided into four types with very different prices:

    • Input: The context actually sent to the model in each request. It is billed at the standard rate and is the most expensive basic cost among the four.
    • Cache Creation: When the context exceeds a certain threshold, usually around a thousand or more tokens, the platform writes it to a server-side cache. This write operation is billed, usually at a slightly higher rate than input because it includes storage overhead.
    • Cache Read: When a later request reuses cached content, it only pays about one-tenth of the input rate. This is the underlying reason why longer sessions can sometimes become more cost-efficient.
    • Output: The content generated by the model. It is usually priced at several times the input rate, but in AI coding scenarios, output often accounts for a relatively small share.

    Understanding the relationship among these four types is the foundation for all later optimization decisions.

    2. Why “Nearly 10 Million Tokens” Is Not Always as Bad as It Looks

    Return to the session that consumed nearly ten million tokens. If we break it down by type, most of the tokens are likely cache reads. Since cache reads are billed at about one-tenth of the input rate, a session that looks shocking in raw token count may have an actual billing cost that is only a small fraction of the surface number.

    The core rule is simple: in long sessions, the higher the proportion of cache reads, the lower the unit cost and the better the value.

    Therefore, evaluating an AI coding session should not depend only on total token count. You also need to look at the distribution of token types—especially the cache read ratio.

    3. The Real Risk: Cache Invalidation and Rebuilding

    There is another side to the story, and it deserves more attention. When you check usage repeatedly during a session, you may notice spikes in cache creation. What do these spikes mean?

    Caching is based on prefix matching of context. Once a segment of context is cached, later requests can hit the cache as long as the beginning of the request remains unchanged. However, if you modify earlier context—even if you only change an early file or adjust a system prompt—everything after that modification point may cause the cache to be invalidated. The platform then has to write the cache again, so cache creation spikes and the higher-cost write operation appears.

    In other words, frequently rewriting earlier history is the main source of runaway cost, not the total token count itself.

    4. Core Strategies for Solving More Problems with Fewer Tokens

    4.1 Layer the Context and Put Stable Content First

    Put content that changes less at the beginning of the context: system prompts, coding conventions, long-term configuration files, README files, and other stable project information. Put frequently changing code snippets and the latest instructions later.

    When the front part is stable, the cache hit rate is higher. Changes in the back part then affect only local content.

    A schematic context structure looks like this:

    1. [Stable area: keep unchanged as much as possible]
    2. System prompts / coding standards / project structure
    3. Fixed dependency list / long-term configuration files
    4. --------------------------------
    5. [Dynamic area: append as needed]
    6. Current task description / code snippets to modify
    7. Brief conclusions from the previous step

    4.2 Append Incrementally Instead of Rewriting Repeatedly

    Try to move the conversation forward by appending new information rather than going back and modifying earlier content that has already been cached. When you need to correct something, use a new instruction to clarify the correction instead of rewriting the entire history. This helps preserve the validity of prefix caching as much as possible.

    4.3 Keep Outputs Concise

    Although output usually accounts for a relatively small share of tokens, asking the model to be direct and avoid unnecessary text can reduce output tokens. It can also keep subsequent context smaller when it is referenced repeatedly.

    You can explicitly request “return only necessary code and key explanations” in your instructions.

    4.4 Manage Sessions and Context

    • Isolate tasks: Separate independent large tasks into different sessions. This prevents unrelated context from occupying the window and lowering cache efficiency.
    • Clean up regularly: Remove expired error logs, temporary files, and ineffective attempts promptly to keep the context compact.
    • Use subtasks effectively: Let the model complete exploratory work in an independent sub-context, then merge the conclusions back into the main context. This prevents intermediate processes from polluting the main context.

    4.5 Pay Attention to the Cache Lifecycle

    Caches usually have a lifetime. If you stay idle for a long time and then continue, the cache may have expired, and the first request may trigger a new write. Understanding this can help you plan session timing better and avoid hidden costs caused by a sudden cache creation spike after a long pause.

    5. A Practical Evaluation Framework

    Optimization should not rely on intuition. It is useful to quantify the health of each session with a few metrics:

    1. Cache read ratio: A higher ratio means more reuse and usually lower cost.
    2. Frequency of cache creation spikes: More frequent spikes indicate unstable context structure, and you should review how context is organized.
    3. Cost per effective output: Measure “effective progress divided by actual billing cost” instead of being misled by total token count.

    If you track these metrics in daily work, you will soon be able to tell which habits help you save money and which ones quietly waste it.

    Conclusion

    The absolute number of tokens is never the real measure of cost. The structure of tokens is what matters. By understanding the price differences among the four token types, recognizing where cache hits and cache invalidations occur, and using simple but effective methods such as layering, appending, simplifying, and isolating, you can solve many problems in a long AI coding session at the lowest practical cost.

    This is not magic. It is measurable and optimizable engineering practice.