Prompt Caching Explained: How to Cut AI Agent Costs by Reusing Context (2026)

Prompt caching explained: how to cut AI agent costs by reusing context

Every time an AI agent takes a step, it sends the whole conversation back to the model: the instructions, the tool list, and everything said so far. After a few dozen steps, you are paying again and again for the same thousands of tokens. That is a big reason AI bills climb faster than most teams expect, as we covered in our guide to the inference cost paradox.

Prompt caching is one of the most practical fixes. It lets the provider reuse work it has already done on the repeated part of your prompt, so you pay less and wait less. But it only works if you design your prompts for it. This guide explains how it works, what the research shows, what accidentally breaks it, and how to set your agents up properly.

Quick answer: Prompt caching reuses the already-processed beginning of a prompt, such as your system instructions and tool definitions, instead of recomputing it on every request. It only works when the beginning is exactly the same each time. A 2026 study of long-running agents found it cut API costs by roughly 41% to 80%. The key habit: keep stable content first, put anything that changes at the end, and never edit the top of your prompt mid-session.

What Is Prompt Caching?

Think of reading a long book before answering questions about it. The first time, you have to read the whole thing. If someone asks a second question, you would not start from page one again. You would remember what you already read and just handle the new question. Prompt caching does something similar for AI models.

When you send a prompt, the model first processes every token of the input. With caching, the provider stores that processed state for the beginning of your prompt. If your next request starts with the same beginning, the provider skips the repeated work and charges you a reduced price for those tokens.

How It Works: The Prefix Rule

Caching works on the prefix, meaning the start of the prompt. A cache hit happens only when the beginning matches exactly. If even one token near the top changes, everything after it has to be recomputed.

[ Tool definitions ]   stable    can be cached
[ System prompt    ]   stable    can be cached
[ Conversation so far ] grows    partly cached
[ New user message ]   changes   always processed

Anthropic's documentation describes the cached prefix as covering tools first, then the system prompt, then messages, in that order. That order matters: a change to your tools invalidates everything below it, while a change to a message only affects the messages after it.

Why Agents Benefit the Most

A simple chatbot sends short prompts. An agent is different. It carries a long set of instructions and tool definitions, then repeats them on every step of a task that may involve dozens of tool calls. That large, repeated, stable chunk is exactly what caching is designed for. The longer the stable prefix and the more steps in a session, the more you save.

How the Big Providers Handle It

Provider How you use it Good to know
Anthropic (Claude) Mark blocks with cache_control, or use automatic caching Default lifetime is 5 minutes, with a 1-hour option. Writing to the cache costs more than normal input; reading costs a fraction of it.
OpenAI Automatic on supported models Needs an exact prefix match and a minimum prompt length.
Google (Gemini) Implicit (automatic) or an explicit context cache you create Explicit caches are referenced by ID, and storage is billed separately.

These details change often, including prices, lifetimes, and minimum prompt sizes. Always check the current docs for Anthropic and OpenAI before you plan a budget. One more thing to know: a prompt below the minimum size is simply not cached, so a very short prompt gets no benefit.

A Simple Cost Example

Here is a rough example using Claude Sonnet 4.5's pricing from early 2026, as listed in the research paper below: $3.00 per million normal input tokens, $3.75 to write to the cache, and $0.30 to read from it. Imagine an agent with a 20,000-token stable prefix that makes 30 calls in one session, all within the 5-minute cache window.

  • Without caching: 30 calls × 20,000 tokens = 600,000 tokens, which costs about $1.80.
  • With caching: one cache write (about $0.075) plus 29 cache reads (about $0.174) comes to roughly $0.25.

That is about 86% less for the repeated prefix. This ignores output tokens and the growing conversation, so real savings will be lower. Also notice the break-even point: with a 5-minute cache, a single reuse already pays off, because writing plus one read costs less than paying full price twice.

What the Research Shows

A 2026 paper from PwC researchers, "Don't Break the Cache", tested prompt caching on long-horizon agent tasks with OpenAI, Anthropic, and Google models. They ran over 500 agent sessions, each with a 10,000-token system prompt, and measured cost and time to first token (how quickly the first part of the answer arrives).

  • Costs dropped a lot. Savings ranged from about 41% to 80%, depending on the provider and model. The big numbers were around 78% to 80% for GPT-5.2 and Claude Sonnet 4.5, while Gemini 2.5 Pro saw about 41%.
  • Speed gains were more mixed. Latency improvements varied widely, from single digits up to about 31%.
  • Bigger prompts saved more. At 50,000 tokens, savings reached roughly 88% to 89% for the two top models. Below the minimum prompt size, caching could not activate at all.
  • Naive caching can backfire. Caching everything, including dynamic tool results, sometimes made responses slower. In one case (GPT-4o) it added about 9% to latency. Caching only the stable system prompt was the most consistent approach.

A fair warning: this was one benchmark, one agent framework, and models from early 2026, so treat the exact numbers as a guide, not a guarantee. The authors themselves suggest testing on your own workload.

What Accidentally Breaks the Cache

Most cache problems are self-inflicted. Watch out for these:

  • Timestamps or session IDs in the system prompt. A "current time" line at the top changes every request, so the prefix never matches.
  • User-specific details near the top. Names or settings placed before the stable instructions break the shared prefix.
  • Changing the tool list. Adding or removing tools mid-session invalidates the cache, which matters for agents that discover tools dynamically.
  • Editing old messages. Rewriting or pruning earlier parts of the conversation changes the prefix and forces a recompute.
  • Waiting too long. With a 5-minute lifetime, a slow gap between calls means the cache expires before the next request.

How to Design Your Agent for Caching

  1. Put stable content first. Tools and system instructions at the top, then examples, then the conversation.
  2. Push dynamic content to the end. If you must include a date or user detail, place it at the end of the system prompt or in the latest message.
  3. Keep the tool list fixed. Use a stable set of general-purpose tools instead of swapping tools in and out.
  4. Append, do not rewrite. Add new messages to the end of the history instead of editing earlier ones.
  5. Pick the right lifetime. If calls come minutes apart, a longer cache option may be worth its higher write cost. If they come seconds apart, the default is fine.
  6. Keep stable instructions in a stable file. A well-organized AGENTS.md file helps you keep instructions consistent from run to run.

This is a core part of good context engineering: deciding what goes into the model's context, and in what order.

Measure Your Cache Hit Rate

You cannot improve what you do not measure. Most providers report caching in the response's usage data. For example, Anthropic's API returns fields for tokens written to the cache and tokens read from it, so you can see whether caching is really working. If your cache-read count stays at zero, something in your prefix is changing, or your prompt is below the minimum size. Track this number on every call, and treat a sudden drop as a warning sign.

When Caching Does Not Pay Off

  • One-off requests, or calls spaced further apart than the cache lifetime, where you pay the write premium and never collect a discount.
  • Prompts below the provider's minimum size.
  • Workloads where the beginning of the prompt is different every time.

A Note on Privacy

Researchers have shown that timing differences from cache hits can, in some multi-tenant setups, reveal information about cached content. For most developers this is a minor concern, but if you handle highly sensitive data, read your provider's data-handling documentation and avoid putting secrets into cached prefixes.

Frequently Asked Questions

What is prompt caching in simple words?

It is a feature that reuses the already-processed beginning of your prompt, so you do not pay full price to process the same instructions again and again.

How much can prompt caching save?

It depends on your setup. In one 2026 study of long-running agents, savings ranged from about 41% to 80%. Your own results depend on prompt size, how stable your prefix is, and how often it is reused.

Does prompt caching change the AI's answers?

It is not meant to. Caching is designed to reuse computation on identical input, so it should not change how the model responds. It only changes how much you pay and how fast the response starts.

Why is my cache hit rate zero?

Common causes are a changing prefix (such as a timestamp near the top), a changed tool list, a prompt below the minimum size, or too much time between requests.

Is prompt caching the same as response caching?

No. Prompt caching reuses processing of the prompt's beginning. Response caching stores a full answer and returns it for an identical question. They solve different problems and can be used together.

Final Thoughts

Prompt caching is one of the few cost tricks that saves money and time without changing your results. The catch is that it rewards discipline: stable content first, changing content last, and no surprise edits to the top of the prompt. Set that up once, measure your hit rate, and a large share of your repeated token costs simply disappears.


You might also like:

Comments

Popular posts from this blog

AI Evaluation Explained: How to Know If Your AI Agent Is Actually Working

What Is Open-Source AI? Why It's Reshaping the Industry in 2026

What Is Agentic Commerce? How AI Agents Are Starting to Shop in 2026