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 let you append a {"role": "system", ...} message to the messages array partway through a conversation. This adds or updates instructions without rewriting the top-level system prompt, so the cached prefix is not invalidated. It is promising for cost reduction in agents that change instructions or tools during a run.

How it works on Amazon Bedrock (Mid-conversation system 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). Violations return 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 header. tool_addition / tool_removal blocks (adding and removing tools mid-conversation) require the "Mid-conversation tool changes" beta, described on the Bedrock tool-use docs page.
  • Supported models per the docs: Claude Opus 4.8 and the Claude 5 family (Opus 5, 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.
  • Claude 5.5 Series