langchain.js
    Preparing search index...

    Function toolCallLimitMiddleware

    • Middleware that tracks tool call counts and enforces limits.

      This middleware monitors the number of tool calls made during agent execution and can terminate the agent when specified limits are reached. It supports both thread-level and run-level call counting with configurable exit behaviors.

      Thread-level: The middleware counts all tool calls in the entire message history and persists this count across multiple runs (invocations) of the agent.

      Run-level: The middleware counts tool calls made after the last HumanMessage, representing the current run (invocation) of the agent.

      Parameters

      • options: InferInteropZodInput

        Configuration options for the middleware

        • toolName

          Name of the specific tool to limit. If undefined, limits apply to all tools.

        • threadLimit

          Maximum number of tool calls allowed per thread. undefined means no limit.

        • runLimit

          Maximum number of tool calls allowed per run. undefined means no limit.

        • exitBehavior

          What to do when limits are exceeded.

          • "continue": Block exceeded tools with error messages, let other tools continue. Model decides when to end. (default)
          • "error": Raise a ToolCallLimitExceededError exception
          • "end": Stop execution immediately with a ToolMessage + AI message for the single tool call that exceeded the limit. Raises NotImplementedError if there are multiple tool calls.

      Returns AgentMiddleware<
          ZodObject<
              {
                  runToolCallCount: ZodDefault<ZodRecord<ZodString, ZodNumber>>;
                  threadToolCallCount: ZodDefault<ZodRecord<ZodString, ZodNumber>>;
              },
              "strip",
              ZodTypeAny,
              {
                  runToolCallCount: Record<string, number>;
                  threadToolCallCount: Record<string, number>;
              },
              {
                  runToolCallCount?: Record<string, number>;
                  threadToolCallCount?: Record<string, number>;
              },
          >,
          undefined,
          any,
      >

      If both limits are undefined, if exitBehavior is invalid, or if runLimit exceeds threadLimit.

      If exitBehavior is "end" and there are multiple tool calls.

      import { toolCallLimitMiddleware } from "@langchain/langchain/agents/middleware";
      import { createAgent } from "@langchain/langchain/agents";

      // Block exceeded tools but let other tools and model continue
      const limiter = toolCallLimitMiddleware({
      threadLimit: 20,
      runLimit: 10,
      exitBehavior: "continue", // default
      });

      const agent = createAgent({
      model: "openai:gpt-4o",
      middleware: [limiter]
      });
      // End execution immediately with an AI message
      const limiter = toolCallLimitMiddleware({
      runLimit: 5,
      exitBehavior: "end"
      });

      const agent = createAgent({
      model: "openai:gpt-4o",
      middleware: [limiter]
      });
      // Strict limit with exception handling
      const limiter = toolCallLimitMiddleware({
      toolName: "search",
      threadLimit: 5,
      exitBehavior: "error"
      });

      const agent = createAgent({
      model: "openai:gpt-4o",
      middleware: [limiter]
      });

      try {
      const result = await agent.invoke({ messages: [new HumanMessage("Task")] });
      } catch (error) {
      if (error instanceof ToolCallLimitExceededError) {
      console.log(`Search limit exceeded: ${error}`);
      }
      }