AI Coding Token Costs: Understanding Your Bill and Reducing Costs with Prompt Caching
Learn how input, cache writes, cache reads, and output contribute to AI coding costs. This article explains prefix-based caching, practical ways to improve reuse, and cost estimates that show why savings depend on pricing, cache lifetime, and request patterns.
1. Breaking Down Token Usage in a Coding Session
One developer recorded approximately 9.71 million tokens during a continuous coding conversation spanning several days. The total looks substantial, but the breakdown tells a different story:
| Token category | Approximate count | Share |
|---|---|---|
| Uncached input | 3,000 | 0.03% |
| Cache writes | 790,000 | 8.1% |
| Cache reads | 8,910,000 | 91.7% |
| Model output | 15,000 | 0.15% |
These figures come from a single conversation. The provider, model version, and reporting methodology were not specified, so they illustrate a usage pattern rather than a benchmark that can be applied to other workloads.
The actual bill depends on the price multiplier for each category. Using illustrative Claude pricing multipliers—1.25 times the base input rate for cache writes, 0.1 times for cache reads, and 5 times for output—the session would cost approximately 1.96 million base-rate token equivalents for roughly 9.71 million tokens processed.
That is about 20% of the hypothetical cost of charging every token at the base input rate. Using an output multiplier of 3 instead of 5 still produces a result close to 20%. This is higher than the frequently cited “one-tenth of the cost,” underscoring the need to calculate savings from the applicable price schedule.
2. How the Four Token Categories Are Billed
Providers classify and price tokens differently. Claude prompt caching provides one example of the underlying mechanics:
| Category | What it means | Illustrative Claude pricing | Common optimization |
|---|---|---|---|
| Uncached input | All input in the current request that is not served from cache, including system prompts, instructions, and code | 1× the base input rate | Avoid repeated pasting; use incremental instructions |
| Cache writes | Writing an eligible prompt prefix to the cache for the first time | Approximately 1.25× for the default five-minute lifetime; one-hour writes cost more | Place stable content at the beginning and minimize prefix changes |
| Cache reads | Reusing a prefix that exactly matches cached content | Approximately 0.1× | Keep the prefix identical and send subsequent requests within the cache lifetime |
| Model output | Code and responses generated by the model | Usually several times the input rate; check official pricing | Request concise output and avoid unnecessary explanations |
The minimum cacheable length varies by model and is commonly around 1,024 tokens.
Other providers, including OpenAI, use different rules. Their caching is generally automatic, cache writes typically carry no surcharge, and read discounts vary by model. Minimum lengths and cache lifetimes also differ. Check the current official pricing and documentation for the specific model before estimating costs.
3. Three Workflows That Commonly Trigger Cache Writes
Scenario A: Cross-File Refactoring
When renaming a public interface or changing a data model, developers often paste several related source files into a prompt. Once that prefix has been written to the cache, later requests can reuse it as long as it remains unchanged.
New instructions and modified files still require input processing rather than reuse of the previous cached content. If any part of the prefix changes, the cached suffix beginning at that point can no longer be reused and must be written again where eligible.
Scenario B: Repeated Reference to Long Specifications
API specifications and requirements documents can run to thousands of lines. Placing a document near the beginning of the conversation allows later rounds of validation and revision to reuse the cached content after the initial write.
This depends on keeping the content before the document unchanged and sending subsequent requests within the cache lifetime.
Scenario C: Maintaining Large Files in Sections
For a source file containing thousands of lines, stable constants and type definitions can be placed before frequently edited functions.
Because caching matches prefixes, changing a function near the end generally invalidates only the cached content from the point of change onward. The stable section before it can still be reused.
4. Three Practical Ways to Reduce Costs
1. Write Stable Context Once, Then Reuse It
Put the largest, most stable materials into the initial request: project structure, core architecture documentation, and coding standards.
The first one or two requests may incur cache-write costs, while later requests can benefit from cache reads. Continue with incremental instructions rather than repeatedly pasting the same background information.
2. Minimize Prefix Changes
System role definitions and code-style requirements tend to change infrequently, so place them at the beginning of the prompt. Put frequently changing requests, such as “modify the logic on line 30,” at the end.
A change anywhere in the prefix prevents reuse of the cached content after that point. Move timestamps, random IDs, and other request-specific values out of the stable prefix as well.
3. Limit History and Start Fresh When Appropriate
As a conversation grows, each request may read more historical context. After completing an independent subtask, consider starting a new conversation with the key decisions, a summary of changes, and a list of newly added files.
This avoids carrying irrelevant history indefinitely. Cache reads are inexpensive, not free, and an excessively long context may also affect response quality.
5. Estimating Savings: Assumptions Matter
The following calculation illustrates the mechanism; it is not an actual bill.
Assume that:
- Only input tokens are counted. Output is billed separately, and new instructions are ignored.
- Every request sends exactly the same context, entirely within the cacheable prefix.
- All reuse occurs within the cache lifetime.
- Cache writes cost 1.25 times the base input rate, and cache reads cost 0.1 times that rate.
For a context of approximately 40,000 tokens sent over 10 requests, normalize the cost of one uncached request to one unit:
| Approach | Request 1 | Requests 2–10 | Total normalized cost |
|---|---|---|---|
| Without caching | 1 | 9 × 1 | 10 |
| With caching | 1.25 | 9 × 0.1 | 2.15 |
Under these assumptions, caching reduces input costs by approximately 78.5%.
Using the same model for 50 requests gives a total of 6.15 units instead of 50, a reduction of approximately 87.7%. These calculations do not support a claim of savings above 95%.
Two limitations are especially important:
- Cache expiration can reverse the savings. If the gap between requests exceeds the cache lifetime and every request requires a new write, 10 requests cost 12.5 units instead of 10 without caching.
- New content increases the bill. If each request adds content, that portion is not covered by the existing cache read and adds input costs.
6. Implementation Checklist
- Inventory stable context. List the names and versions of project structure documents, coding standards, and core interface files. Decide which belong in the prefix.
- Check prefix stability. Confirm that system prompts and fixed documents remain unchanged during the intended reuse period.
- Check request intervals. Keep consecutive requests within the provider’s cache lifetime, such as five minutes where applicable.
- Track the cache-read share. A share above 85% is a suggested rule of thumb, not a universal target. Compare it with historical trends in your own usage.
- Monitor cache-write frequency. If writes remain higher than expected, inspect the prefix for timestamps, random IDs, or frequently changing content.
- Run quality regression tests. Use a fixed set of test questions to compare response quality before and after caching-related changes and confirm that quality has not materially declined.
Usage-report field names vary by platform. Refer to the relevant documentation when interpreting your bill.
7. Conclusion
A high cache-read share indicates effective prefix reuse, but it does not determine the final bill on its own. Pricing multipliers, cache lifetime, prefix stability, and the proportion of output tokens all affect total cost.
The 78.5% reduction above applies only to the stated assumptions. A different provider, request cadence, or output mix will produce different results.
Start by checking the pricing for your provider and model. Then establish a baseline using a week of your own usage, and track cache reuse and write frequency with the checklist above. For changes that may affect response quality, run A/B evaluations with a fixed test set before adopting them more broadly.
