Prompt Caching in Practice: Save 90% on AI Coding Token Costs

How prompt caching slashes AI coding agent costs by 90% through caching inputs, not outputs.
Prompt caching doesn't cache LLM outputs—it caches the KV pairs computed during input processing. In AI coding agents where full context is resent every turn, costs explode exponentially. By keeping system prompts static, making history append-only, understanding prefix matching, and managing cache expiration, you can achieve up to 90% token cost reduction.
When using AI coding agents, you've probably noticed how alarmingly fast your bills grow. A seemingly ordinary conversation can consume millions or even tens of millions of tokens behind the scenes. The core issue often isn't the model itself, but whether your agent framework correctly implements Prompt Caching. This article, based on technical insights shared by Hugging Face, provides a deep dive into the principles and practical essentials of prompt caching, helping you reduce token costs by up to 90%.
What Prompt Caching Actually Caches: A Widely Misunderstood Concept
If you have a software development background, your first impression of "caching" is probably database caching: a user sends a query, the result gets cached, and the next identical query hits the cache directly without accessing the database. Many people naturally assume prompt caching works the same way—caching the LLM's output so that the same prompt returns the cached result next time.
This is a fundamental misconception.
In reality, caching LLM outputs is nearly useless. What prompt caching actually caches is the input, not the output. More precisely, it caches the intermediate computation results produced when the model processes the input. To understand this, you need to grasp two layers of knowledge: the internal computation mechanism of Transformers, and how coding agents work.
The KV Cache Mechanism in LLM Inference
The fundamental reason prompt caching can dramatically reduce costs lies in the KV Cache (Key-Value Cache) mechanism within the Transformer architecture. When a large language model processes input, each Transformer layer computes Key and Value vectors for every token—these vectors form the core data for attention calculations. Without caching, every request requires computing KV pairs for all tokens from scratch, with computational cost proportional to the token count. The essence of prompt caching is storing these pre-computed KV pairs in GPU memory or high-speed storage on the API server side. When subsequent requests have an identical prefix, these intermediate computation results are directly reused, skipping redundant forward pass calculations. This is the technical foundation for why cached pricing can drop to 10% of the original—providers save massive GPU compute and pass part of those savings on to users.
With this underlying principle understood, the coding agent's workflow becomes much clearer: with each conversation turn, the agent resends the complete historical context to the model.
Here's an example: you've been chatting with an agent for a while, accumulating 50,000 tokens of context (including tool calls, tool results, etc.). Then you ask a new question, writing 1K tokens of prompt. To let the model understand the full context, you must send all ~51,000 tokens at once. After the model responds with ~3K tokens, when you ask the next question, you send all 55,000 tokens again.
This is the essence of agents—continuously appending new content at the end, then resending the entire conversation history. Some APIs (like the Responses API) abstract this process away, making the model appear to "have memory," but behind the scenes the LLM reprocesses the entire context every time.

Why AI Coding Agent Token Costs Grow Exponentially
The reality is harsh: if you sustain a 50,000-token conversation, your actual consumption is far more than 50,000 tokens—it's the cumulative total of resending each round: 50K, 51K, 55K... layer upon layer, with costs skyrocketing exponentially.
The Mathematical Model of Cumulative Resending
Modern LLM context windows have grown from early 4K and 8K to today's 128K, 200K, or even million-token levels. But larger context windows in agent scenarios actually mean higher potential costs. Take a typical coding agent workflow: each tool call (reading files, executing code, searching documents) produces both tool input and tool output tokens, all appended to the conversation history. A complex coding task might involve dozens of tool calls, each potentially generating thousands of tokens of file content or execution results. Using the arithmetic series summation model, if each round adds about 3,000 tokens over 50 rounds of conversation, the cumulative resent tokens total approximately 50×(initial context) + 50×51/2×3000, easily exceeding ten million. This explains why real-world cases can produce 10.9 million tokens of exchange.
Taking the two most expensive providers on the market as examples—OpenAI's GPT-5 series and Anthropic's Claude Opus 4.5—input pricing is approximately $4 per million tokens. Under the cumulative resending pattern described above, costs become terrifying.
Assume a conversation with 200,000 tokens of context with an agent, and calculate the costs across different models: Claude Opus, GPT-5.6, Gemini 3.0, Kimi, DeepSeek, etc.

The results are staggering: without caching, Opus and GPT-5.6 cost about $8 for just one short 200K-token conversation. And this is still a very short conversation. Google and Kimi are similar—equally expensive.
But once prompt caching is enabled, costs drop dramatically. DeepSeek is absurdly cheap, practically free (though such low pricing is likely temporary).
How Prompt Caching Works: Full Price First, Discounted on Reuse
Virtually all major LLM APIs adopt this strategy: the model charges standard price the first time it sees a token, and subsequent times it sees the exact same token, the price drops significantly—typically to about 10% of the original.
This means:
- First time reading your prompt → full price (or even slightly higher)
- Subsequent repeated readings of the same tokens → discounted price (~10%)
Prefix Matching Mechanism and Cache Invalidation Details
It's crucial to emphasize that prompt caching uses a strict Prefix Matching strategy, not arbitrary-position matching. This means the cache compares token by token starting from the very first token in the sequence. Once a mismatch occurs at any position, all cache after that position is invalidated. This explains why inserting a dynamic timestamp in your system prompt is catastrophic: if the timestamp appears at the 500th token position, even if tens of thousands of tokens after it are identical to the cache, they cannot be reused because the prefix already broke at token 500. Providers also have block alignment requirements—for example, Anthropic requires a minimum cache unit of 1024 tokens, while OpenAI caches at 128-token granularity. Understanding this core prefix matching mechanism is a prerequisite for correctly using prompt caching.
The cost comparison chart makes this difference more intuitive: the x-axis shows the current token count in the conversation, and the y-axis shows the price per request.
- Without caching: as conversation context grows, price increases exponentially
- With caching: price maintains relatively linear growth
The latter is obviously what we want. You want to enjoy that ~10% discount price as much as possible.

A real-world case: using Tunacode (a Python port of PI) running a DeepSeek model through Hugging Face. Throughout the entire conversation, 10.9 million tokens were exchanged with the model. The context window was only 126K, but because of back-and-forth conversation, massive tokens were repeatedly exchanged each round. And all of this cost only $0.06—essentially free.
Practical Essentials: Maximizing Prompt Cache Utilization in Your Agent
To truly save money, you need to pay special attention to the following points when designing your agent framework.
Pay Attention to Cache Expiration Times
Caches aren't permanent—they expire after a period, depending on the provider:
- OpenAI API: cache duration ~1 hour
- Anthropic API: default ~5 minutes, but can be extended to 1 hour when authenticated via Claude Code
- Other routing providers (e.g., Together AI, Cerebras): varies by case
The cache hit pattern is very intuitive: at the start of a conversation, tokens are charged at full price, and cache hit rate increases as the conversation progresses. But when you leave for an extended period (say, for lunch), the cache expires due to timeout, and resuming the conversation requires writing the cache again—paying full price for those tokens once more.
It's worth noting that Hugging Face's inference service achieves excellent caching support through Intel—all requests are routed to the same provider, directly hitting the hot cache.
Cache Challenges for Routing-Based Inference Providers
Routing-based inference providers like Together AI, Cerebras, and Fireworks AI operate by distributing user requests across multiple GPU clusters or different inference nodes. This architecture faces a core challenge in prompt caching scenarios: KV Cache is stored in the memory of specific GPU nodes, and if a user's consecutive requests get routed to different physical nodes, the previously computed KV Cache cannot be reused. Solutions typically include session-based Sticky Routing (ensuring requests from the same conversation are always assigned to the same node) and distributed KV Cache storage (moving cache from GPU memory to a shared high-speed storage layer). Hugging Face's inference service, through its partnership with Intel, employs an optimized routing strategy that ensures requests are directed to nodes holding the hot cache, achieving high cache hit rates. Implementation quality varies significantly across providers, which is why you should pay attention to a provider's caching architecture when making your selection.
Confirm Whether Your Provider Enables Auto-Caching by Default
Different providers have different default behaviors:
- OpenAI, Hugging Face: automatically cache your input
- Anthropic, Gemini: do NOT auto-cache; you need to manually enable it in your agent
Architectural Differences Between Responses API and Chat Completions API
OpenAI's Responses API (launched early 2025, replacing the former Assistants API) is a stateful conversation management interface. It maintains conversation state on the server side—developers only need to send new user messages and a session ID, and the API automatically concatenates historical context and handles caching. The traditional Chat Completions API is stateless, requiring developers to assemble the complete messages array for every request. These two APIs differ significantly in caching behavior: the Responses API, because it manages context concatenation server-side, naturally ensures prefix consistency and cache hits. The Chat Completions API delegates concatenation responsibility to developers, and if developers introduce any subtle changes during assembly (such as format tweaks to messages, reordering of tool definitions), cache invalidation can occur without them realizing it.
Providers using the Responses API typically enable caching by default, while the Chat Completions API may not. Make sure to research how to enable it.
Never Put Dynamic Content in System Prompts
This is the easiest pitfall to stumble into. Say your system prompt is 10K tokens—you obviously want it cached on every request. But if your system prompt contains timestamps, current working directories, dynamically updated tool lists, or any other changing content, the moment these change, the entire cache after that point becomes invalid.
Recalling the prefix matching mechanism discussed earlier, this problem becomes even clearer: the system prompt typically sits at the very beginning of the entire token sequence. Once it changes, prefix matching fails right there, and the cache for tens of thousands of tokens of conversation history that follows is entirely wasted—you pay full price for all of them again. This is why a seemingly harmless Current time: 2025-07-14 10:30:00 can multiply your bill by 10x.

Therefore, remember: system prompts, tool definitions, and history should remain stable during a conversation—conversation history should be append-only. If you genuinely need to pass dynamic information to the model (like the current time), place it in user messages rather than system prompts, since user messages sit at the end of the token sequence and won't affect previously cached content. This ensures a small mistake won't invalidate your cache and blow up your bill.
Understand That Context Compression Resets the Cache
When you run context compression—condensing the entire conversation into a small summary before continuing—this operation invalidates the previous cache. This is normal and expected behavior for compression, but you need to be aware that it resets the cache, and subsequent tokens will need to be read again at full price.
How Context Compression Works and the Trade-offs
Context compression is a common technique for handling ultra-long conversations. Its core idea is that when conversation history approaches the context window limit, the historical content is compressed into a shorter version through a summarization model or rule-based extraction. Common compression strategies include: LLM-based summary compression (having the model generate a concise summary of conversation history), selective dropping (keeping the most recent N rounds and key tool call results while discarding redundant intermediate content), and embedding-based semantic compression (encoding historical content into vectors and recalling by relevance).
Compression resets the cache because the compressed summary text is completely different from the original history text, making prefix matching inevitably fail. Developers need to balance "cache hit rate" against "context window utilization": frequent compression controls context length but sacrifices caching benefits, while no compression maintains cache but may hit window limits. A compromise strategy is setting a high compression threshold (e.g., triggering compression only when context reaches 80% of the window), delaying the trigger as long as possible to balance both concerns.
Summary: Core Principles for Building Cost-Efficient AI Coding Agents
Whether you're building your own agent framework or using existing tools, follow these principles:
- Keep system prompts, tool definitions, and history stable—don't dynamically inject timestamps, working directories, or other changing content
- Make conversation history append-only, never modify—avoid invalidating the entire cache with minor edits
- Know your provider's cache expiration time—maintain continuous conversation within the cache validity period
- Choose an agent framework that can monitor cache hit rates—observe your caching effectiveness in real time
- Understand the prefix matching mechanism—place dynamic content at the end of the token sequence, not the beginning
- Set context compression thresholds wisely—balance between saving window space and maintaining cache hits
- Pay attention to your provider's routing architecture—prefer inference services that support sticky routing or distributed KV Cache
Prompt caching is the most easily overlooked yet highest-ROI aspect of coding agent cost control. Understanding its essence of "caching input, not output"—specifically, caching the intermediate KV pair computation results during Transformer inference—combined with proper framework design, you can absolutely reduce AI coding token costs by up to 90%. In an era where million-token consumption is routine, this isn't optional—it's essential.
Related articles

How AI Data Centers Are Reshaping Electricity Pricing: Cost Allocation and Energy Market Transformation
Surging AI data center power demand is reshaping electricity pricing. This article analyzes grid impacts, three pricing pathways, and implications for consumer bills and energy transition.

Chiplab: AI Tests Firmware on Virtual Chips Without Physical Development Boards
Chiplab enables AI coding assistants to compile, run, and debug embedded firmware on high-fidelity virtual chips via MCP protocol, supporting STM32 and Nordic platforms without physical hardware.

Muse Glimmer Local Testing: Meta's Open-Source 30B Multimodal Model Runs on a Single GPU
Meta releases Muse Glimmer, a 30B open-source multimodal model running on a single 24GB GPU. Tested at 233 tokens/sec with speculative decoding on RTX 5090, Apache 2.0 licensed with GGUF support.