langchain.js
    Preparing search index...

    Function llmToolSelectorMiddleware

    • 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.

      Parameters

      • options: InferInteropZodInput

        Configuration options for the middleware

        • model

          The language model to use for tool selection (default: the provided model from the agent options).

        • systemPrompt

          Instructions for the selection model.

        • maxTools

          Maximum number of tools to select. If the model selects more, only the first maxTools will be used. No limit if not specified.

        • alwaysInclude

          Tool names to always include regardless of selection. These do not count against the maxTools limit.

      Returns AgentMiddleware<any>

      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
      });