AI Coding Tools: How to Solve More Problems with Fewer Tokens
Long sessions with AI coding assistants often report token counts in the millions, which looks alarming at first glance. This article explains why raw token volume is a misleading cost signal, walks through the four token types (input, cache creation, cache read, output) and how differently they are priced, and shows why prompt caching rewards a stable request prefix. It also identifies three common causes of hidden context growth and offers practical habits—stable-prefix-first prompt structure, targeted file reads instead of full dumps, reusing one session for related tasks, starting a fresh session when the topic changes, trimming tool output, and keeping cached prefixes byte-for-byte stable—plus notes on assembling the same capabilities on general-purpose cloud products. The goal is not to save tokens, but to spend each one where it matters most.
A Common Misreading: Token Consumption Is Not the Same as Real Cost
When you work with an AI coding tool over a long development cycle, you may see a startling number: a session that ran and iterated all day accumulates close to ten million tokens. The first reaction is usually panic. But when you look at a detailed breakdown of token usage, the distribution turns out to be extremely uneven—more than ninety percent of it comes from cache reads, and cache reads are billed at a unit price far lower than ordinary direct input.
In other words, even though the surface number looks enormous, the actual billed cost may be a tiny fraction of it. So whether a session was expensive cannot be judged by the total token count alone; what matters is the structure of those tokens. That is the first step toward understanding cost and then optimizing it.
Four Types of Tokens, Four Very Different Prices
In the general billing model used for interacting with large language models, tokens usually fall into four categories with significant price differences:
- Input tokens: the context actually sent to the model in a request that is not served from cache. This is standard pricing and generally the most “expensive” component.
- Cache creation tokens: when the context exceeds a certain length threshold, the system writes it into the prompt cache. This write is billed at a unit price usually slightly higher than normal input, because it involves storage overhead.
- Cache read tokens: the next request reuses the cached content and pays only a price far below that of input. This is the underlying reason why longer sessions can be cheaper.
- Output tokens: what the model actually generates, typically priced at several times the input rate. In coding scenarios, however, output usually accounts for a small share.
The core rule can be compressed into one sentence: the more cache reads you get, the more value you extract from the same money. Long sessions pay off precisely because large amounts of repeated context (system prompts, project structure, files already read) turn into cheap cache reads after the first round.
Why Prompt Caching Works: Reusing a Stable Prefix
The key precondition for prompt caching is that the prefix of the request must remain stable. Mainstream implementations follow a common principle: content from the beginning of the request up to a certain marker is treated as a single cache unit, and any later request hits the cache as long as that prefix matches byte for byte.
This leads to two conclusions:
- Content that appears earlier and changes less should go into the cache zone. System prompts, coding standards, project conventions, and directory structures rarely change, so they belong at the very front.
- Any tiny change invalidates the cache. Insert a single space into the stable prefix or reorder one line, and everything after it is treated as new content, recomputed and re-cached.
A schematic structure illustrates the ideal way to organize messages:
[Stable prefix: cacheable]System prompt (role, standards, safety constraints)Project structure notes, common conventionsLong-lived background documents--------------------------------[Dynamic part: changes every round]The user's specific question this roundFile excerpts read for the first time this round
What Inflates the Context: Three Hidden Sources
To control cost, first identify what is quietly inflating the context. There are three common sources:
- Reading entire large files. Dumping a whole large file into the context makes cost rise linearly with file size, and you often do not need all of it.
- Repeated, verbose system prompts. Carrying long blocks of instructions on every round looks convenient but continuously consumes input and cache space.
- Untrimmed tool results. Directory listings, dependency lists, logs, and test output, if fed back in full and as-is, quickly accumulate into a massive context.
Practical Strategies for Reducing Token Consumption
Building on the principles above, here is what you can do in day-to-day use:
- Put the stable prefix first and dynamic content later. Keep unchanging project standards and conventions at the very front of the request so they hit the cache for as long as possible.
- Locate precisely instead of dumping everything. Describe the problem and give key file paths or function names so the tool can read only the relevant excerpts, rather than pulling in the whole repository at once.
- Reuse the same session for related tasks. Grouping related, consecutive development tasks in one session maximizes use of the cache already built, turning later rounds into cheap cache reads.
- Converge and clean up in time. When the topic shifts or old context is no longer relevant, start a new session rather than dragging unrelated history along.
- Control how much tool output flows back in. Trim logs and test results to the parts relevant to the current decision.
- Keep the prefix byte-for-byte stable. Avoid casual edits to content in the cached prefix, which would trigger a chain of cache misses.
Building the Capability Yourself on General-Purpose Cloud Products
For teams that want to implement similar capabilities on their own platform, these principles can be assembled on top of general-purpose cloud products: use object storage to host a long-lived project knowledge base and standards documents, container platforms to run inference services, message queues to queue and smooth request spikes, and logging plus monitoring and alerting to observe token structure and cache hit rates. Baidu AI Cloud offers this kind of general-purpose capability set, making it easier for teams to build a cost-optimization pipeline that is both controllable and observable, on demand.
Summary
“A huge number of tokens consumed” is often an illusion; real cost is determined by the type structure of tokens. Once you understand the differences among input, cache creation, cache read, and output, grasp the core mechanism that a stable prefix is cacheable, and manage context through precise reads, session reuse, and timely cleanup, you can solve plenty of problems at lower cost. The essence of cost optimization is not saving tokens, but making sure every token is spent where it is worth the most.
