How to Do More with Fewer Tokens in AI Programming: Master Token Cost Structure
A real 9.7-million-token session may cost far less than the raw number suggests, because the right measure of cost in AI coding isn't total token count but the *type* breakdown. This article explains the four token categories—input, cache creation, cache read, and output—and why cache reads dominate cost efficiency. It shows how prompt caching turns long sessions into a savings advantage rather than a burden, identifies what actually inflates context, and offers six practical tactics: feeding only necessary context, keeping the prefix stable to maximize cache hits, trimming tool output, controlling output length, starting fresh sessions per task, and using structured prompts to reduce round trips. The takeaway: control cost by spending tokens "smartly," not just sparingly.
The Truth Behind a “10-Million-Token” Session
A developer spent a full day iterating on a project with an AI coding tool. When they closed the terminal and pulled up the usage report, the number was 9.7 million tokens.
A figure that large usually triggers panic. But once you break the report down, the picture is far less alarming. The 9.7 million tokens were distributed extremely unevenly:
- Direct input (Input): ~0.03%
- Cache creation (Cache Creation): ~8.1%
- Cache read (Cache Read): ~91.7%
- Model output (Output): ~0.15%
The real bulk is cache read—more than 90% of the total. And cache reads happen to be the cheapest category in the billing model. In other words, the nearly ten million tokens that “burned” on paper translated into an actual charge that was just a fraction of what the surface number suggests.
This points to a key insight that many beginners overlook: in AI coding, the correct measure of cost is not the total number of tokens, but the type structure of those tokens.
1. Four Token “Identities,” Wildly Different Costs
In the billing models of mainstream large language models, the tokens involved in a request are usually split into four categories, each with a distinctly different unit price:
- Input tokens—the new context actually sent to the model on each request. This is the standard billing unit and has the highest unit price.
- Cache creation tokens—when the context exceeds a certain length threshold, the system writes that content into a server-side cache. The initial write costs extra, usually slightly more than ordinary input, because it includes the overhead of storage and index building.
- Cache read tokens—when later requests reuse content that’s already cached, you’re charged only a small fraction of the ordinary input price (in typical industry implementations, on the order of one-tenth). This is exactly why “longer sessions get more economical.”
- Output tokens—the content the model actually generates. The unit price is usually several times that of input, but in “read-heavy, write-light” scenarios like coding, output often makes up a very small share.
From this we can distill a core rule:
The higher the share of cache reads, the more efficiently your cost is being used. Conversely, if input and cache creation make up the vast majority of a bill, it means the cache isn’t being reused effectively, and your money is being wasted.
To put it in one sentence: when you look at a bill, don’t just look at the total—look at what’s inflating the “expensive” part.
2. Prompt Caching: Turning Long Sessions from a Burden into an Advantage
Many people intuitively assume that “the longer you chat, the more it costs,” so they deliberately start fresh sessions frequently. This is actually a misconception.
Mainstream large models widely offer a Prompt Caching capability, whose working principle can be simplified into three steps:
- When your context (system prompt, codebase snippets, conversation history, etc.) stays consistent and reaches the caching threshold, the model “remembers” it on the server side;
- On every subsequent turn, as long as this prefix content stays unchanged, it hits the cache directly and is read at extremely low cost;
- Once the prefix changes (for example, you edit the instruction at the top), the cache is invalidated and must be written again.
This leads to an important conclusion: a stable context prefix is the prerequisite for saving money; a frequently changing prefix is the culprit that burns it.
Here’s an illustrative example. Suppose every request must carry the same project description and coding conventions:
# Stable system prefix (should be cached, gets cheaper with use)System role: Senior backend engineerCoding conventions: 4-space indentation, functions must include type annotationsProject background: This is a data processing service, input is semi-structured logs...# Changing conversation part (different each turn, billed normally)This turn's task: Implement pagination logic, handle empty-list edge cases
As long as the prefix stays unchanged, the system prompt and coding conventions will be continuously served from cache at very low cost. The only thing that grows turn by turn is the new input at the end—“this turn’s task.” This is why an organized long session is often cheaper than a series of repeatedly restarted short sessions.
3. What’s Actually Inflating the Context?
To save money, you first need to know where it’s going. When you look at the “cache creation peaks” in a real session, the culprits inflating the context usually cluster into a few categories:
- Whole-file reads: repeatedly telling the tool to “read the entire file and then modify it,” dumping hundreds or thousands of lines into the context at once.
- Dependency and environment info: repeatedly pasting dependency lists, lock files, and complete directory trees.
- Bloated tool output: dumping build logs, error stack traces, and test output in full without any trimming.
- Unbounded conversation history: repeated trial and error within the same session, where failed attempts also stay in the context.
- Repeatedly pasted large code blocks: attaching the same chunk of code across many turns when it’s already sitting in the cache.
These all share a common trait: large in size, frequent in change, and low in reusability. They inflate both input and cache creation costs while crowding the limited context window—leading the model to “see more but grasp less accurately.”
4. Token Throttling in Practice: Spend Compute Where It Counts
Once you understand the cost structure, you can optimize with precision. The following practices deliver results almost immediately.
1. Feed only necessary context
Don’t hand the model “the entire project.” Precisely describe the files, functions, and error lines you need, and use retrieval to locate them rather than pasting everything in full. An efficient prompt looks like this:
// Bad: dumping the whole file wastes input tokensHere are all 320 lines of utils.py, please fix the bug on line 87...// Good: precise targeting hits the key pointIn the parse_date function of utils.py (around lines 80-95),passing an empty string raises a ValueError,please modify only the boundary-check logic in this function.
2. Keep the prefix stable to maximize cache hits
Place the unchanging content (system role, coding conventions, project background) at the very front of the session, and avoid modifying it mid-way. Put task-related, changing content at the end. This maximizes the cache read share and drives down the most expensive “input + cache creation” to the lowest possible level.
3. Trim tool output
For build logs, errors, and test results, keep only the key fragments. Rather than pasting two thousand lines of logs, share just the most critical stack trace and the relevant lines. Many tools support “show error summary only”—make good use of these to significantly shrink input volume.
4. Control output length
Output tokens are the most expensive. When you need just a snippet of code, a single function, or one judgment call, tell the model explicitly: “give only the changed part, don’t restate the whole file” or “answer in one sentence.” Suppressing verbose output saves both money and your reading time.
5. Start a new session in stages and per task
When one task is complete and you move on to a completely unrelated next task, decisively start a fresh session. Let the old task’s history leave the context entirely, rather than letting it keep occupying space in the cache and accumulating cost. Keeping one session focused on one coherent goal is key discipline for cost control.
6. Use structured instructions to reduce round trips
State the requirements completely in one go—what the input is, the expected output, the constraints, and the edge cases. Every extra round trip is a full “input + cache read + output” expense. Saying it once clearly beats asking ten times.
5. Summary: From “Total Volume” to “Structure”
Back to that 9.7-million-token bill from the start—its truth is that the truly valuable part was small, while the vast majority was cheap cache reads. The most important lesson here is:
In AI coding, the core of cost control isn’t “use less,” but “use it right”:
- Understand the structure: distinguish the four token types first, then talk about optimization;
- Feed accurate context: give only what’s necessary, and don’t let junk blow up the window;
- Stabilize the prefix: let caching do its work, and dilute expensive operations down to the minimum;
- Manage the output: make the model talk less and get to the point.
Turn these four into habits, and you’ll find the same budget solves far more problems. Tokens aren’t better the less you spend—they’re more worthwhile the more intelligently you spend them.
