Building Agents

Notes on designing and building LLM agents

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

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):

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

{"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."}
]}
{"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"]}
  }}}
]}

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: