Understanding Token Billing in AI Coding Tools: How to Solve More Problems at Lower Cost
AI coding sessions can consume millions of tokens without generating an equally large bill. This article explains the four main token categories—direct input, cache creation, cache reads, and model output—and shows how context size, compression, large tool outputs, and session management affect cost. It also provides six practical strategies for improving cache reuse and reducing unnecessary spending.
A Startling Number—and the Truth Behind It
Last week, I spent an entire day iterating on an engineering project. When I closed the terminal and checked the usage statistics, I saw approximately 9.7 million tokens consumed in a single session.
My first reaction was panic. But after breaking the bill down by token type, the number looked very different:
| 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% |
Cache reads accounted for 91.7% of the total. Since cache reads typically cost about one-tenth as much as direct input, the actual bill was only a fraction of what the headline token count suggested.
But that is only half the story. The other half—why the context became so large and which operations silently caused cache rebuilds—is the part worth paying attention to.
1. The Four Types of Tokens: One Unit, Four Price Tiers
Most major large language model APIs use tiered pricing. Tokens are generally classified into four categories, with prices that can differ by several multiples:
1. Direct input
This is context sent from the client in a request that is not served from the cache. It is billed at the standard input rate and serves as the baseline price.
2. Cache creation
When the context exceeds a certain threshold—around 1,024 tokens is a common industry threshold—the service may write that content to a prompt cache. Cache creation is billable, often at roughly 1.25 times the baseline input price. It costs more than ordinary input because it involves storing the data.
3. Cache reads
When later requests reuse cached context, they only pay approximately one-tenth of the baseline input price. This is the underlying reason that a long, stable session can become more cost-efficient over time.
4. Model output
This is the content generated by the model. Output is typically priced at three to five times the baseline input rate, making it the most expensive category.
One counterintuitive fact is that output often represents a very small share of usage in AI coding workflows. Whether the model is writing code or applying changes, the number of output tokens generated in each turn is usually small compared with the large context repeatedly carried across requests.
The main cost driver is how that context is repeatedly transferred.
This leads to a useful rule of thumb: a high cache-read share usually indicates efficient spending, while frequent cache creation is a warning sign that costs may be getting out of control.
2. What Makes the Context So Large? Three Common Causes
After reviewing the cache-creation spikes in the 9.7-million-token session, I found that most context growth came from three types of operations.
1. Context compression invalidates the cache
Coding agents typically include context-window management. When a conversation approaches the window limit, the agent may automatically perform compression: it summarizes the conversation history and continues working from the summary.
The problem is that compression effectively rewrites much of the context. The old cache becomes invalid, and the compressed context must be cached again. This creates a large, one-time cache-creation cost.
If compression happens frequently—for example, because a task is poorly scoped or a single conversation contains too much information—the cost of rebuilding the cache can accumulate quickly.
2. Large files and large command outputs enter the context directly
Reading an entire source file with several thousand lines, running a command that returns tens of thousands of log lines, or pasting a large error stack trace can inject a significant number of tokens in one operation.
These tokens increase the cost of the current request, but they are also carried into subsequent requests—usually as cache reads—until the context is compressed or discarded.
3. Frequent task switching or manually starting new sessions
Caches usually have a limited lifetime and may expire after a period of inactivity. Frequently switching between unrelated tasks in one workflow, or starting a new session midway through a project, can lead to repeated cold-start cache-creation costs.
Similarly, if every request modifies an earlier part of the context—for example, by editing previous messages or inserting content near the beginning—the cache hit point may move forward. The earlier portion then has to be written to the cache again.
3. Cost-Reduction Practices: Six Practical Strategies
Once the billing model is clear, the goal is straightforward: maximize cache reads, avoid unnecessary cache creation, and reduce the amount of new content injected in each request.
Strategy 1: Define clear task boundaries and focus each session
Break a large project into independent subtasks, and use a separate session for each one. End a session when its task is complete.
This prevents unrelated history from being carried into every request and reduces the chance that an overly long conversation will trigger compression.
Strategy 2: Design the initial context carefully
Place relatively stable content—such as system instructions, project structure, and important conventions—at the beginning of the session.
That content can remain stable in the cache, allowing later requests to pay the much lower cache-read rate. If the same content changes from one request to the next, caching becomes far less useful.
Strategy 3: Avoid reading entire large files
Have the agent read only the relevant portions of a file by using line ranges or targeted searches instead of loading the entire file into context. For very large logs, filter them before injecting the results.
# Poor approach: read the entire filecat huge-file.log # May contain tens of thousands of lines# Better approach: filter before readinggrep "ERROR" huge-file.log | tail -50
Strategy 4: Control the size of tool outputs
Limit command output with tools such as head and tail. Ask the agent to summarize results instead of quoting them in full, and avoid using browser-like tools to retrieve entire HTML pages when only a small section is needed.
Tool output is often the largest source of new context injected in a single turn.
Strategy 5: Monitor cache-hit rates regularly
Most agent tools provide session-level token statistics. Make it a habit to review the share of all four token categories.
If cache creation keeps increasing while cache reads decline, the context may be repeatedly undergoing structural changes. This is a signal to investigate whether the workflow is causing unnecessary cache rebuilds.
Strategy 6: When compression is unavoidable, make it worthwhile
Sometimes compression is necessary. When it happens, make sure the compressed context preserves the information needed for future work, including key decisions, file paths, and unfinished tasks.
One high-quality compression can support dozens of subsequent low-cost cache reads.
4. A Simplified Cost Model
You can think of the cost of a session approximately like this:
Cost per request ≈ (newly injected content × baseline rate)+ (changed context × baseline rate × 1.25)+ (cache-hit context × baseline rate × 0.1)+ (model output × baseline rate × 4)
This produces three useful intuitions:
- A long, stable session is often better than repeatedly starting new sessions—as long as the context is not structurally rewritten, cache reads can spread the cost of historical context over many requests.
- Output is expensive but usually small in volume, so there is no need to be overly restrictive about asking the model for explanations or code comments.
- The real cost killer is repeatedly rewriting the context, which causes cache creation to happen again and again.
Conclusion
A usage figure of 9.7 million tokens sounds alarming, but once the bill is broken down, the genuinely expensive portion may account for less than one-tenth of the total.
For developers, understanding the four token billing categories is more useful than simply trying to “use fewer tokens.” It makes it possible to perform deep, iterative work in a single session while controlling context structure, filtering injected content, and reducing cache rebuilds.
The essence of saving tokens is not asking fewer questions. It is making sure that each token is reused through caching as effectively as possible.
