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 allowlet you append a system{"role": "system", ...} message to bethe appendedmessages mid-conversationarray ratherpartway thanthrough a conversation. This adds or updates instructions without rewriting the top-level system prompt.prompt, Theso earlierthe cached prefix stays byte-identical, so it stays cached and only the new tail is processed.not Thisinvalidated. It is promising for cost reduction in agents that change instructions or tools during a run.

    How

    it works on Amazon Bedrock documents this as "(Mid-conversation system messages"messages):
      Fields: role (must be system), content (string or content blocks), optional cache_control Put cache_control on the last block that stays unchanged, then append the system message after that breakpoint. The new message can itself be cached on later turns. Placement: not the first message, and it must follow a user message (or an assistant message ending in a server tool result). ThereViolations isreturn a 400 error. Don't edit or remove a mid-conversation system message you already sent, as that invalidates the cache from that point. Plain text needs no beta supportheader. fortool_addition / tool_removal blocks (adding and removing tools thismid-conversation) way.require (Directthe link"Mid-conversation totool changes" beta, described on the Bedrock pagetool-use stilldocs topage. be added; details such as supportedSupported models per the docs: Claude Opus 4.8 and betathe flagsClaude are5 notfamily yet(Opus verified5, here.)Opus 5.5, Sonnet 5, Fable 5, 5.1 and 5.2). Other models return a 400 error.

      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.
        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

        Related Content

        • Claude 5.5 Series