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.
toolCallLimitMiddleware(
options: __type
): AgentMiddleware<ZodObject<__type, "strip", ZodTypeAny, __type, __type>, undefined, unknown, readonly ClientTool | ServerTool[]>| Name | Type | Description |
|---|---|---|
options* | __type | Configuration options for the middleware |
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}`);
}
}