Configuration options for the middleware
The language model to use for tool selection (default: the provided model from the agent options).
System prompt for the tool selection model.
Maximum number of tools to select.
Whether to include the full conversation history in the tool selection prompt.
Maximum number of retries if the model selects incorrect tools.
import { llmToolSelectorMiddleware } from "langchain/agents/middleware";
const middleware = llmToolSelectorMiddleware({
maxTools: 3,
systemPrompt: "Select the most relevant tools for the user's query."
});
const agent = createAgent({
model: "openai:gpt-4o",
tools: [tool1, tool2, tool3, tool4, tool5],
middleware: [middleware],
});
Middleware for selecting tools using an LLM-based strategy.
This middleware analyzes the user's query and available tools to select the most relevant tools for the task, reducing the cognitive load on the main model and improving response quality.