Uses an LLM to select relevant tools before calling the main model.
When an agent has many tools available, this middleware filters them down to only the most relevant ones for the user's query. This reduces token usage and helps the main model focus on the right tools.
LLMToolSelectorMiddleware(
self,
*,
model: str | BaseChatModel | None = None,
system_prompt: str = DEFAULT_SYSTEM_PROMPT,
max_tools: int | None = None,
always_include: list[str] | None = None
)| Name | Type | Description |
|---|---|---|
model | str | BaseChatModel | None | Default: NoneModel to use for selection. If not provided, uses the agent's main model. Can be a model identifier string or |
system_prompt | str | Default: DEFAULT_SYSTEM_PROMPTInstructions for the selection model. |
max_tools | int | None | Default: NoneMaximum number of tools to select. If the model selects more, only the first If not specified, there is no limit. |
always_include | list[str] | None | Default: NoneTool names to always include regardless of selection. These do not count against the |
Start the shell session and run startup commands.
Async start the shell session and run startup commands.
Check model call limits before making a model call.
Async check model call limits before making a model call.
Check for parallel write_todos tool calls and return errors if detected.
Check for parallel write_todos tool calls and return errors if detected.
Run shutdown commands and release resources when an agent completes.
Async run shutdown commands and release resources when an agent completes.
Intercept tool execution for retries, monitoring, or modification.
Intercept and control async tool execution via handler callback.