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 besystem),content(string or content blocks), optionalcache_control - Put
cache_controlon 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. Adding and removing tools is covered in the next section.
- 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.
Adding and removing tools mid-conversation (beta)
Tool changes are made with tool_addition and tool_removal content blocks inside a role: "system" message, instead of re-sending the top-level tools array (which would invalidate the cache). Documented on Bedrock under Mid-conversation tool changes (Beta).
- Beta flags go in the request body as
anthropic_beta, not an HTTP header:mid-conversation-tool-changes-2026-07-01for changes by reference, andinline-tools-2026-09-15as well for tool definitions given inline. Per the docs the inline flag also enables the by-reference blocks, so it can be sent on its own. - By reference: the tool must already be declared in the top-level
toolsarray. The available set starts astools[], removals subtract from it and additions re-add.
{"role": "system", "content": [
{"type": "tool_removal", "tool": {"type": "tool_reference", "name": "get_weather"}},
{"type": "tool_addition", "tool": {"type": "tool_reference", "name": "search_docs"}},
{"type": "text", "text": "Tool set updated for phase 2."}
]}
- Inline: define a brand-new tool in place with
tool_definition. An identical re-definition is a no-op, and a different definition replaces the earlier one from that point on.
{"role": "system", "content": [
{"type": "tool_addition", "tool": {"type": "tool_definition", "definition": {
"name": "convert_length",
"description": "Convert meters to feet.",
"input_schema": {"type": "object", "properties": {"meters": {"type": "integer"}}, "required": ["meters"]}
}}}
]}
- Caching: later turns carry only the definition in history, so adding a tool does not invalidate the cached prefix.
cache_controlis accepted on either block type (on the block or insidedefinition, not both). - Constraints: blocks must be inside a
role: "system"message,tool_removalalways uses a reference, and computer and browser toolset tools can't be defined inline. MCP references and MCP toolset definitions are not available on Bedrock. Limits include 512tool_additionblocks per request for by-reference changes. - Supported models: by reference, Claude Opus 4.8 and the Claude 5 family including Opus 5.5, Sonnet 5.5, Fable 5.2 and Mythos 5.2 (not Sonnet 5). Haiku 5.5 supports inline definitions only. Other models (Sonnet 5, Opus 4.7 and earlier 4.x) return a 400 error.
- Missing beta flag, unsupported model, a block outside a system message or an unknown tool name all return a 400
invalid_request_error.
Check against the live Bedrock page before relying on the limits and model list: they were read from a summary of the later part of that page.
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.
Related Content
- Claude 5.5 Series
No comments to display
No comments to display