Skip to main content

Managing Caching in Agents

Prompt caching is one of the biggest levers on the cost and latency of an agent, because an agent resends a growing prefix (system prompt, tool definitions, history) on every turn. Anything that changes an earlier part of that prefix invalidates the cache for everything after it.

What invalidates the cache

  • Editing the system prompt (it sits at the top, so everything after it is invalidated)
  • Adding, removing or reordering tools, for example when skills load or unload tools dynamically
  • Editing or removing earlier messages in the history, such as compaction or trimming

In our agent, skills and tools changing mid-run is one of the biggest sources of cache misses.

Mid-conversation system messages

Newer Claude models allow a system message to be appended mid-conversation rather than rewriting the top-level system prompt. The earlier prefix stays byte-identical, so it stays cached and only the new tail is processed. This is promising for cost reduction in agents that change instructions or tools during a run.

  • Amazon Bedrock documents this as "Mid-conversation system messages". There is beta support for adding and removing tools this way. (Direct link to the Bedrock page still to be added; details such as supported models and beta flags are not yet verified here.)
  • Pydantic AI supports the same idea in its message history: a system prompt part in the first request is the standing system prompt, while one in a later request is a mid-conversation instruction that keeps its position. See Pydantic AI: message history.

Caveats:

  • Position alone does not enable caching. You still need the provider's prompt-caching settings (or an explicit cache point).
  • Never build a mid-conversation system message from untrusted content such as tool output or fetched pages. Only inject instructions you wrote yourself.
  • Frontends that don't send mid-conversation system prompts back can change the prefix every turn and defeat the cache.

To do

  • Add the Bedrock docs link and confirm model support, beta header and cache behaviour
  • Prototype in our agent: append tool/skill changes as mid-conversation messages and measure cache hit rate
  • Claude 5.5 Series