Too many tools: tool search and deferred loading for agents
Once an agent has dozens or hundreds of tools, sending every schema on every turn costs tokens and makes the model pick worse. Here's how Anthropic's and OpenAI's tool search work, where they differ, and the mistakes that get you a 400.
Every agent starts with five tools. Then somebody plugs in the GitHub MCP server, then Slack, then the internal ticketing thing, and three months later the model is reading a phone book of JSON schemas before it does anything at all. You pay for that on every single turn, and the model gets worse at picking the right entry.
Both Anthropic and OpenAI now ship a fix for this, and they call it the same thing: tool search. You mark most of your tools as deferred, the model sees a short catalog, and it pulls in full definitions only when it needs them. The two versions look alike on the surface but differ in ways that will bite you if you port code from one to the other. This post walks through both, using what the official docs actually say.
Why a big tool list hurts
Anthropic's tool search tool docs give a concrete number. A typical multi-server setup with GitHub, Slack, Sentry, Grafana and Splunk "can consume ~55k tokens in definitions before Claude does any work." The same page says tool search typically cuts that by over 85 percent, because Claude only loads the three to five tools it needs for the request.
Put a price on it. At the $2 per million input tokens Claude Sonnet 5.5 lists for on our model table, 55,000 tokens of definitions is $0.11 per request. An agent loop that takes 30 turns to finish a task resends those definitions every turn, so that's $3.30 in tool schemas per task before caching. Caching softens the blow (we covered how in prompt caching for agent loops), but you're still carrying the weight in the context window.
The second problem is accuracy, and it's the one people notice first. Anthropic's docs say Claude gets worse at picking the right tool once you go past 30 to 50 available tools. If you've ever watched an agent call search_issues when it should have called search_code, that's this. Good names and descriptions help a lot, and we went through those in writing tool descriptions and schemas, but past a certain size you need to show the model fewer things at once.
How Anthropic's version works
On the Claude side you add one of two server tools to your tools array: tool_search_tool_regex_20251119 or tool_search_tool_bm25_20251119. Then you put defer_loading: true on every tool you don't want loaded up front. Claude starts out seeing only the search tool and your non-deferred tools. When it needs something else, it searches, the API returns matches as tool_reference blocks (five by default), and the API expands those into full definitions before Claude sees them.
The two variants search differently. With regex, Claude writes Python re.search() patterns, case-insensitive, up to 200 characters, things like get_.*_data or database.*query|query.*database. With BM25 it writes plain natural-language queries up to 500 characters. Both search tool names, descriptions, argument names and argument descriptions, so a keyword buried in a parameter description can still get a tool found.
This next bit catches a lot of people out. You still send every tool's full definition on every request, deferred ones included. The docs are explicit that the API needs them server-side to run the search and expand the references. So tool search doesn't shrink your request payload. It shrinks what goes into the model's context, which is what you're billed for and what hurts accuracy.
A few rules will save you a debugging session. At least one tool has to stay non-deferred, normally the search tool itself, and if you defer everything you get a 400. Never put defer_loading on the search tool. Every tool a search could return has to be in your tools array, or you get a 400 for a missing definition. And when you send the next request, pass the assistant's server_tool_use and tool_search_tool_result blocks back unchanged and don't send a tool_result for the srvtoolu_... ID, because the API rejects that. Your loop should only answer the regular tool_use blocks for the tools Claude actually called.
Search failures don't come back as HTTP errors. A malformed regex, for example, returns a 200 with an error code inside the result: invalid_tool_input, unavailable or too_many_requests. If your harness only checks status codes, it won't notice that a search failed. Claude will just carry on without the tool it was looking for.
The limits are generous. You can defer up to 10,000 tools per request, and Claude can raise the result count with a limit anywhere from 1 to 10,000. Tool search isn't billed as a separate server tool either. The docs say the definitions it loads "count as input tokens like any other tool definition." Support covers current Claude models back to Haiku 4.5 and Sonnet 4.5, but Opus 4.1 and earlier don't have it.
If your tools come through the MCP connector, you don't mark them one by one. You set defer_loading once on the mcp_toolset entry's default_config for the whole server, or per tool in its configs. That's usually the place this pays off most, since MCP servers are where tool counts explode.
Bring your own search
You don't have to use regex or BM25. Anthropic lets you write a custom search tool, run whatever lookup you like (embeddings are the obvious choice), and return a normal tool_result whose content is a list of tool_reference blocks. Every referenced tool still has to exist in the top-level tools array, normally deferred, and the API expands them the same way it does for the built-in search. This is the route to take if your tool descriptions are written in team jargon that a user's plain-English request won't match with keywords.
How OpenAI's version works
On OpenAI's Responses API, you add {"type": "tool_search"} to tools and set defer_loading: true on the functions you want deferred. Per the OpenAI tool search guide, only gpt-5.4 and later models support it there.
The big difference is what the model sees before searching. OpenAI says plainly that for an individual deferred function, the model "still sees the function name and description, so in practice tool search is mostly deferring the parameter schema." For real savings OpenAI wants you to group functions into namespaces or MCP servers. Then the model sees only the namespace's name and description at the start, and none of the functions inside. The guide says its models "have primarily been trained to search those surfaces," and recommends keeping each namespace under 10 functions.
So a deferred setup on OpenAI usually looks like this:
{
"tools": [
{
"type": "namespace",
"name": "crm",
"description": "CRM tools for customer lookup and order management.",
"tools": [
{
"type": "function",
"name": "list_open_orders",
"description": "List open orders for a customer ID.",
"defer_loading": true,
"parameters": {
"type": "object",
"properties": { "customer_id": { "type": "string" } },
"required": ["customer_id"],
"additionalProperties": false
}
}
]
},
{ "type": "tool_search" }
]
}
Notice that defer_loading sits on the functions inside the namespace, not on the namespace object. A namespace can also mix deferred and non-deferred functions, and the non-deferred ones are callable right away.
OpenAI gives you two modes. In hosted mode, OpenAI runs the search over the tools you declared, and the response includes a tool_search_call and a tool_search_output before the eventual function call. In client mode you set execution: "client", the model emits a tool_search_call and stops, your code does the lookup, and you send back a tool_search_output that echoes the same call_id. Client mode lets you hand back tools that weren't in the original request at all, which is handy when the available tools depend on the tenant or project. The guide treats that as advanced and tells you to validate the returned schemas and only expose trusted definitions. Take that seriously. If a tool registry can be written to by someone you don't fully trust, client-mode search is a way to slip a new tool into your agent mid-run.
Caching is the hidden reason this works
Both providers built tool search so it doesn't wreck your prompt cache. OpenAI says newly discovered tools "are injected at the end of the context window," which keeps the cached prefix intact. Anthropic says deferred tools are left out of the system-prompt prefix and discovered tools get appended inline as tool_reference blocks. The practical upshot is the same on both: loading a tool halfway through a run doesn't make you pay to re-cache everything before it.
There are two small traps here. On Anthropic, a deferred tool can't carry cache_control, so if you set a cache breakpoint on your tools, put it on a non-deferred one or you'll get a 400. On OpenAI, if you prune a loaded tool by editing the tool_search_output item, the guide warns that changing the loaded set "will break the model's cache from that point forward." Pruning is allowed, it just costs you.
When to bother
Anthropic's own rule of thumb is to reach for tool search at 10 or more tools, when definitions pass 10,000 tokens, when selection accuracy is slipping, or when you're stacking MCP servers into the hundreds of tools. Under 10 small tools that get used on almost every request, skip it, since every search is an extra model step. OpenAI frames the same tradeoff in its guide: deferred loading "adds a step and depends on finding the relevant tool," so compare task completion, input tokens and latency on real requests before you make it the default.
Whichever provider you're on, keep the handful of tools your agent uses constantly loaded up front. Anthropic says keep your three to five most-used tools non-deferred so Claude can call them without searching first. Prefix tool names by service, like github_ and slack_, so one search pulls in the whole group. Add a line to your system prompt that names the categories it can search for, such as Slack, GitHub and Jira, so the model knows the tools exist at all. Then log which tools actually get discovered. If the model keeps failing to find refund_order, the fix is almost always the description, not the model.
The one thing we'd do first, today, is count. Take the tools array your agent sends now and run it through a token counter (we wrote up how to count tokens before you send). If the definitions come to a few hundred tokens, leave them alone. If it's tens of thousands, you've found the cheapest win in your whole loop.