Configuration options for the middleware
Name of the specific tool to limit. If undefined, limits apply to all tools.
Maximum number of tool calls allowed per thread. undefined means no limit.
Maximum number of tool calls allowed per run. undefined means no limit.
What to do when limits are exceeded.
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}`);
}
}
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.