Scale to many tools with tool search
--- a/tool-search.md
+++ b/tool-search.md
@@ -3,21 +3,13 @@
* **Context efficiency:** Tool definitions can consume large portions of the context window (50 tools can use 10-20K tokens), leaving less room for actual work.
* **Tool selection accuracy:** Tool selection accuracy degrades with more than 30-50 tools loaded at once.
-##
-
-
-
-How tool search works
+## How tool search works
Tool search is on by default, with the exceptions listed in Configure tool search. When it is active, tool definitions are withheld from the context window. The agent receives a summary of available tools and searches for relevant ones when the task requires a capability not already loaded. Up to five of the most relevant tools are loaded into context by default, where they stay available for subsequent turns until the SDK compacts the messages where the agent discovered them. After that compaction, the agent searches for those tools again when it next needs them. Tool search adds one extra round-trip each time Claude searches for tools, but for large tool sets this is offset by smaller context on every turn. With fewer than ~10 tools whose definitions fit comfortably in the context window, loading everything upfront is typically faster. For details on the underlying API mechanism, see [Tool search in the API](<https://platform.claude.com/docs/en/agents-and-tools/tool-use/tool-search-tool>).
Tool search isn’t supported on Microsoft Foundry [deployments hosted on Azure](<https://platform.claude.com/docs/en/build-with-claude/claude-in-microsoft-foundry#hosting-options>), which reject it server-side: the SDK detects the rejection and loads tool definitions upfront for that deployment instead. `ENABLE_TOOL_SEARCH` can’t override this, since the rejection comes from the deployment itself.
-##
-
-
-
-Configure tool search
+## Configure tool search
Tool search is on by default. For models on the SDK’s unsupported-model list, the SDK loads tool definitions upfront instead, and no `ENABLE_TOOL_SEARCH` value overrides that. On Google Cloud’s Agent Platform, the SDK decides by model generation:
@@ -107,11 +99,7 @@
To run this example, replace `https://tools.example.com/mcp` with the URL of your own MCP server. On success the result text prints to the console. Because this is a single-shot `query()` call, the SDK raises after yielding an error result, so the example wraps the loop in a try block. To see why a run failed, check the result message’s `subtype`, such as `error_during_execution`, inside the loop. For more on result messages, see [Handle the result](</docs/en/agent-sdk/agent-loop#handle-the-result>).
-##
-
-
-
-Optimize tool discovery
+## Optimize tool discovery
The search mechanism matches queries against tool names and descriptions. Names like `search_slack_messages` surface for a wider range of requests than `query_slack`. Descriptions with specific keywords (“Search Slack messages by keyword, channel, or date range”) match more queries than generic ones (“Query Slack”). You can also add a system prompt section listing available tool categories. This gives the agent context about what kinds of tools are available to search for. Pass the text in the `systemPrompt` option of `query()` in TypeScript, or `system_prompt` in Python. Use the `claude_code` preset with `append`, which adds your text to the preset’s prompt instead of replacing it:
@@ -137,21 +125,13 @@
For the full set of system prompt options, see [Modifying system prompts](</docs/en/agent-sdk/modifying-system-prompts>).
-##
-
-
-
-Limits
+## Limits
* **Maximum tools:** 10,000 tools in your catalog
* **Search results:** returns up to five most relevant tools per search by default
* **Model support:** Opus 4, Sonnet 4, Haiku 4.5, and later models, or Opus 4.5, Sonnet 4.5, Haiku 4.5, and later models on Google Cloud’s Agent Platform. On earlier models the SDK loads tool definitions upfront, as Configure tool search describes.
-##
-
-
-
-Related documentation
+## Related documentation
* [Tool search in the API](<https://platform.claude.com/docs/en/agents-and-tools/tool-use/tool-search-tool>): Full API documentation for tool search, including custom implementations
* [Connect MCP servers](</docs/en/agent-sdk/mcp>): Connect to external tools via MCP servers
--- a/tool-search.md
+++ b/tool-search.md
@@ -24,13 +24,17 @@
* **Claude Opus 4.5, Sonnet 4.5, Haiku 4.5, and later** : tool search is on by default.
* **Earlier Agent Platform models** : the SDK loads tool definitions upfront, because their serving stacks reject the required beta header. `ENABLE_TOOL_SEARCH` can’t override this.
-Before Claude Code v2.1.221, the SDK disabled tool search for all models on Google Cloud’s Agent Platform unless you set `ENABLE_TOOL_SEARCH`. The SDK also disables tool search when `ANTHROPIC_BASE_URL` points to a non-first-party host, since most proxies don’t forward `tool_reference` blocks. You can override that default with the `ENABLE_TOOL_SEARCH` environment variable:
+Before Claude Code v2.1.221, the SDK disabled tool search for all models on Google Cloud’s Agent Platform unless you set `ENABLE_TOOL_SEARCH`.
+
+Tool search is on by default behind an `ANTHROPIC_BASE_URL` gateway, with nothing to set. If the gateway rejects it, see [Tool search through an LLM gateway](</docs/en/mcp#tool-search-through-a-gateway>). Before Claude Code v2.1.304, the SDK disabled tool search behind such a gateway unless `ENABLE_TOOL_SEARCH` was `true`, `auto`, or `auto:N`.
+
+You control whether and when tool definitions are deferred with the `ENABLE_TOOL_SEARCH` environment variable, which takes these values:
Value| Behavior
---|---
-(unset)| Tool search is on. Tool definitions are deferred and discovered on demand. Falls back to loading upfront on models earlier than Opus 4 and Sonnet 4, Google Cloud’s Agent Platform models earlier than Opus 4.5, Sonnet 4.5, and Haiku 4.5, a non-first-party `ANTHROPIC_BASE_URL`, or a Microsoft Foundry deployment hosted on Azure.
-`true`| Tool search stays on with a non-first-party `ANTHROPIC_BASE_URL`. The other (unset) fallbacks still apply. The SDK sends the beta header through proxies, and requests fail on proxies that don’t support `tool_reference` blocks.
-`auto`| Counts the tokens in the tool definitions that tool search can defer and compares the total against the model’s context window. When the total reaches 10% of the window, tool search activates. Below that, the SDK loads every tool definition into context upfront.
+(unset)| Tool search is on, with tool definitions deferred and discovered on demand. Falls back to loading upfront on models earlier than Opus 4 and Sonnet 4, on the earlier Agent Platform models, and on Microsoft Foundry deployments hosted on Azure.
+`true`| Tool search is on, with the same fallbacks as (unset).
+`auto`| Tool search is off, and tool definitions load upfront, until the definitions it can defer reach 10% of the model’s context window. Tool search then activates, and can [stay on in the same conversation](</docs/en/mcp#tools-that-stay-deferred-below-the-threshold>) even if those definitions later drop below 10%.
`auto:N`| Same as `auto` with a custom percentage. `auto:5` activates when those definitions reach 5% of the context window. Lower values activate sooner.
`false`| Tool search is off. All tool definitions are loaded into context on every turn.
--- a/tool-search.md
+++ b/tool-search.md
@@ -28,13 +28,13 @@
Value| Behavior
---|---
-(unset)| Tool search is on. Tool definitions are deferred and discovered on demand. Falls back to loading upfront on Google Cloud’s Agent Platform models earlier than the Claude 4.5 generation, a non-first-party `ANTHROPIC_BASE_URL`, or a Microsoft Foundry deployment hosted on Azure.
-`true`| Tool search is always on, except on a Microsoft Foundry deployment hosted on Azure, where the server-side rejection still forces upfront loading, and on Google Cloud’s Agent Platform models earlier than the Claude 4.5 generation, where the SDK keeps loading tool definitions upfront. The SDK sends the beta header through proxies, and requests fail on proxies that don’t support `tool_reference` blocks.
+(unset)| Tool search is on. Tool definitions are deferred and discovered on demand. Falls back to loading upfront on models earlier than Opus 4 and Sonnet 4, Google Cloud’s Agent Platform models earlier than Opus 4.5, Sonnet 4.5, and Haiku 4.5, a non-first-party `ANTHROPIC_BASE_URL`, or a Microsoft Foundry deployment hosted on Azure.
+`true`| Tool search stays on with a non-first-party `ANTHROPIC_BASE_URL`. The other (unset) fallbacks still apply. The SDK sends the beta header through proxies, and requests fail on proxies that don’t support `tool_reference` blocks.
`auto`| Counts the tokens in the tool definitions that tool search can defer and compares the total against the model’s context window. When the total reaches 10% of the window, tool search activates. Below that, the SDK loads every tool definition into context upfront.
`auto:N`| Same as `auto` with a custom percentage. `auto:5` activates when those definitions reach 5% of the context window. Lower values activate sooner.
`false`| Tool search is off. All tool definitions are loaded into context on every turn.
-Setting [`CLAUDE_CODE_DISABLE_EXPERIMENTAL_BETAS`](</docs/en/env-vars>) keeps tool search off. You can’t override it by setting `ENABLE_TOOL_SEARCH` yourself. Your organization can keep tool search on through [managed settings](</docs/en/managed-settings>), on Claude Code v2.1.227 or later. [Disable pre-release capabilities](</docs/en/llm-gateway-protocol#disable-pre-release-capabilities>) covers where the override applies and what the variable strips. Tool search applies to all registered tools, whether they come from remote MCP servers or [custom SDK MCP servers](</docs/en/agent-sdk/custom-tools>). When you use `auto`, the SDK counts every definition that tool search can defer toward one combined threshold: each MCP tool that isn’t marked [`alwaysLoad`](</docs/en/mcp#exempt-a-server-from-deferral>), from any server, plus the built-in tools that load on demand. The SDK loads core built-in tools such as Bash, Read, and Edit upfront and doesn’t count them toward the threshold. A [mod](</docs/en/plugins/mods/reference#tools>) in one of your plugins can also defer a tool or load it upfront, which changes that count. Set the value in the `env` option on `query()`. In TypeScript, `env` replaces the subprocess environment, so spread `...process.env` to keep inherited variables. In Python, `env` is merged on top of the inherited environment. This example connects to a remote MCP server that exposes many tools, pre-approves all of them with a wildcard, and uses `auto:5` so tool search activates when the definitions it can defer reach 5% of the context window:
+Setting [`CLAUDE_CODE_DISABLE_EXPERIMENTAL_BETAS`](</docs/en/env-vars>) keeps tool search off. You can’t override it by setting `ENABLE_TOOL_SEARCH` yourself. On Claude Code v2.1.227 or later, your organization can keep tool search on under this variable through [managed settings](</docs/en/managed-settings>). In a session that [gets the HIPAA configuration](</docs/en/hipaa-setup#check-how-developers-sign-in-and-connect>), tool search is off, and neither `ENABLE_TOOL_SEARCH` nor managed settings turns it back on. [Disable pre-release capabilities](</docs/en/llm-gateway-protocol#disable-pre-release-capabilities>) covers where the override applies and what the variable strips. Tool search applies to all registered tools, whether they come from remote MCP servers or [custom SDK MCP servers](</docs/en/agent-sdk/custom-tools>). When you use `auto`, the SDK counts every definition that tool search can defer toward one combined threshold: each MCP tool that isn’t marked [`alwaysLoad`](</docs/en/mcp#exempt-a-server-from-deferral>), from any server, plus the built-in tools that load on demand. The SDK loads core built-in tools such as Bash, Read, and Edit upfront and doesn’t count them toward the threshold. A [mod](</docs/en/plugins/mods/reference#tools>) in one of your plugins can also defer a tool or load it upfront, which changes that count. Set the value in the `env` option on `query()`. In TypeScript, `env` replaces the subprocess environment, so spread `...process.env` to keep inherited variables. In Python, `env` is merged on top of the inherited environment. This example connects to a remote MCP server that exposes many tools, pre-approves all of them with a wildcard, and uses `auto:5` so tool search activates when the definitions it can defer reach 5% of the context window:
TypeScript
@@ -141,7 +141,7 @@
* **Maximum tools:** 10,000 tools in your catalog
* **Search results:** returns up to five most relevant tools per search by default
- * **Model support:** Claude Sonnet 4.5, Claude Haiku 4.5, Claude Opus 4.5, and later models; see [model compatibility in the API docs](<https://platform.claude.com/docs/en/agents-and-tools/tool-use/tool-search-tool#model-compatibility>) for the current list. The same minimums apply on Google Cloud’s Agent Platform.
+ * **Model support:** Opus 4, Sonnet 4, Haiku 4.5, and later models, or Opus 4.5, Sonnet 4.5, Haiku 4.5, and later models on Google Cloud’s Agent Platform. On earlier models the SDK loads tool definitions upfront, as Configure tool search describes.
##
--- a/tool-search.md
+++ b/tool-search.md
@@ -34,7 +34,7 @@
`auto:N`| Same as `auto` with a custom percentage. `auto:5` activates when those definitions reach 5% of the context window. Lower values activate sooner.
`false`| Tool search is off. All tool definitions are loaded into context on every turn.
-Setting [`CLAUDE_CODE_DISABLE_EXPERIMENTAL_BETAS`](</docs/en/env-vars>) keeps tool search off. You can’t override it by setting `ENABLE_TOOL_SEARCH` yourself. Your organization can keep tool search on through [managed settings](</docs/en/managed-settings>), on Claude Code v2.1.227 or later. [Disable pre-release capabilities](</docs/en/llm-gateway-protocol#disable-pre-release-capabilities>) covers where the override applies and what the variable strips. Tool search applies to all registered tools, whether they come from remote MCP servers or [custom SDK MCP servers](</docs/en/agent-sdk/custom-tools>). When you use `auto`, the SDK counts every definition that tool search can defer toward one combined threshold: each MCP tool that isn’t marked [`alwaysLoad`](</docs/en/mcp#exempt-a-server-from-deferral>), from any server, plus the built-in tools that load on demand. The SDK always loads core built-in tools such as Bash, Read, and Edit upfront and doesn’t count them toward the threshold. Set the value in the `env` option on `query()`. In TypeScript, `env` replaces the subprocess environment, so spread `...process.env` to keep inherited variables. In Python, `env` is merged on top of the inherited environment. This example connects to a remote MCP server that exposes many tools, pre-approves all of them with a wildcard, and uses `auto:5` so tool search activates when the definitions it can defer reach 5% of the context window:
+Setting [`CLAUDE_CODE_DISABLE_EXPERIMENTAL_BETAS`](</docs/en/env-vars>) keeps tool search off. You can’t override it by setting `ENABLE_TOOL_SEARCH` yourself. Your organization can keep tool search on through [managed settings](</docs/en/managed-settings>), on Claude Code v2.1.227 or later. [Disable pre-release capabilities](</docs/en/llm-gateway-protocol#disable-pre-release-capabilities>) covers where the override applies and what the variable strips. Tool search applies to all registered tools, whether they come from remote MCP servers or [custom SDK MCP servers](</docs/en/agent-sdk/custom-tools>). When you use `auto`, the SDK counts every definition that tool search can defer toward one combined threshold: each MCP tool that isn’t marked [`alwaysLoad`](</docs/en/mcp#exempt-a-server-from-deferral>), from any server, plus the built-in tools that load on demand. The SDK loads core built-in tools such as Bash, Read, and Edit upfront and doesn’t count them toward the threshold. A [mod](</docs/en/plugins/mods/reference#tools>) in one of your plugins can also defer a tool or load it upfront, which changes that count. Set the value in the `env` option on `query()`. In TypeScript, `env` replaces the subprocess environment, so spread `...process.env` to keep inherited variables. In Python, `env` is merged on top of the inherited environment. This example connects to a remote MCP server that exposes many tools, pre-approves all of them with a wildcard, and uses `auto:5` so tool search activates when the definitions it can defer reach 5% of the context window:
TypeScript
--- a/tool-search.md
+++ b/tool-search.md
@@ -109,7 +109,7 @@
Optimize tool discovery
-The search mechanism matches queries against tool names and descriptions. Names like `search_slack_messages` surface for a wider range of requests than `query_slack`. Descriptions with specific keywords (“Search Slack messages by keyword, channel, or date range”) match more queries than generic ones (“Query Slack”). You can also add a system prompt section listing available tool categories. This gives the agent context about what kinds of tools are available to search for. Pass the text through the `systemPrompt` option in TypeScript or `system_prompt` in Python, using the `claude_code` preset with `append`, which adds your text to the preset’s prompt instead of replacing it:
+The search mechanism matches queries against tool names and descriptions. Names like `search_slack_messages` surface for a wider range of requests than `query_slack`. Descriptions with specific keywords (“Search Slack messages by keyword, channel, or date range”) match more queries than generic ones (“Query Slack”). You can also add a system prompt section listing available tool categories. This gives the agent context about what kinds of tools are available to search for. Pass the text in the `systemPrompt` option of `query()` in TypeScript, or `system_prompt` in Python. Use the `claude_code` preset with `append`, which adds your text to the preset’s prompt instead of replacing it:
TypeScript
--- a/tool-search.md
+++ b/tool-search.md
@@ -9,7 +9,7 @@
How tool search works
-Tool search is on by default, with the exceptions listed in Configure tool search. When it is active, tool definitions are withheld from the context window. The agent receives a summary of available tools and searches for relevant ones when the task requires a capability not already loaded. Up to five of the most relevant tools are loaded into context by default, where they stay available for subsequent turns. When the SDK compacts earlier messages to free space, it keeps the discovered tools loaded. Tool search adds one extra round-trip the first time Claude discovers a tool (the search step), but for large tool sets this is offset by smaller context on every turn. With fewer than ~10 tools whose definitions fit comfortably in the context window, loading everything upfront is typically faster. For details on the underlying API mechanism, see [Tool search in the API](<https://platform.claude.com/docs/en/agents-and-tools/tool-use/tool-search-tool>).
+Tool search is on by default, with the exceptions listed in Configure tool search. When it is active, tool definitions are withheld from the context window. The agent receives a summary of available tools and searches for relevant ones when the task requires a capability not already loaded. Up to five of the most relevant tools are loaded into context by default, where they stay available for subsequent turns until the SDK compacts the messages where the agent discovered them. After that compaction, the agent searches for those tools again when it next needs them. Tool search adds one extra round-trip each time Claude searches for tools, but for large tool sets this is offset by smaller context on every turn. With fewer than ~10 tools whose definitions fit comfortably in the context window, loading everything upfront is typically faster. For details on the underlying API mechanism, see [Tool search in the API](<https://platform.claude.com/docs/en/agents-and-tools/tool-use/tool-search-tool>).
Tool search isn’t supported on Microsoft Foundry [deployments hosted on Azure](<https://platform.claude.com/docs/en/build-with-claude/claude-in-microsoft-foundry#hosting-options>), which reject it server-side: the SDK detects the rejection and loads tool definitions upfront for that deployment instead. `ENABLE_TOOL_SEARCH` can’t override this, since the rejection comes from the deployment itself.
--- a/tool-search.md
+++ b/tool-search.md
@@ -9,7 +9,7 @@
How tool search works
-Tool search is on by default, with the exceptions listed in Configure tool search. When it is active, tool definitions are withheld from the context window. The agent receives a summary of available tools and searches for relevant ones when the task requires a capability not already loaded. Up to five of the most relevant tools are loaded into context by default, where they stay available for subsequent turns. If the conversation is long enough that the SDK compacts earlier messages to free space, previously discovered tools may be removed, and the agent searches again as needed. Tool search adds one extra round-trip the first time Claude discovers a tool (the search step), but for large tool sets this is offset by smaller context on every turn. With fewer than ~10 tools whose definitions fit comfortably in the context window, loading everything upfront is typically faster. For details on the underlying API mechanism, see [Tool search in the API](<https://platform.claude.com/docs/en/agents-and-tools/tool-use/tool-search-tool>).
+Tool search is on by default, with the exceptions listed in Configure tool search. When it is active, tool definitions are withheld from the context window. The agent receives a summary of available tools and searches for relevant ones when the task requires a capability not already loaded. Up to five of the most relevant tools are loaded into context by default, where they stay available for subsequent turns. When the SDK compacts earlier messages to free space, it keeps the discovered tools loaded. Tool search adds one extra round-trip the first time Claude discovers a tool (the search step), but for large tool sets this is offset by smaller context on every turn. With fewer than ~10 tools whose definitions fit comfortably in the context window, loading everything upfront is typically faster. For details on the underlying API mechanism, see [Tool search in the API](<https://platform.claude.com/docs/en/agents-and-tools/tool-use/tool-search-tool>).
Tool search isn’t supported on Microsoft Foundry [deployments hosted on Azure](<https://platform.claude.com/docs/en/build-with-claude/claude-in-microsoft-foundry#hosting-options>), which reject it server-side: the SDK detects the rejection and loads tool definitions upfront for that deployment instead. `ENABLE_TOOL_SEARCH` can’t override this, since the rejection comes from the deployment itself.
--- a/tool-search.md
+++ b/tool-search.md
@@ -34,7 +34,7 @@
`auto:N`| Same as `auto` with a custom percentage. `auto:5` activates when those definitions reach 5% of the context window. Lower values activate sooner.
`false`| Tool search is off. All tool definitions are loaded into context on every turn.
-Setting [`CLAUDE_CODE_DISABLE_EXPERIMENTAL_BETAS`](</docs/en/env-vars>) keeps tool search off. You can’t override it by setting `ENABLE_TOOL_SEARCH` yourself. Your organization can keep tool search on through [managed settings](</docs/en/settings#settings-files>), on Claude Code v2.1.227 or later. [Disable pre-release capabilities](</docs/en/llm-gateway-protocol#disable-pre-release-capabilities>) covers where the override applies and what the variable strips. Tool search applies to all registered tools, whether they come from remote MCP servers or [custom SDK MCP servers](</docs/en/agent-sdk/custom-tools>). When you use `auto`, the SDK counts every definition that tool search can defer toward one combined threshold: each MCP tool that isn’t marked [`alwaysLoad`](</docs/en/mcp#exempt-a-server-from-deferral>), from any server, plus the built-in tools that load on demand. The SDK always loads core built-in tools such as Bash, Read, and Edit upfront and doesn’t count them toward the threshold. Set the value in the `env` option on `query()`. In TypeScript, `env` replaces the subprocess environment, so spread `...process.env` to keep inherited variables. In Python, `env` is merged on top of the inherited environment. This example connects to a remote MCP server that exposes many tools, pre-approves all of them with a wildcard, and uses `auto:5` so tool search activates when the definitions it can defer reach 5% of the context window:
+Setting [`CLAUDE_CODE_DISABLE_EXPERIMENTAL_BETAS`](</docs/en/env-vars>) keeps tool search off. You can’t override it by setting `ENABLE_TOOL_SEARCH` yourself. Your organization can keep tool search on through [managed settings](</docs/en/managed-settings>), on Claude Code v2.1.227 or later. [Disable pre-release capabilities](</docs/en/llm-gateway-protocol#disable-pre-release-capabilities>) covers where the override applies and what the variable strips. Tool search applies to all registered tools, whether they come from remote MCP servers or [custom SDK MCP servers](</docs/en/agent-sdk/custom-tools>). When you use `auto`, the SDK counts every definition that tool search can defer toward one combined threshold: each MCP tool that isn’t marked [`alwaysLoad`](</docs/en/mcp#exempt-a-server-from-deferral>), from any server, plus the built-in tools that load on demand. The SDK always loads core built-in tools such as Bash, Read, and Edit upfront and doesn’t count them toward the threshold. Set the value in the `env` option on `query()`. In TypeScript, `env` replaces the subprocess environment, so spread `...process.env` to keep inherited variables. In Python, `env` is merged on top of the inherited environment. This example connects to a remote MCP server that exposes many tools, pre-approves all of them with a wildcard, and uses `auto:5` so tool search activates when the definitions it can defer reach 5% of the context window:
TypeScript
--- a/tool-search.md
+++ b/tool-search.md
@@ -30,11 +30,11 @@
---|---
(unset)| Tool search is on. Tool definitions are deferred and discovered on demand. Falls back to loading upfront on Google Cloud’s Agent Platform models earlier than the Claude 4.5 generation, a non-first-party `ANTHROPIC_BASE_URL`, or a Microsoft Foundry deployment hosted on Azure.
`true`| Tool search is always on, except on a Microsoft Foundry deployment hosted on Azure, where the server-side rejection still forces upfront loading, and on Google Cloud’s Agent Platform models earlier than the Claude 4.5 generation, where the SDK keeps loading tool definitions upfront. The SDK sends the beta header through proxies, and requests fail on proxies that don’t support `tool_reference` blocks.
-`auto`| Checks the combined token count of all tool definitions against the model’s context window. If they exceed 10%, tool search activates. If they’re under 10%, all tools are loaded into context normally.
-`auto:N`| Same as `auto` with a custom percentage. `auto:5` activates when tool definitions exceed 5% of the context window. Lower values activate sooner.
+`auto`| Counts the tokens in the tool definitions that tool search can defer and compares the total against the model’s context window. When the total reaches 10% of the window, tool search activates. Below that, the SDK loads every tool definition into context upfront.
+`auto:N`| Same as `auto` with a custom percentage. `auto:5` activates when those definitions reach 5% of the context window. Lower values activate sooner.
`false`| Tool search is off. All tool definitions are loaded into context on every turn.
-Setting [`CLAUDE_CODE_DISABLE_EXPERIMENTAL_BETAS`](</docs/en/env-vars>) keeps tool search off. You can’t override it by setting `ENABLE_TOOL_SEARCH` yourself. Your organization can keep tool search on through [managed settings](</docs/en/settings#settings-files>), on Claude Code v2.1.227 or later. [Disable pre-release capabilities](</docs/en/llm-gateway-protocol#disable-pre-release-capabilities>) covers where the override applies and what the variable strips. Tool search applies to all registered tools, whether they come from remote MCP servers or [custom SDK MCP servers](</docs/en/agent-sdk/custom-tools>). When using `auto`, the threshold is based on the combined size of all tool definitions across all servers. Set the value in the `env` option on `query()`. In TypeScript, `env` replaces the subprocess environment, so spread `...process.env` to keep inherited variables. In Python, `env` is merged on top of the inherited environment. This example connects to a remote MCP server that exposes many tools, pre-approves all of them with a wildcard, and uses `auto:5` so tool search activates when their definitions exceed 5% of the context window:
+Setting [`CLAUDE_CODE_DISABLE_EXPERIMENTAL_BETAS`](</docs/en/env-vars>) keeps tool search off. You can’t override it by setting `ENABLE_TOOL_SEARCH` yourself. Your organization can keep tool search on through [managed settings](</docs/en/settings#settings-files>), on Claude Code v2.1.227 or later. [Disable pre-release capabilities](</docs/en/llm-gateway-protocol#disable-pre-release-capabilities>) covers where the override applies and what the variable strips. Tool search applies to all registered tools, whether they come from remote MCP servers or [custom SDK MCP servers](</docs/en/agent-sdk/custom-tools>). When you use `auto`, the SDK counts every definition that tool search can defer toward one combined threshold: each MCP tool that isn’t marked [`alwaysLoad`](</docs/en/mcp#exempt-a-server-from-deferral>), from any server, plus the built-in tools that load on demand. The SDK always loads core built-in tools such as Bash, Read, and Edit upfront and doesn’t count them toward the threshold. Set the value in the `env` option on `query()`. In TypeScript, `env` replaces the subprocess environment, so spread `...process.env` to keep inherited variables. In Python, `env` is merged on top of the inherited environment. This example connects to a remote MCP server that exposes many tools, pre-approves all of them with a wildcard, and uses `auto:5` so tool search activates when the definitions it can defer reach 5% of the context window:
TypeScript
@@ -56,7 +56,7 @@
allowedTools: ["mcp__enterprise-tools__*"], // Wildcard pre-approves all tools from this server
env: {
...process.env, // env replaces the subprocess environment, so keep inherited variables
- ENABLE_TOOL_SEARCH: "auto:5" // Activate tool search when tools exceed 5% of context
+ ENABLE_TOOL_SEARCH: "auto:5" // Activate tool search when deferrable definitions reach 5% of context
}
}
})) {
@@ -84,7 +84,7 @@
"mcp__enterprise-tools__*"
], # Wildcard pre-approves all tools from this server
env={
- "ENABLE_TOOL_SEARCH": "auto:5" # Activate tool search when tools exceed 5% of context
+ "ENABLE_TOOL_SEARCH": "auto:5" # Activate tool search when deferrable definitions reach 5% of context
},
)
--- a/tool-search.md
+++ b/tool-search.md
@@ -2,8 +2,6 @@
* **Context efficiency:** Tool definitions can consume large portions of the context window (50 tools can use 10-20K tokens), leaving less room for actual work.
* **Tool selection accuracy:** Tool selection accuracy degrades with more than 30-50 tools loaded at once.
-
-Tool search is enabled by default.
##
@@ -11,7 +9,7 @@
How tool search works
-When tool search is active, tool definitions are withheld from the context window. The agent receives a summary of available tools and searches for relevant ones when the task requires a capability not already loaded. Up to five of the most relevant tools are loaded into context by default, where they stay available for subsequent turns. If the conversation is long enough that the SDK compacts earlier messages to free space, previously discovered tools may be removed, and the agent searches again as needed. Tool search adds one extra round-trip the first time Claude discovers a tool (the search step), but for large tool sets this is offset by smaller context on every turn. With fewer than ~10 tools, loading everything upfront is typically faster. For details on the underlying API mechanism, see [Tool search in the API](<https://platform.claude.com/docs/en/agents-and-tools/tool-use/tool-search-tool>).
+Tool search is on by default, with the exceptions listed in Configure tool search. When it is active, tool definitions are withheld from the context window. The agent receives a summary of available tools and searches for relevant ones when the task requires a capability not already loaded. Up to five of the most relevant tools are loaded into context by default, where they stay available for subsequent turns. If the conversation is long enough that the SDK compacts earlier messages to free space, previously discovered tools may be removed, and the agent searches again as needed. Tool search adds one extra round-trip the first time Claude discovers a tool (the search step), but for large tool sets this is offset by smaller context on every turn. With fewer than ~10 tools whose definitions fit comfortably in the context window, loading everything upfront is typically faster. For details on the underlying API mechanism, see [Tool search in the API](<https://platform.claude.com/docs/en/agents-and-tools/tool-use/tool-search-tool>).
Tool search isn’t supported on Microsoft Foundry [deployments hosted on Azure](<https://platform.claude.com/docs/en/build-with-claude/claude-in-microsoft-foundry#hosting-options>), which reject it server-side: the SDK detects the rejection and loads tool definitions upfront for that deployment instead. `ENABLE_TOOL_SEARCH` can’t override this, since the rejection comes from the deployment itself.
@@ -36,7 +34,7 @@
`auto:N`| Same as `auto` with a custom percentage. `auto:5` activates when tool definitions exceed 5% of the context window. Lower values activate sooner.
`false`| Tool search is off. All tool definitions are loaded into context on every turn.
-Setting [`CLAUDE_CODE_DISABLE_EXPERIMENTAL_BETAS`](</docs/en/env-vars>) keeps tool search off, and `ENABLE_TOOL_SEARCH` can’t override it. The variable strips the beta header that `defer_loading` tool definitions and `tool_reference` content blocks require. Tool search applies to all registered tools, whether they come from remote MCP servers or [custom SDK MCP servers](</docs/en/agent-sdk/custom-tools>). When using `auto`, the threshold is based on the combined size of all tool definitions across all servers. Set the value in the `env` option on `query()`. In TypeScript, `env` replaces the subprocess environment, so spread `...process.env` to keep inherited variables. In Python, `env` is merged on top of the inherited environment. This example connects to a remote MCP server that exposes many tools, pre-approves all of them with a wildcard, and uses `auto:5` so tool search activates when their definitions exceed 5% of the context window:
+Setting [`CLAUDE_CODE_DISABLE_EXPERIMENTAL_BETAS`](</docs/en/env-vars>) keeps tool search off. You can’t override it by setting `ENABLE_TOOL_SEARCH` yourself. Your organization can keep tool search on through [managed settings](</docs/en/settings#settings-files>), on Claude Code v2.1.227 or later. [Disable pre-release capabilities](</docs/en/llm-gateway-protocol#disable-pre-release-capabilities>) covers where the override applies and what the variable strips. Tool search applies to all registered tools, whether they come from remote MCP servers or [custom SDK MCP servers](</docs/en/agent-sdk/custom-tools>). When using `auto`, the threshold is based on the combined size of all tool definitions across all servers. Set the value in the `env` option on `query()`. In TypeScript, `env` replaces the subprocess environment, so spread `...process.env` to keep inherited variables. In Python, `env` is merged on top of the inherited environment. This example connects to a remote MCP server that exposes many tools, pre-approves all of them with a wildcard, and uses `auto:5` so tool search activates when their definitions exceed 5% of the context window:
TypeScript
--- a/tool-search.md
+++ b/tool-search.md
@@ -13,7 +13,7 @@
When tool search is active, tool definitions are withheld from the context window. The agent receives a summary of available tools and searches for relevant ones when the task requires a capability not already loaded. Up to five of the most relevant tools are loaded into context by default, where they stay available for subsequent turns. If the conversation is long enough that the SDK compacts earlier messages to free space, previously discovered tools may be removed, and the agent searches again as needed. Tool search adds one extra round-trip the first time Claude discovers a tool (the search step), but for large tool sets this is offset by smaller context on every turn. With fewer than ~10 tools, loading everything upfront is typically faster. For details on the underlying API mechanism, see [Tool search in the API](<https://platform.claude.com/docs/en/agents-and-tools/tool-use/tool-search-tool>).
-Tool search is supported on Claude Sonnet 4.5, Claude Haiku 4.5, Claude Opus 4.5, and later models; see [model compatibility in the API docs](<https://platform.claude.com/docs/en/agents-and-tools/tool-use/tool-search-tool#model-compatibility>) for the current list. The same minimums apply on Google Cloud’s Agent Platform, where the SDK loads tool definitions upfront for earlier models instead, because their serving stacks reject the required beta header.Tool search also isn’t supported on Microsoft Foundry [deployments hosted on Azure](<https://platform.claude.com/docs/en/build-with-claude/claude-in-microsoft-foundry#hosting-options>), which reject it server-side: the SDK detects the rejection and loads tool definitions upfront for that deployment instead. `ENABLE_TOOL_SEARCH` can’t override this, since the rejection comes from the deployment itself.
+Tool search isn’t supported on Microsoft Foundry [deployments hosted on Azure](<https://platform.claude.com/docs/en/build-with-claude/claude-in-microsoft-foundry#hosting-options>), which reject it server-side: the SDK detects the rejection and loads tool definitions upfront for that deployment instead. `ENABLE_TOOL_SEARCH` can’t override this, since the rejection comes from the deployment itself.
##
@@ -103,7 +103,7 @@
asyncio.run(main())
-To run this example, replace `https://tools.example.com/mcp` with the URL of your own MCP server. On success the result text prints to the console. Because this is a single-shot `query()` call, the SDK raises after yielding an error result, so the example wraps the loop in a try block. To see why a run failed, check the result message’s `subtype`, such as `error_during_execution`, inside the loop. For more on result messages, see [Handle the result](</docs/en/agent-sdk/agent-loop#handle-the-result>). Setting `ENABLE_TOOL_SEARCH` to `"false"` disables tool search and loads all tool definitions into context on every turn. This removes the search round-trip, which can be faster when the tool set is small (fewer than ~10 tools) and the definitions fit comfortably in the context window.
+To run this example, replace `https://tools.example.com/mcp` with the URL of your own MCP server. On success the result text prints to the console. Because this is a single-shot `query()` call, the SDK raises after yielding an error result, so the example wraps the loop in a try block. To see why a run failed, check the result message’s `subtype`, such as `error_during_execution`, inside the loop. For more on result messages, see [Handle the result](</docs/en/agent-sdk/agent-loop#handle-the-result>).
##
--- a/tool-search.md
+++ b/tool-search.md
@@ -13,7 +13,7 @@
When tool search is active, tool definitions are withheld from the context window. The agent receives a summary of available tools and searches for relevant ones when the task requires a capability not already loaded. Up to five of the most relevant tools are loaded into context by default, where they stay available for subsequent turns. If the conversation is long enough that the SDK compacts earlier messages to free space, previously discovered tools may be removed, and the agent searches again as needed. Tool search adds one extra round-trip the first time Claude discovers a tool (the search step), but for large tool sets this is offset by smaller context on every turn. With fewer than ~10 tools, loading everything upfront is typically faster. For details on the underlying API mechanism, see [Tool search in the API](<https://platform.claude.com/docs/en/agents-and-tools/tool-use/tool-search-tool>).
-Tool search is supported on Claude Sonnet 4.5, Claude Haiku 4.5, Claude Opus 4.5, and later models; see [model compatibility in the API docs](<https://platform.claude.com/docs/en/agents-and-tools/tool-use/tool-search-tool#model-compatibility>) for the current list. On Google Cloud’s Agent Platform, the minimum supported models are Claude Sonnet 4.5 and Claude Opus 4.5.Tool search also isn’t supported on Microsoft Foundry [deployments hosted on Azure](<https://platform.claude.com/docs/en/build-with-claude/claude-in-microsoft-foundry#hosting-options>), which reject it server-side: the SDK detects the rejection and loads tool definitions upfront for that deployment instead. `ENABLE_TOOL_SEARCH` can’t override this, since the rejection comes from the deployment itself.
+Tool search is supported on Claude Sonnet 4.5, Claude Haiku 4.5, Claude Opus 4.5, and later models; see [model compatibility in the API docs](<https://platform.claude.com/docs/en/agents-and-tools/tool-use/tool-search-tool#model-compatibility>) for the current list. The same minimums apply on Google Cloud’s Agent Platform, where the SDK loads tool definitions upfront for earlier models instead, because their serving stacks reject the required beta header.Tool search also isn’t supported on Microsoft Foundry [deployments hosted on Azure](<https://platform.claude.com/docs/en/build-with-claude/claude-in-microsoft-foundry#hosting-options>), which reject it server-side: the SDK detects the rejection and loads tool definitions upfront for that deployment instead. `ENABLE_TOOL_SEARCH` can’t override this, since the rejection comes from the deployment itself.
##
@@ -21,12 +21,17 @@
Configure tool search
-Tool search is on by default. It is disabled by default on Google Cloud’s Agent Platform, where it is supported for Claude Sonnet 4.5 and later and Claude Opus 4.5 and later. It is also disabled when `ANTHROPIC_BASE_URL` points to a non-first-party host, since most proxies do not forward `tool_reference` blocks. You can override either default with the `ENABLE_TOOL_SEARCH` environment variable:
+Tool search is on by default. For models on the SDK’s unsupported-model list, the SDK loads tool definitions upfront instead, and no `ENABLE_TOOL_SEARCH` value overrides that. On Google Cloud’s Agent Platform, the SDK decides by model generation:
+
+ * **Claude Opus 4.5, Sonnet 4.5, Haiku 4.5, and later** : tool search is on by default.
+ * **Earlier Agent Platform models** : the SDK loads tool definitions upfront, because their serving stacks reject the required beta header. `ENABLE_TOOL_SEARCH` can’t override this.
+
+Before Claude Code v2.1.221, the SDK disabled tool search for all models on Google Cloud’s Agent Platform unless you set `ENABLE_TOOL_SEARCH`. The SDK also disables tool search when `ANTHROPIC_BASE_URL` points to a non-first-party host, since most proxies don’t forward `tool_reference` blocks. You can override that default with the `ENABLE_TOOL_SEARCH` environment variable:
Value| Behavior
---|---
-(unset)| Tool search is on. Tool definitions are deferred and discovered on demand. Falls back to loading upfront on Google Cloud’s Agent Platform, a non-first-party `ANTHROPIC_BASE_URL`, or a Microsoft Foundry deployment hosted on Azure.
-`true`| Tool search is always on, except on a Microsoft Foundry deployment hosted on Azure, where the server-side rejection still forces upfront loading. The SDK sends the beta header even on Google Cloud’s Agent Platform and through proxies. Requests fail on Google Cloud’s Agent Platform models earlier than Sonnet 4.5 or Opus 4.5, or on proxies that do not support `tool_reference` blocks.
+(unset)| Tool search is on. Tool definitions are deferred and discovered on demand. Falls back to loading upfront on Google Cloud’s Agent Platform models earlier than the Claude 4.5 generation, a non-first-party `ANTHROPIC_BASE_URL`, or a Microsoft Foundry deployment hosted on Azure.
+`true`| Tool search is always on, except on a Microsoft Foundry deployment hosted on Azure, where the server-side rejection still forces upfront loading, and on Google Cloud’s Agent Platform models earlier than the Claude 4.5 generation, where the SDK keeps loading tool definitions upfront. The SDK sends the beta header through proxies, and requests fail on proxies that don’t support `tool_reference` blocks.
`auto`| Checks the combined token count of all tool definitions against the model’s context window. If they exceed 10%, tool search activates. If they’re under 10%, all tools are loaded into context normally.
`auto:N`| Same as `auto` with a custom percentage. `auto:5` activates when tool definitions exceed 5% of the context window. Lower values activate sooner.
`false`| Tool search is off. All tool definitions are loaded into context on every turn.
@@ -138,7 +143,7 @@
* **Maximum tools:** 10,000 tools in your catalog
* **Search results:** returns up to five most relevant tools per search by default
- * **Model support:** Claude Sonnet 4.5, Claude Haiku 4.5, Claude Opus 4.5, and later models; see [model compatibility in the API docs](<https://platform.claude.com/docs/en/agents-and-tools/tool-use/tool-search-tool#model-compatibility>) for the current list. On Google Cloud’s Agent Platform, Claude Sonnet 4.5 and later and Claude Opus 4.5 and later.
+ * **Model support:** Claude Sonnet 4.5, Claude Haiku 4.5, Claude Opus 4.5, and later models; see [model compatibility in the API docs](<https://platform.claude.com/docs/en/agents-and-tools/tool-use/tool-search-tool#model-compatibility>) for the current list. The same minimums apply on Google Cloud’s Agent Platform.
##
--- a/tool-search.md
+++ b/tool-search.md
@@ -13,7 +13,7 @@
When tool search is active, tool definitions are withheld from the context window. The agent receives a summary of available tools and searches for relevant ones when the task requires a capability not already loaded. Up to five of the most relevant tools are loaded into context by default, where they stay available for subsequent turns. If the conversation is long enough that the SDK compacts earlier messages to free space, previously discovered tools may be removed, and the agent searches again as needed. Tool search adds one extra round-trip the first time Claude discovers a tool (the search step), but for large tool sets this is offset by smaller context on every turn. With fewer than ~10 tools, loading everything upfront is typically faster. For details on the underlying API mechanism, see [Tool search in the API](<https://platform.claude.com/docs/en/agents-and-tools/tool-use/tool-search-tool>).
-Tool search is supported on Claude Sonnet 4.5, Claude Haiku 4.5, Claude Opus 4.5, and later models; see [model compatibility in the API docs](<https://platform.claude.com/docs/en/agents-and-tools/tool-use/tool-search-tool#model-compatibility>) for the current list. On Google Cloud’s Agent Platform, the minimum supported models are Claude Sonnet 4.5 and Claude Opus 4.5.
+Tool search is supported on Claude Sonnet 4.5, Claude Haiku 4.5, Claude Opus 4.5, and later models; see [model compatibility in the API docs](<https://platform.claude.com/docs/en/agents-and-tools/tool-use/tool-search-tool#model-compatibility>) for the current list. On Google Cloud’s Agent Platform, the minimum supported models are Claude Sonnet 4.5 and Claude Opus 4.5.Tool search also isn’t supported on Microsoft Foundry [deployments hosted on Azure](<https://platform.claude.com/docs/en/build-with-claude/claude-in-microsoft-foundry#hosting-options>), which reject it server-side: the SDK detects the rejection and loads tool definitions upfront for that deployment instead. `ENABLE_TOOL_SEARCH` can’t override this, since the rejection comes from the deployment itself.
##
@@ -25,8 +25,8 @@
Value| Behavior
---|---
-(unset)| Tool search is on. Tool definitions are deferred and discovered on demand. Falls back to loading upfront on Google Cloud’s Agent Platform or a non-first-party `ANTHROPIC_BASE_URL`.
-`true`| Tool search is always on. The SDK sends the beta header even on Google Cloud’s Agent Platform and through proxies. Requests fail on Google Cloud’s Agent Platform models earlier than Sonnet 4.5 or Opus 4.5, or on proxies that do not support `tool_reference` blocks.
+(unset)| Tool search is on. Tool definitions are deferred and discovered on demand. Falls back to loading upfront on Google Cloud’s Agent Platform, a non-first-party `ANTHROPIC_BASE_URL`, or a Microsoft Foundry deployment hosted on Azure.
+`true`| Tool search is always on, except on a Microsoft Foundry deployment hosted on Azure, where the server-side rejection still forces upfront loading. The SDK sends the beta header even on Google Cloud’s Agent Platform and through proxies. Requests fail on Google Cloud’s Agent Platform models earlier than Sonnet 4.5 or Opus 4.5, or on proxies that do not support `tool_reference` blocks.
`auto`| Checks the combined token count of all tool definitions against the model’s context window. If they exceed 10%, tool search activates. If they’re under 10%, all tools are loaded into context normally.
`auto:N`| Same as `auto` with a custom percentage. `auto:5` activates when tool definitions exceed 5% of the context window. Lower values activate sooner.
`false`| Tool search is off. All tool definitions are loaded into context on every turn.
Show diff (142 lines)
--- a/tool-search.md
+++ b/tool-search.md
@@ -11,9 +11,9 @@
How tool search works
-When tool search is active, tool definitions are withheld from the context window. The agent receives a summary of available tools and searches for relevant ones when the task requires a capability not already loaded. The 3-5 most relevant tools are loaded into context, where they stay available for subsequent turns. If the conversation is long enough that the SDK compacts earlier messages to free space, previously discovered tools may be removed, and the agent searches again as needed. Tool search adds one extra round-trip the first time Claude discovers a tool (the search step), but for large tool sets this is offset by smaller context on every turn. With fewer than ~10 tools, loading everything upfront is typically faster. For details on the underlying API mechanism, see [Tool search in the API](<https://platform.claude.com/docs/en/agents-and-tools/tool-use/tool-search-tool>).
+When tool search is active, tool definitions are withheld from the context window. The agent receives a summary of available tools and searches for relevant ones when the task requires a capability not already loaded. Up to five of the most relevant tools are loaded into context by default, where they stay available for subsequent turns. If the conversation is long enough that the SDK compacts earlier messages to free space, previously discovered tools may be removed, and the agent searches again as needed. Tool search adds one extra round-trip the first time Claude discovers a tool (the search step), but for large tool sets this is offset by smaller context on every turn. With fewer than ~10 tools, loading everything upfront is typically faster. For details on the underlying API mechanism, see [Tool search in the API](<https://platform.claude.com/docs/en/agents-and-tools/tool-use/tool-search-tool>).
-Tool search is supported on every Claude model except Haiku.
+Tool search is supported on Claude Sonnet 4.5, Claude Haiku 4.5, Claude Opus 4.5, and later models; see [model compatibility in the API docs](<https://platform.claude.com/docs/en/agents-and-tools/tool-use/tool-search-tool#model-compatibility>) for the current list. On Google Cloud’s Agent Platform, the minimum supported models are Claude Sonnet 4.5 and Claude Opus 4.5.
##
@@ -31,7 +31,7 @@
`auto:N`| Same as `auto` with a custom percentage. `auto:5` activates when tool definitions exceed 5% of the context window. Lower values activate sooner.
`false`| Tool search is off. All tool definitions are loaded into context on every turn.
-Tool search applies to all registered tools, whether they come from remote MCP servers or [custom SDK MCP servers](</docs/en/agent-sdk/custom-tools>). When using `auto`, the threshold is based on the combined size of all tool definitions across all servers. Set the value in the `env` option on `query()`. This example connects to a remote MCP server that exposes many tools, pre-approves all of them with a wildcard, and uses `auto:5` so tool search activates when their definitions exceed 5% of the context window:
+Setting [`CLAUDE_CODE_DISABLE_EXPERIMENTAL_BETAS`](</docs/en/env-vars>) keeps tool search off, and `ENABLE_TOOL_SEARCH` can’t override it. The variable strips the beta header that `defer_loading` tool definitions and `tool_reference` content blocks require. Tool search applies to all registered tools, whether they come from remote MCP servers or [custom SDK MCP servers](</docs/en/agent-sdk/custom-tools>). When using `auto`, the threshold is based on the combined size of all tool definitions across all servers. Set the value in the `env` option on `query()`. In TypeScript, `env` replaces the subprocess environment, so spread `...process.env` to keep inherited variables. In Python, `env` is merged on top of the inherited environment. This example connects to a remote MCP server that exposes many tools, pre-approves all of them with a wildcard, and uses `auto:5` so tool search activates when their definitions exceed 5% of the context window:
TypeScript
@@ -39,25 +39,31 @@
import { query } from "@anthropic-ai/claude-agent-sdk";
- for await (const message of query({
- prompt: "Find and run the appropriate database query",
- options: {
- mcpServers: {
- "enterprise-tools": {
- // Connect to a remote MCP server
- type: "http",
- url: "https://tools.example.com/mcp"
+ try {
+ for await (const message of query({
+ prompt: "Find and run the appropriate database query",
+ options: {
+ mcpServers: {
+ "enterprise-tools": {
+ // Connect to a remote MCP server
+ type: "http",
+ url: "https://tools.example.com/mcp"
+ }
+ },
+ allowedTools: ["mcp__enterprise-tools__*"], // Wildcard pre-approves all tools from this server
+ env: {
+ ...process.env, // env replaces the subprocess environment, so keep inherited variables
+ ENABLE_TOOL_SEARCH: "auto:5" // Activate tool search when tools exceed 5% of context
}
- },
- allowedTools: ["mcp__enterprise-tools__*"], // Wildcard pre-approves all tools from this server
- env: {
- ENABLE_TOOL_SEARCH: "auto:5" // Activate tool search when tools exceed 5% of context
+ }
+ })) {
+ if (message.type === "result" && message.subtype === "success") {
+ console.log(message.result);
}
}
- })) {
- if (message.type === "result" && message.subtype === "success") {
- console.log(message.result);
- }
+ } catch (error) {
+ // A single-shot query() throws after yielding an error result
+ console.log(`Session ended with an error: ${error}`);
}
import asyncio
@@ -79,16 +85,20 @@
},
)
- async for message in query(
- prompt="Find and run the appropriate database query",
- options=options,
- ):
- if isinstance(message, ResultMessage) and message.subtype == "success":
- print(message.result)
+ try:
+ async for message in query(
+ prompt="Find and run the appropriate database query",
+ options=options,
+ ):
+ if isinstance(message, ResultMessage) and message.subtype == "success":
+ print(message.result)
+ except Exception as error:
+ # A single-shot query() raises after yielding an error result
+ print(f"Session ended with an error: {error}")
asyncio.run(main())
-Setting `ENABLE_TOOL_SEARCH` to `"false"` disables tool search and loads all tool definitions into context on every turn. This removes the search round-trip, which can be faster when the tool set is small (fewer than ~10 tools) and the definitions fit comfortably in the context window.
+To run this example, replace `https://tools.example.com/mcp` with the URL of your own MCP server. On success the result text prints to the console. Because this is a single-shot `query()` call, the SDK raises after yielding an error result, so the example wraps the loop in a try block. To see why a run failed, check the result message’s `subtype`, such as `error_during_execution`, inside the loop. For more on result messages, see [Handle the result](</docs/en/agent-sdk/agent-loop#handle-the-result>). Setting `ENABLE_TOOL_SEARCH` to `"false"` disables tool search and loads all tool definitions into context on every turn. This removes the search round-trip, which can be faster when the tool set is small (fewer than ~10 tools) and the definitions fit comfortably in the context window.
##
@@ -96,9 +106,29 @@
Optimize tool discovery
-The search mechanism matches queries against tool names and descriptions. Names like `search_slack_messages` surface for a wider range of requests than `query_slack`. Descriptions with specific keywords (“Search Slack messages by keyword, channel, or date range”) match more queries than generic ones (“Query Slack”). You can also add a system prompt section listing available tool categories. This gives the agent context about what kinds of tools are available to search for:
+The search mechanism matches queries against tool names and descriptions. Names like `search_slack_messages` surface for a wider range of requests than `query_slack`. Descriptions with specific keywords (“Search Slack messages by keyword, channel, or date range”) match more queries than generic ones (“Query Slack”). You can also add a system prompt section listing available tool categories. This gives the agent context about what kinds of tools are available to search for. Pass the text through the `systemPrompt` option in TypeScript or `system_prompt` in Python, using the `claude_code` preset with `append`, which adds your text to the preset’s prompt instead of replacing it:
- You can search for tools to interact with Slack, GitHub, and Jira.
+TypeScript
+
+Python
+
+ options: {
+ systemPrompt: {
+ type: "preset",
+ preset: "claude_code",
+ append: "You can search for tools to interact with Slack, GitHub, and Jira."
+ }
+ }
+
+ options = ClaudeAgentOptions(
+ system_prompt={
+ "type": "preset",
+ "preset": "claude_code",
+ "append": "You can search for tools to interact with Slack, GitHub, and Jira.",
+ }
+ )
+
+For the full set of system prompt options, see [Modifying system prompts](</docs/en/agent-sdk/modifying-system-prompts>).
##
@@ -107,8 +137,8 @@
Limits
* **Maximum tools:** 10,000 tools in your catalog
- * **Search results:** Returns 3-5 most relevant tools per search
- * **Model support:** every Claude model except Haiku
+ * **Search results:** returns up to five most relevant tools per search by default
+ * **Model support:** Claude Sonnet 4.5, Claude Haiku 4.5, Claude Opus 4.5, and later models; see [model compatibility in the API docs](<https://platform.claude.com/docs/en/agents-and-tools/tool-use/tool-search-tool#model-compatibility>) for the current list. On Google Cloud’s Agent Platform, Claude Sonnet 4.5 and later and Claude Opus 4.5 and later.
##
--- a/tool-search.md
+++ b/tool-search.md
@@ -60,6 +60,34 @@
}
}
+ import asyncio
+ from claude_agent_sdk import query, ClaudeAgentOptions, ResultMessage
+
+ async def main():
+ options = ClaudeAgentOptions(
+ mcp_servers={
+ "enterprise-tools": {
+ "type": "http",
+ "url": "https://tools.example.com/mcp",
+ }
+ },
+ allowed_tools=[
+ "mcp__enterprise-tools__*"
+ ], # Wildcard pre-approves all tools from this server
+ env={
+ "ENABLE_TOOL_SEARCH": "auto:5" # Activate tool search when tools exceed 5% of context
+ },
+ )
+
+ async for message in query(
+ prompt="Find and run the appropriate database query",
+ options=options,
+ ):
+ if isinstance(message, ResultMessage) and message.subtype == "success":
+ print(message.result)
+
+ asyncio.run(main())
+
Setting `ENABLE_TOOL_SEARCH` to `"false"` disables tool search and loads all tool definitions into context on every turn. This removes the search round-trip, which can be faster when the tool set is small (fewer than ~10 tools) and the definitions fit comfortably in the context window.
##
--- a/tool-search.md
+++ b/tool-search.md
@@ -21,12 +21,12 @@
Configure tool search
-Tool search is on by default. It is disabled by default on Vertex AI, where it is supported for Claude Sonnet 4.5 and later and Claude Opus 4.5 and later. It is also disabled when `ANTHROPIC_BASE_URL` points to a non-first-party host, since most proxies do not forward `tool_reference` blocks. You can override either default with the `ENABLE_TOOL_SEARCH` environment variable:
+Tool search is on by default. It is disabled by default on Google Cloud’s Agent Platform, where it is supported for Claude Sonnet 4.5 and later and Claude Opus 4.5 and later. It is also disabled when `ANTHROPIC_BASE_URL` points to a non-first-party host, since most proxies do not forward `tool_reference` blocks. You can override either default with the `ENABLE_TOOL_SEARCH` environment variable:
Value| Behavior
---|---
-(unset)| Tool search is on. Tool definitions are deferred and discovered on demand. Falls back to loading upfront on Vertex AI or a non-first-party `ANTHROPIC_BASE_URL`.
-`true`| Tool search is always on. The SDK sends the beta header even on Vertex AI and through proxies. Requests fail on Vertex AI models earlier than Sonnet 4.5 or Opus 4.5, or on proxies that do not support `tool_reference` blocks.
+(unset)| Tool search is on. Tool definitions are deferred and discovered on demand. Falls back to loading upfront on Google Cloud’s Agent Platform or a non-first-party `ANTHROPIC_BASE_URL`.
+`true`| Tool search is always on. The SDK sends the beta header even on Google Cloud’s Agent Platform and through proxies. Requests fail on Google Cloud’s Agent Platform models earlier than Sonnet 4.5 or Opus 4.5, or on proxies that do not support `tool_reference` blocks.
`auto`| Checks the combined token count of all tool definitions against the model’s context window. If they exceed 10%, tool search activates. If they’re under 10%, all tools are loaded into context normally.
`auto:N`| Same as `auto` with a custom percentage. `auto:5` activates when tool definitions exceed 5% of the context window. Lower values activate sooner.
`false`| Tool search is off. All tool definitions are loaded into context on every turn.
--- a/tool-search.md
+++ b/tool-search.md
@@ -3,7 +3,7 @@
* **Context efficiency:** Tool definitions can consume large portions of the context window (50 tools can use 10-20K tokens), leaving less room for actual work.
* **Tool selection accuracy:** Tool selection accuracy degrades with more than 30-50 tools loaded at once.
-Tool search is enabled by default. This page covers how it works, how to configure it, and how to optimize tool discovery.
+Tool search is enabled by default.
##
--- a/tool-search.md
+++ b/tool-search.md
@@ -13,7 +13,7 @@
When tool search is active, tool definitions are withheld from the context window. The agent receives a summary of available tools and searches for relevant ones when the task requires a capability not already loaded. The 3-5 most relevant tools are loaded into context, where they stay available for subsequent turns. If the conversation is long enough that the SDK compacts earlier messages to free space, previously discovered tools may be removed, and the agent searches again as needed. Tool search adds one extra round-trip the first time Claude discovers a tool (the search step), but for large tool sets this is offset by smaller context on every turn. With fewer than ~10 tools, loading everything upfront is typically faster. For details on the underlying API mechanism, see [Tool search in the API](<https://platform.claude.com/docs/en/agents-and-tools/tool-use/tool-search-tool>).
-Tool search requires Claude Sonnet 4 or later, or Claude Opus 4 or later. Haiku models do not support tool search.
+Tool search is supported on every Claude model except Haiku.
##
@@ -80,7 +80,7 @@
* **Maximum tools:** 10,000 tools in your catalog
* **Search results:** Returns 3-5 most relevant tools per search
- * **Model support:** Claude Sonnet 4 and later, Claude Opus 4 and later (no Haiku)
+ * **Model support:** every Claude model except Haiku
##
--- a/tool-search.md
+++ b/tool-search.md
@@ -1,9 +1,3 @@
-> ## Documentation Index
->
-> Fetch the complete documentation index at: <https://code.claude.com/docs/llms.txt>
->
-> Use this file to discover all available pages before exploring further.
-
Tool search enables your agent to work with hundreds or thousands of tools by dynamically discovering and loading them on demand. Instead of loading all tool definitions into the context window upfront, the agent searches your tool catalog and loads only the tools it needs. This approach solves two challenges as tool libraries scale:
* **Context efficiency:** Tool definitions can consume large portions of the context window (50 tools can use 10-20K tokens), leaving less room for actual work.
--- a/tool-search.md
+++ b/tool-search.md
@@ -27,12 +27,12 @@
Configure tool search
-Tool search is on by default. It is disabled by default on Vertex AI, which does not accept the tool search beta header, and when `ANTHROPIC_BASE_URL` points to a non-first-party host, since most proxies do not forward `tool_reference` blocks. You can override this with the `ENABLE_TOOL_SEARCH` environment variable:
+Tool search is on by default. It is disabled by default on Vertex AI, where it is supported for Claude Sonnet 4.5 and later and Claude Opus 4.5 and later. It is also disabled when `ANTHROPIC_BASE_URL` points to a non-first-party host, since most proxies do not forward `tool_reference` blocks. You can override either default with the `ENABLE_TOOL_SEARCH` environment variable:
Value| Behavior
---|---
(unset)| Tool search is on. Tool definitions are deferred and discovered on demand. Falls back to loading upfront on Vertex AI or a non-first-party `ANTHROPIC_BASE_URL`.
-`true`| Tool search is always on. The SDK sends the beta header even on Vertex AI and through proxies. Requests fail if the backend or proxy does not support `tool_reference` blocks.
+`true`| Tool search is always on. The SDK sends the beta header even on Vertex AI and through proxies. Requests fail on Vertex AI models earlier than Sonnet 4.5 or Opus 4.5, or on proxies that do not support `tool_reference` blocks.
`auto`| Checks the combined token count of all tool definitions against the model’s context window. If they exceed 10%, tool search activates. If they’re under 10%, all tools are loaded into context normally.
`auto:N`| Same as `auto` with a custom percentage. `auto:5` activates when tool definitions exceed 5% of the context window. Lower values activate sooner.
`false`| Tool search is off. All tool definitions are loaded into context on every turn.
--- a/tool-search.md
+++ b/tool-search.md
@@ -27,12 +27,12 @@
Configure tool search
-By default, tool search is always on. You can change this with the `ENABLE_TOOL_SEARCH` environment variable:
+Tool search is on by default. It is disabled by default on Vertex AI, which does not accept the tool search beta header, and when `ANTHROPIC_BASE_URL` points to a non-first-party host, since most proxies do not forward `tool_reference` blocks. You can override this with the `ENABLE_TOOL_SEARCH` environment variable:
Value| Behavior
---|---
-(unset)| Tool search is always on. Tool definitions are never loaded into context. This is the default.
-`true`| Same as unset.
+(unset)| Tool search is on. Tool definitions are deferred and discovered on demand. Falls back to loading upfront on Vertex AI or a non-first-party `ANTHROPIC_BASE_URL`.
+`true`| Tool search is always on. The SDK sends the beta header even on Vertex AI and through proxies. Requests fail if the backend or proxy does not support `tool_reference` blocks.
`auto`| Checks the combined token count of all tool definitions against the model’s context window. If they exceed 10%, tool search activates. If they’re under 10%, all tools are loaded into context normally.
`auto:N`| Same as `auto` with a custom percentage. `auto:5` activates when tool definitions exceed 5% of the context window. Lower values activate sooner.
`false`| Tool search is off. All tool definitions are loaded into context on every turn.
--- a/tool-search.md
+++ b/tool-search.md
@@ -1,3 +1,9 @@
+> ## Documentation Index
+>
+> Fetch the complete documentation index at: <https://code.claude.com/docs/llms.txt>
+>
+> Use this file to discover all available pages before exploring further.
+
Tool search enables your agent to work with hundreds or thousands of tools by dynamically discovering and loading them on demand. Instead of loading all tool definitions into the context window upfront, the agent searches your tool catalog and loads only the tools it needs. This approach solves two challenges as tool libraries scale:
* **Context efficiency:** Tool definitions can consume large portions of the context window (50 tools can use 10-20K tokens), leaving less room for actual work.
--- a/tool-search.md
+++ b/tool-search.md
@@ -0,0 +1,95 @@
+Tool search enables your agent to work with hundreds or thousands of tools by dynamically discovering and loading them on demand. Instead of loading all tool definitions into the context window upfront, the agent searches your tool catalog and loads only the tools it needs. This approach solves two challenges as tool libraries scale:
+
+ * **Context efficiency:** Tool definitions can consume large portions of the context window (50 tools can use 10-20K tokens), leaving less room for actual work.
+ * **Tool selection accuracy:** Tool selection accuracy degrades with more than 30-50 tools loaded at once.
+
+Tool search is enabled by default. This page covers how it works, how to configure it, and how to optimize tool discovery.
+
+##
+
+
+
+How tool search works
+
+When tool search is active, tool definitions are withheld from the context window. The agent receives a summary of available tools and searches for relevant ones when the task requires a capability not already loaded. The 3-5 most relevant tools are loaded into context, where they stay available for subsequent turns. If the conversation is long enough that the SDK compacts earlier messages to free space, previously discovered tools may be removed, and the agent searches again as needed. Tool search adds one extra round-trip the first time Claude discovers a tool (the search step), but for large tool sets this is offset by smaller context on every turn. With fewer than ~10 tools, loading everything upfront is typically faster. For details on the underlying API mechanism, see [Tool search in the API](<https://platform.claude.com/docs/en/agents-and-tools/tool-use/tool-search-tool>).
+
+Tool search requires Claude Sonnet 4 or later, or Claude Opus 4 or later. Haiku models do not support tool search.
+
+##
+
+
+
+Configure tool search
+
+By default, tool search is always on. You can change this with the `ENABLE_TOOL_SEARCH` environment variable:
+
+Value| Behavior
+---|---
+(unset)| Tool search is always on. Tool definitions are never loaded into context. This is the default.
+`true`| Same as unset.
+`auto`| Checks the combined token count of all tool definitions against the model’s context window. If they exceed 10%, tool search activates. If they’re under 10%, all tools are loaded into context normally.
+`auto:N`| Same as `auto` with a custom percentage. `auto:5` activates when tool definitions exceed 5% of the context window. Lower values activate sooner.
+`false`| Tool search is off. All tool definitions are loaded into context on every turn.
+
+Tool search applies to all registered tools, whether they come from remote MCP servers or [custom SDK MCP servers](</docs/en/agent-sdk/custom-tools>). When using `auto`, the threshold is based on the combined size of all tool definitions across all servers. Set the value in the `env` option on `query()`. This example connects to a remote MCP server that exposes many tools, pre-approves all of them with a wildcard, and uses `auto:5` so tool search activates when their definitions exceed 5% of the context window:
+
+TypeScript
+
+Python
+
+ import { query } from "@anthropic-ai/claude-agent-sdk";
+
+ for await (const message of query({
+ prompt: "Find and run the appropriate database query",
+ options: {
+ mcpServers: {
+ "enterprise-tools": {
+ // Connect to a remote MCP server
+ type: "http",
+ url: "https://tools.example.com/mcp"
+ }
+ },
+ allowedTools: ["mcp__enterprise-tools__*"], // Wildcard pre-approves all tools from this server
+ env: {
+ ENABLE_TOOL_SEARCH: "auto:5" // Activate tool search when tools exceed 5% of context
+ }
+ }
+ })) {
+ if (message.type === "result" && message.subtype === "success") {
+ console.log(message.result);
+ }
+ }
+
+Setting `ENABLE_TOOL_SEARCH` to `"false"` disables tool search and loads all tool definitions into context on every turn. This removes the search round-trip, which can be faster when the tool set is small (fewer than ~10 tools) and the definitions fit comfortably in the context window.
+
+##
+
+
+
+Optimize tool discovery
+
+The search mechanism matches queries against tool names and descriptions. Names like `search_slack_messages` surface for a wider range of requests than `query_slack`. Descriptions with specific keywords (“Search Slack messages by keyword, channel, or date range”) match more queries than generic ones (“Query Slack”). You can also add a system prompt section listing available tool categories. This gives the agent context about what kinds of tools are available to search for:
+
+ You can search for tools to interact with Slack, GitHub, and Jira.
+
+##
+
+
+
+Limits
+
+ * **Maximum tools:** 10,000 tools in your catalog
+ * **Search results:** Returns 3-5 most relevant tools per search
+ * **Model support:** Claude Sonnet 4 and later, Claude Opus 4 and later (no Haiku)
+
+##
+
+
+
+Related documentation
+
+ * [Tool search in the API](<https://platform.claude.com/docs/en/agents-and-tools/tool-use/tool-search-tool>): Full API documentation for tool search, including custom implementations
+ * [Connect MCP servers](</docs/en/agent-sdk/mcp>): Connect to external tools via MCP servers
+ * [Custom tools](</docs/en/agent-sdk/custom-tools>): Build your own tools with SDK MCP servers
+ * [TypeScript SDK reference](</docs/en/agent-sdk/typescript>): Full API reference
+ * [Python SDK reference](</docs/en/agent-sdk/python>): Full API reference