Provider-side tool search middleware.
Base middleware class for an agent.
Subclass this and implement any of the defined methods to customize agent behavior between steps in the main agent loop.
State schema for the agent.
Model request information for the agent.
Response from model execution including messages and optional structured output.
The result will usually contain a single AIMessage, but may include an additional
ToolMessage if the model used a tool for structured output.
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.
Tool name or tool instance that can be deferred behind provider tool search.