ProviderToolSearchMiddleware(
self,
*,
searchable_tools: list[ToolIdentifier] | None = None
)The name of the middleware instance.
Logic to run before the agent execution starts.
Async logic to run before the agent execution starts.
Logic to run before the model is called.
Async logic to run before the model is called.
Logic to run after the model is called.
Async logic to run after the model is called.
Logic to run after the agent execution completes.
Async logic to run after the agent execution completes.
Intercept tool execution for retries, monitoring, or modification.
Intercept and control async tool execution via handler callback.
| Name | Type | Description |
|---|---|---|
searchable_tools | list[ToolIdentifier] | None | Default: NoneTools or tool names to defer behind provider-native tool search. |
| Name | Type |
|---|---|
| searchable_tools | list[ToolIdentifier] | None |
Defer selected tools behind provider-native tool search.
Instead of sending every tool schema on every turn, this middleware marks
selected tools as deferred (via extras["defer_loading"]) and injects the
provider's server-side tool search tool. The provider then retrieves the
full schema of a deferred tool only when the model needs it, which keeps the
request payload small when many tools are bound.
A tool is deferred when its name (or instance) is passed in searchable_tools,
or when it already carries extras["defer_loading"] is True.
Only providers with server-side tool search are supported (currently Anthropic and OpenAI). The provider is inferred from the bound model.
This relies on provider-native tool search and only takes effect for
supported providers. If a tool is deferred but the model's provider
cannot be identified or does not support tool search, the model call
raises ValueError. When no tool is deferred, the middleware passes the
request through unchanged regardless of provider.
Example:
from langchain.agents import create_agent
from langchain.agents.middleware import ProviderToolSearchMiddleware
agent = create_agent(
"anthropic:claude-opus-4-8",
tools=[get_weather, send_email, lookup_order],
middleware=[ProviderToolSearchMiddleware(searchable_tools=["lookup_order"])],
)