How to Solve More Problems with Fewer Tokens in AI Coding Assistants
Token usage in AI coding assistants can look alarming, but the real cost depends on how tokens are distributed among direct input, cache creation, cache reads, and model output. This article explains the economics of each token type and presents ten practical strategies for improving cache reuse, controlling context size, reducing unnecessary requests, and lowering costs without sacrificing development efficiency.
How to Solve More Problems with Fewer Tokens in AI Coding Assistants
1. A Real-World Example: The Truth Behind a Large Token Count
A developer recently used an AI coding tool to work on a project that evolved continuously throughout the day. At the end of the session, the usage dashboard showed more than 9.7 million tokens.
The initial reaction was panic. However, a closer look at the breakdown revealed that the usage was highly uneven:
| Type | Amount | Share |
|---|---|---|
| Direct input | Approximately 3,000 | 0.03% |
| Cache creation | Approximately 790,000 | 8.1% |
| Cache reads | Approximately 8.91 million | 91.7% |
| Model output | Approximately 15,000 | 0.15% |
More than 90% of the tokens came from cache reads. In other words, the headline number looked enormous, but the actual billing impact was far lower than the figure suggested.
That is only half the story. The more important lesson is that understanding token billing is the foundation for effective optimization.
2. The Four Roles of Tokens—and Their Very Different Costs
In the pricing systems used by many major large language model APIs, tokens generally fall into four categories. Their costs can differ significantly.
1. Direct input tokens
These are the context contents actually sent to the model in each request. They are billed at the standard input rate and are among the most expensive token types.
In a long-running conversation, each request may include the conversation history again. If the context is not managed carefully, costs can grow linearly or even faster as the session continues.
2. Cache creation tokens
Many model services provide prompt caching. When the context exceeds a certain threshold—often around 1,024 tokens—the system can store it on the server for reuse.
Writing content to the cache incurs a charge. The price is approximately 1.25 times the standard input rate, making cache creation more expensive than ordinary input because it also involves storage.
3. Cache read tokens
When a later request reuses cached content, the cost can be approximately one-tenth of the standard input price.
This is the underlying reason a long conversation can sometimes be more cost-effective than expected. When the cache hit rate is high, large amounts of context can be reused at a very low cost.
4. Output tokens
These are the tokens generated by the model. They typically cost three to five times more than input tokens.
However, in coding-assistance scenarios, output often represents only a small share of total usage. In the example above, output accounted for just 0.15%, so it is usually not the primary area for cost optimization.
The core principle
The higher the proportion of cache reads, the lower the cost per unit of work. Frequent cache invalidation and repeated cache creation, rather than context reuse itself, are what cause usage to increase rapidly.
3. Reviewing the Case: Why 23% of the Daily Quota Disappeared
In the 9.7-million-token session, the most noticeable pattern was a series of cache-creation spikes.
The investigation showed that the cache had a fixed lifetime, typically measured in minutes. If the interval between two requests exceeded that lifetime, the entire cache expired. The next request then had to write the full context again, effectively charging the developer for hundreds of thousands of tokens at 1.25 times the standard input rate.
On one occasion, the developer stepped away to handle another task and returned later to continue the conversation. A single cache expiration and rebuild consumed approximately 23% of the daily quota.
This experience points directly to the most useful optimization strategies.
4. Ten Practical Token Optimization Strategies
1. Keep the session active to avoid cache expiration
Understand the cache lifetime of the service you use. During long-running tasks, avoid extended interruptions whenever possible.
If a pause is unavoidable, consider sending a lightweight request after returning to warm up the cache before starting the main task.
2. Put stable content at the beginning of the prompt
Caching commonly works through prefix matching. Stable content—such as system instructions and project documentation—should appear at the beginning of the context, while changing conversation content should follow it.
This arrangement maximizes cache reuse. Even a small modification near the beginning of the context can invalidate all cached content that follows.
3. Replace lengthy history with summaries
When a conversation becomes too long, ask the model to create a progress summary. Then start a new session and continue with that summary.
A 500-token summary can replace tens of thousands of tokens of complete conversation history, often without a noticeable decline in output quality.
4. Specify files precisely instead of scanning the entire project
When asking for help, identify the relevant file paths and code sections instead of asking the tool to search the whole repository.
Repository-wide searches can place large amounts of unrelated code into the context, making them one of the most common causes of excessive cache creation.
5. Load large files only when needed
Avoid pasting entire log files, data files, dependency manifests, or other large documents into the context. Provide the relevant line ranges or key sections instead.
If necessary, first ask the tool to provide an overview of the file structure, then read only the sections required for the task.
6. Break large tasks into smaller units
Broad instructions such as “refactor the entire module” can trigger extensive exploratory reading.
Break the work into specific subtasks, such as “modify this function” or “update this interface.” Completing each subtask in a separate session keeps the context cleaner and makes costs easier to control.
7. Use context compression when available
Many coding assistants provide context compression or session-cleanup features.
When a conversation starts to feel slower or the quality of responses begins to decline, compress the context proactively rather than waiting for automatic truncation.
8. Reduce unnecessary back-and-forth
Provide the relevant background, error logs, and expected outcome in the initial request whenever possible.
Avoid gradually revealing information across many small messages. Every additional round trip may require the full context to be sent again.
9. Monitor usage regularly
Review the categories that make up your token consumption on a regular basis.
Pay particular attention to unusual spikes in cache creation. They often indicate a context-management problem rather than an unusually complex task.
10. Use general cloud cost-governance capabilities
When deploying model capabilities in an enterprise environment, combine them with general-purpose cloud services for logging, monitoring, alerting, and usage analysis.
Teams can set token-usage thresholds and create categorized reports, turning individual optimization habits into shared engineering standards. Platforms such as Baidu AI Cloud provide monitoring, alerting, and log-analysis capabilities that can support this type of general cost governance.
5. Conclusion
Token optimization is not about using less AI. It is about making every token serve a clear purpose.
The goal is to maximize cache reads, minimize cache rebuilds, and keep model output focused. Once developers understand the cost structure of the four token types and apply engineering practices such as session management, precise context selection, and usage monitoring, they can often reduce costs substantially without sacrificing coding productivity.
In the age of AI-assisted development, cost awareness is becoming a basic professional skill—just like programming ability itself.
