Skip to main content

Prompt caching for AI agents: what to keep in the stable prefix

Prompt caching only pays off when each agent request starts the same way. How to order instructions, tools and context so an AI agent keeps hitting the cache.

Share -
Over-the-shoulder view of a developer typing code on a laptop in a dark room, in front of a large glowing blue wireframe of a brain on a wall screen.

Prompt caching lowers the cost and delay of an AI agent only when the start of each request is byte-for-byte the same as an earlier one. Put instructions, tool definitions and fixed reference material first, push anything that changes per call to the end, and measure cached tokens so a small prompt edit cannot quietly undo the saving.

Why agents depend on prompt caching

An AI agent rarely makes one model call. It plans, calls a tool, reads the result and calls the model again, often many times for a single task. Each of those calls usually resends the same system instructions, the same tool definitions and a growing conversation history. Without caching, the provider reprocesses all of that on every step, and you pay for it every time.

Prompt caching, offered by the main model providers, stores the processed form of a prompt prefix that has been seen recently. When the next request starts with exactly the same content, that part is served from the cache at a lower price and with less waiting. The model still writes a fresh answer from the full input, so caching changes cost and speed, not behaviour. That is different from semantic caching, which returns a stored answer and needs its own safeguards.

The catch is the word "exactly". The match runs from the first token forward and stops at the first difference. One changed character near the top of a prompt means everything after it is processed at full price again.

Order the prompt from stable to volatile

The most useful habit is to treat the prompt as layers, ordered by how often each one changes. OpenAI's prompt caching guide gives the same advice: durable content first, volatile content last.

Layer 1: instructions that never change per request

System instructions, the agent's role, output rules and safety policies go first. Keep user names, dates, request IDs and anything else specific to one call out of this block. If the instructions must vary by customer or product line, keep a small number of fixed variants rather than building a slightly different prompt each time.

Layer 2: tool definitions and schemas

Tool definitions and response schemas are part of the cached prefix, and so is their order. Load them from one fixed source, in a fixed order, with the same formatting on every call. If a step should only use some tools, keep the full list in place and restrict the choice through the provider's tool-choice setting where it offers one, rather than removing tools from the request. Clear, stable tool design, covered in designing tools an AI agent can use, helps here as well.

Layer 3: shared reference context

Reference documents, policies or product data that many requests share come next. This layer changes occasionally, for example when a policy is updated, and the cache rebuilds once after each change.

Layer 4: the conversation and the current request

User messages, retrieved passages, tool results and the current question go last. New turns should be appended to the end of the history, not inserted earlier, so the previous turns stay part of the reusable prefix.

Common ways teams break the cache without noticing

Most cache misses come from small, well-meant changes rather than from the design itself:

  • A timestamp or request ID placed in the system prompt, which makes every request unique from the first line
  • Tool definitions built from a dictionary or registry whose order is not guaranteed between calls
  • JSON serialised with varying key order or whitespace
  • Adding or removing a tool mid-conversation, or changing a schema between steps
  • Rewriting or trimming earlier turns to save space, which changes the prefix that follows them
  • Switching model or reasoning settings between steps, since a cache belongs to one configuration
  • Long gaps between calls, so the cached prefix expires before it is reused

Each of these is easy to fix once it is found, and hard to spot without measurement.

Long-running agents and context limits

Agents that work for a long time eventually fill their context window. Compacting the history is often necessary, as discussed in context engineering for AI agents, but every rewrite of earlier content starts a new prefix. Plan compaction as an occasional, deliberate step rather than trimming a little on every turn, and keep the instructions and tools above the compacted section untouched so they stay cached.

For work spread across many users or parallel threads, check how your provider decides which requests share a cache. Some offer a routing key that groups related requests, and choosing it per user, per conversation or per group of users affects how often the cache is reused.

Measure cache hits like any other production metric

Provider responses report how many input tokens were served from the cache. Log that figure next to latency and cost for every model call, broken down by agent and by step. A sudden drop after a release usually points to a prompt or tool change that moved something volatile into the prefix. Treat it as a regression, the same way you would treat a slower endpoint, and add it to the cost dashboards described in controlling AI agent running costs.

Caching is not worth chasing everywhere. Some providers charge extra to write a prefix into the cache, so a prefix that is rarely reused, or an endpoint with too little traffic to reuse it before it expires, may cost more with caching than without. Check the pricing for your provider and look at real traffic before designing around it.

How CodeDTX helps

CodeDTX builds AI agents for production systems, including the prompt structure, tool layer and monitoring that keep running costs predictable as usage grows. If your agent costs or response times are higher than expected, talk to us about reviewing how its requests are built.

Frequently asked questions

Does prompt caching change what an AI agent says?

No. Prompt caching stores the processed form of a prompt prefix the provider has already seen, so it does not have to recompute that part. The model still generates a new answer from the complete input, including the parts that changed. Answers can vary between calls for the usual reasons, but caching itself does not alter them. Caching stored answers is a separate technique with different risks.

Where should dynamic details like dates and user IDs go?

Put them after the stable layers, close to the current request, or in request metadata if your provider supports it and the model does not need to see them. A date in the first line of the system prompt makes every request unique and means nothing after it is reused. If the model needs the date, add it near the end of the prompt, where it only affects that request.

How do we restrict tools on one step without breaking the cache?

Keep the full, fixed list of tool definitions in every request and limit which tools the model may call through the provider's tool-choice setting, where it is available. Removing tools from the request changes the prefix and forces a rebuild. If certain tools should never be available to an agent, leave them out of its fixed definition from the start rather than adding and removing them per step.

Is prompt caching worth setting up for every agent?

Not always. It pays off when the same long prefix is sent many times within the provider's cache lifetime, which is typical for agents with long instructions and many tool calls. A low-traffic endpoint, or one whose prompt is short or different on every call, may see little benefit, and a write premium can make it more expensive. Measure cached tokens on real traffic before investing heavily.

Share this post

Contact us to build the right product

Talk to our engineers about your application, the systems it connects to, and what you want to build next.

Get in touch
Two people discussing work with a laptop