YOUR AVERAGE DEV
AI
Prompt Caching: Why Your AI Agent Gets Faster as the Chat Gets Longer
AI
Prompt Caching: Why Your AI Agent Gets Faster as the Chat Gets Longer
Every AI agent turn resends the whole conversation. Prompt caching is how providers avoid reprocessing it, and it shapes what these tools cost. How it works, what it costs, and what breaks it, from the official docs.
If you have used an AI coding agent for a long session, you have probably noticed two odd things. The first reply in a fresh session is slow, and later replies come back faster even though the conversation is much longer. And if you ever look at the token usage, you see words like "cache write" and "cache read" next to the normal input count. Both come from the same mechanism: prompt caching. It is one of the least glamorous parts of how these tools work, and one of the most important for what they cost.
## The problem it solves
A language model does not remember your conversation between requests. Every time an agent like Claude Code or Cursor asks the model for the next step, it sends the whole thing again: the system prompt, the tool definitions, the files it has read, every earlier message and tool result. In a long agent session that can be tens of thousands of tokens, resent on every single turn, and the model has to process all of it before it can write a word.
Most of that input is identical from one turn to the next. Only the end changes. Prompt caching lets the provider skip reprocessing the part it has already seen.
## How it actually works
The key word in both Anthropic's and OpenAI's documentation is **prefix**. The cache does not look for similar text anywhere in your prompt. It matches the beginning of the request, token for token, against something it processed recently. When the start of your new request is exactly the same as a cached one, the provider reuses the work it already did for that stretch and only computes the new part at the end.
OpenAI's guide describes what is actually being stored: prompt caching "may store encrypted key/value tensors in GPU-local storage." In plain terms, these are the intermediate numbers the model calculated while reading your prompt. Keeping them around is what makes the reuse possible, and it is also why the cache is tied to a particular model and does not last forever.
Order matters a lot because of this. Anthropic documents a fixed hierarchy: `tools`, then `system`, then `messages`. A change at one level invalidates that level and everything after it. Edit a tool definition and the whole cache is gone. Edit the system prompt and the tools survive but the conversation does not. Append a new message at the end and everything before it can still be reused. That is exactly the shape of an agent loop, which is why caching fits it so well.
## What it costs
Caching is not free, and the pricing explains a lot about how agents are built. On Anthropic's API, writing tokens into a cache with the default 5-minute lifetime costs 1.25 times the normal input price, a 1-hour lifetime costs 2 times, and reading cached tokens back costs 0.1 times the normal input price on most models. OpenAI's guide lists the same 1.25 times write rate for GPT-5.6 and later with a default 30-minute lifetime, and reads at 0.1 times the ordinary input rate on most of those models, with some newer models lower still. OpenAI also sets a minimum: a prefix has to be at least 1,024 tokens on GPT-5.6 and later before it can be cached at all.
So you pay a little extra the first time, and much less every time after. If a prefix is reused even once or twice, you come out ahead. If it is written and never read again, you have paid a premium for nothing.
## The clock is part of the deal
Cache entries expire. Anthropic's default lifetime is 5 minutes, and its documentation points out a detail that surprises people: the time a response spends streaming counts against that lifetime. If a reply takes 4 minutes to stream, the next request has to start within about a minute of it finishing to still hit the cache. OpenAI's caches are also scoped: they are not shared across organizations and cannot be reused across regional processing boundaries.
This is the real reason a session that sits idle while you get coffee can feel slow and cost more when you come back. The agent did not get worse. The cache simply expired, and the next turn had to pay to rebuild it.
## What this means if you build with these APIs
You do not need to be running a big agent for this to matter. A few habits follow straight from the documentation:
- **Put stable content first.** Tool definitions, long instructions and reference documents belong at the start of the request. Anything that changes per request, like a timestamp, a user's name or the latest question, belongs at the end.
- **Do not edit the past.** Rewriting or reordering earlier messages breaks the prefix. Appending keeps it.
- **Watch for sneaky changes.** A date injected into the system prompt, or tool definitions serialized in a different order each time, quietly makes every request a cache miss.
- **Check the usage numbers.** Both providers report cached tokens in the response usage. If cache reads stay at zero across a conversation, something in your prefix is changing.
## Why I think this is worth knowing
A lot of the conversation about AI coding tools is about which model is smartest. Much less of it is about the plumbing, and the plumbing is where cost and speed actually come from. When a tool feels fast in a long session, a good part of that is careful prompt layout that keeps the cache warm, not magic. When a bill looks higher than you expected, a cache that keeps getting invalidated is one of the first things worth checking.
It is a good reminder that these tools are software, built from understandable parts. Knowing how one of those parts works makes you better at using them, and much harder to impress with hype.
## Sources
- [Anthropic: Prompt caching](https://docs.anthropic.com/en/docs/build-with-claude/prompt-caching)
- [OpenAI: Prompt caching](https://platform.openai.com/docs/guides/prompt-caching)
#ai
#prompt caching
#llm
#explainer