Understanding AI Coding Token Costs: Caching and Context Basics for Beginners
This guide explains why AI coding assistant sessions can accumulate millions of tokens, how the four token billing categories work, and how prompt caching affects costs—so you can identify which consumption is worth optimizing and which is just normal cost.
This article is for developers using AI coding assistants, especially newcomers to these tools. After running a full day of project iteration, many people open their usage dashboard and see a single session has accumulated several million or even tens of millions of tokens. The number looks alarming, but its composition is far more complex than the headline figure. Understanding how it breaks down is the only way to judge which consumption is worth reducing and which is just normal cost.
Why the Numbers Look So Large
Coding sessions have a defining characteristic: every request resends the previous conversation, file contents, and tool outputs to the model. The model itself has no memory of previous turns, so the context keeps accumulating as the session grows. As a result, each request carries an increasingly large number of tokens, and the total climbs accordingly.
Usage breakdowns typically show that the vast majority of tokens come from cache reads, with direct input and model output accounting for only a small fraction. This indicates that most consumption comes from repeatedly resending old context, while the genuinely new content per turn is small. The proportions vary widely across tools and sessions, so no universal figures are given here—check your own usage details.
Four Types of Tokens, Each Priced Differently
Billing for mainstream LLM APIs typically divides tokens into four categories:
- Direct input: Content that is genuinely new in each request, such as a new question or newly read code snippet. It is billed at the standard input rate.
- Cache creation: Tokens generated when the server writes a segment of prefix content into cache. Writing usually costs more than standard input because of the extra cost of maintaining the cache.
- Cache read: The fee for reusing cached content when a later request hits the cache. Most providers charge far less for reads than for standard input.
- Model output: Code and text generated by the model. The unit price is typically several times that of input, with the exact multiplier varying by model.
This leads to a key insight: in long sessions, the higher the proportion of cache reads, the lower the cost per unit of useful output. The key to saving tokens lies in adjusting your consumption structure.
How Prompt Caching Is Billed and Activated
Caching only takes effect under certain rules, which vary by provider. Anthropic’s prompt caching is used as an example below; for other providers, refer to their official documentation.
- Explicit marking required: Developers must use parameters such as cache_control to mark the content blocks to be cached before the server writes them to cache.
- Minimum length requirements: Cacheable content must reach a certain length, typically starting at around one thousand tokens, with thresholds varying by model.
- Tiered pricing: Using Anthropic’s published prices as an example, the default five-minute cache write costs about 1.25x standard input, and reads cost about 0.1x; the one-hour tier has a higher write price of roughly 2x.
- TTL refreshes: The default cache is valid for about five minutes, with the timer resetting on each hit. During continuous work, the cache keeps hitting. If you pause too long, the cache expires and the next request incurs the write cost again.
OpenAI’s prompt caching is typically automatic, caching longer prefixes without extra marking, with discounted pricing on cache hits. Google’s Gemini offers both implicit and explicit caching, with explicit caching also incurring storage fees. These differences mean the same session can have completely different billing structures across tools.
Caching is prefix-based. As soon as any earlier segment of content changes, everything after it can no longer hit the cache. This has a major impact on your workflow habits, as explained below.
What’s Inflating Your Context
Analyzing cache-write peaks often reveals the following causes:
- Repeatedly injecting full files: Every request includes entire large files or long configs in context, when only part…
