Middleware for selecting tools using an LLM-based strategy.
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(options: __type): AgentMiddleware<StateDefinitionInit | undefined>| Name | Type | Description |
|---|---|---|
options* | __type | Configuration options for the middleware |
Limit to 3 tools:
import { llmToolSelectorMiddleware } from "langchain/agents/middleware";
const middleware = llmToolSelectorMiddleware({ maxTools: 3 });
const agent = createAgent({
model: "openai:gpt-4o",
tools: [tool1, tool2, tool3, tool4, tool5],
middleware: [middleware],
});Use a smaller model for selection:
const middleware = llmToolSelectorMiddleware({
model: "openai:gpt-4o-mini",
maxTools: 2
});