LangChain Reference home pageLangChain ReferenceLangChain Reference
  • GitHub
  • Main Docs
Deep Agents
LangChain
LangGraph
Integrations
LangSmith
LangChain
  • Browser
  • Universal
  • Hub
  • Node
  • Load
  • Serializable
  • Encoder Backed
  • File System
  • In Memory
LangChain Core
  • Agents
  • Caches
  • Base
  • Dispatch
  • Web
  • Manager
  • Promises
  • Chat History
  • Context
  • Base
  • Langsmith
  • Documents
  • Embeddings
  • Errors
  • Example Selectors
  • Indexing
  • Base
  • Chat Models
  • Llms
  • Profile
  • Structured Output
  • Load
  • Serializable
  • Memory
  • Messages
  • Tool
  • Output Parsers
  • Openai Functions
  • Openai Tools
  • Outputs
  • Prompt Values
  • Prompts
  • Retrievers
  • Document Compressors
  • Runnables
  • Graph
  • Singletons
  • Stores
  • Structured Query
  • Testing
  • Tools
  • Base
  • Console
  • Log Stream
  • Run Collector
  • Tracer Langchain
  • Stream
  • Async Caller
  • Chunk Array
  • Context
  • Env
  • Event Source Parse
  • Format
  • Function Calling
  • Hash
  • Json Patch
  • Json Schema
  • Math
  • Ssrf
  • Standard Schema
  • Stream
  • Testing
  • Tiktoken
  • Types
  • Vectorstores
Text Splitters
MCP Adapters
⌘I

LangChain Assistant

Ask a question to get started

Enter to send•Shift+Enter new line

Menu

LangChain
BrowserUniversalHubNodeLoadSerializableEncoder BackedFile SystemIn Memory
LangChain Core
AgentsCachesBaseDispatchWebManagerPromisesChat HistoryContextBaseLangsmithDocumentsEmbeddingsErrorsExample SelectorsIndexingBaseChat ModelsLlmsProfileStructured OutputLoadSerializableMemoryMessagesToolOutput ParsersOpenai FunctionsOpenai ToolsOutputsPrompt ValuesPromptsRetrieversDocument CompressorsRunnablesGraphSingletonsStoresStructured QueryTestingToolsBaseConsoleLog StreamRun CollectorTracer LangchainStreamAsync CallerChunk ArrayContextEnvEvent Source ParseFormatFunction CallingHashJson PatchJson SchemaMathSsrfStandard SchemaStreamTestingTiktokenTypesVectorstores
Text Splitters
MCP Adapters
Language
Theme
JavaScriptlangchainbrowsertoolCallLimitMiddleware
Functionā—Since v1.2

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.

Copy
toolCallLimitMiddleware(
  options: __type
): AgentMiddleware<ZodObject<__type, "strip", ZodTypeAny, __type, __type>, undefined, unknown, readonly ClientTool | ServerTool[]>

Used in Docs

  • Prebuilt middleware
  • Subgraphs

Parameters

NameTypeDescription
options*__type

Configuration options for the middleware

Example 1

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

Example 2

Copy
// End execution immediately with an AI message
const limiter = toolCallLimitMiddleware({
  runLimit: 5,
  exitBehavior: "end"
});

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

Example 3

Copy
// 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}`);
  }
}
View source on GitHub