Practical technical guides / AI engineering: tool interoperability and context cost (deferred tool loading and prompt-prefix caching)

Deferred Tool Loading and the Prompt Cache: Keep the Tools Section Fixed and Let Discovered Definitions Join the Conversation

Inserting discovered tools into the tools list reprocessed 19,542 tokens, more than a full 120-tool catalog. Appending them inline reprocessed 7,537.

By Sandeep Belgavi · Published 2026-11-19

Where discovered tool definitions go decides what the cache can reuse
Original explanatory diagram; synthetic values where shown.

Inserting discovered tools into the tools list reprocessed 19,542 tokens, more than a full 120-tool catalog. Appending them inline reprocessed 7,537.

On-demand tools that made the cache worse

The help-desk agent at Belgavi's Book Store has 120 tools: fifteen business areas, from orders and refunds to gift cards, loyalty points and supplier records, each with get, search, create, update, history, export, note and summary operations. As compact JSON in the o200k_base encoding, their definitions come to 13,892 tokens on every model call.

Tanishka's team built on-demand loading themselves. The agent starts with six common tools and a search_tools tool. When a search finds something, the application adds those definitions to the tools list for every later request. The context shrank to a fraction of its old size, and the team expected input processing to fall in proportion. It did not. Turns that loaded a tool were consistently slow, and the share of input served from the provider's prompt cache dropped.

Prompt caching reuses the longest prefix that is identical to an earlier request. Tool definitions sit near the start of that prefix, before the conversation. Every time the team changed the tools list, everything after it, which is the whole conversation so far, had to be processed again. This guide is about where discovered definitions should go, why the hosted tool-search features put them where they do, and how to check a custom implementation before shipping it. Choosing which tools to route to, and testing descriptions for selection, are covered in related guides on this site.

What the hosted tool-search features do

Both large API providers document a deferred-loading mechanism, and both describe it in terms of the prefix.

Anthropic's tool search tool comes in regex and BM25 variants (tool_search_tool_regex_20251119 and tool_search_tool_bm25_20251119). Tools marked defer_loading: true are excluded from the system-prompt prefix. You still send every definition in the tools array on every request, because the API needs them to run the search; defer_loading controls what enters the context window, not what you send. When the model discovers a tool, the API appends a tool_reference block inline in the conversation and expands it into the full definition there, and later turns can reuse it without searching again. The documentation recommends keeping the three to five most frequently used tools non-deferred, notes that a deferred tool cannot carry cache_control, so the cache breakpoint belongs on a non-deferred tool, and lets you write your own search by returning tool_reference blocks from a custom tool.

OpenAI's tool_search in the Responses API works with deferred functions, namespaces and MCP servers. Its guide says tool search is designed to preserve the model's cache: newly discovered tools are injected at the end of the context window. A client-executed variant lets the application run the search and return the loaded definitions as a tool_search_output item.

The common idea is that discovered definitions join the conversation where they were found. The stable part of the request stays stable.

Three designs, one conversation

The companion example replays a 12-turn conversation that needs four tools outside the always-on set, found by searches at turns 2, 4, 7 and 9, each returning five definitions (19 new ones in total). It models each request as a 600-token system prompt, then the tools section, then the conversation, which grows by 350 tokens per turn plus any definitions that join it. A request may reuse the longest prefix identical to the previous request; everything after the first difference is processed again.

DesignContext tokens over 12 turnsTokens processed againFinal context
Full catalog in the tools section201,20418,69218,692
Deferred, discovered definitions appended inline58,0517,5377,537
Discovered definitions inserted into the tools section58,05119,5427,537

The second and third designs carry exactly the same context. The difference is where the new definitions go. Inserting them into the tools section changes the prefix at turns 2, 4, 7 and 9, and each of those turns reprocesses the entire conversation: 1,831, 3,183, 4,651 and 5,887 tokens. Over the conversation that is more reprocessing than sending all 120 tools every time, because a static catalog, however large, is processed once and then served from cache.

These are model numbers with fixed assumptions. Real caches have minimum cacheable lengths, explicit breakpoints and expiry, and providers bill cached and uncached tokens differently. The model shows where a design changes the prefix, which is the part you control.

Building it yourself

A custom search is reasonable when the hosted variants do not fit: tools that depend on the user, a search you want to test offline, or a provider without the feature. Keep three rules.

  1. Never change the tools section after the first request of a conversation. Put the always-on set and the search tool there, sorted in a fixed order, and leave it alone.
  2. Return discovered definitions as content in the search tool's result, or use the provider's reference mechanism, so they enter the conversation at the point of discovery.
  3. Make the search deterministic, so the same query returns the same tools and the same bytes.

With Anthropic's API, a custom search tool returns references rather than raw definitions, and the API expands them:

{
  "type": "tool_result",
  "tool_use_id": "toolu_search_1",
  "content": [
    {"type": "tool_reference", "tool_name": "deliveries_get"},
    {"type": "tool_reference", "tool_name": "deliveries_update"}
  ]
}

Every referenced tool must also be in the request's tools array, normally marked deferred. The companion's search is BM25 over each tool's name, description and parameter names, iterating query words in sorted order. That last detail matters: floating-point sums depend on order, Python's set order changes between processes, and an unsorted loop can flip a tie and change which definitions, and therefore which bytes, join the conversation.

The always-on set

Pick always-on tools by how often conversations call them. The companion keeps cases_get, cases_note, orders_get, customers_get, returns_get, refunds_get and the search tool: 590 tokens, about 4% of the catalog. Keeping reads always on and leaving writes deferred has a side benefit: a write the model must search for is a write it had to describe first. Place the cache breakpoint at the end of this fixed set.

Check that search finds the right tool

On-demand loading only helps if the search finds what the model needs. The companion labels 20 synthetic requests with the tool a reviewer would choose. Plain BM25 put the right tool in the top five for 15 of 20; adding short reviewed keyword lists per area and per operation, plus plural folding, raised that to 18 of 20. The plain search failed on vocabulary: "Open a return for the damaged hardback" matched the verb "return" in descriptions such as "Return counts and totals", while the returns tools are described in the plural, and nothing said that "open" means create. Run a test like this on your own catalog whenever a definition changes.

Failure modes to test

  • A conversation that loads tools at many turns. With inline loading, context still grows; set a limit and start a fresh conversation with a summary when it is reached.
  • Definitions that change between deployments mid-conversation. The tools array must contain every referenced tool with the same definition, or the request fails or behaves differently.
  • A search that returns a write tool for a read request. Log searches and the tool called next.
  • A cache breakpoint placed after the conversation starts, or on a deferred tool.

Verification

python example.py in the companion prints catalog sizes, recall, every search miss and the per-turn context and reprocessed tokens for the three designs; the tests assert every figure above. For your own agent, log per-request cached and uncached input tokens from the provider's usage fields, and check that turns which load a tool do not reprocess the whole conversation.

Primary references and authorship

Original explanation and synthetic examples by Sandeep Belgavi. Companion code: Sandeep-BA/agent-decision-lab/examples/python/tool_catalog_on_demand. Views are my own and do not represent any employer, past or present.