Track tool call counts and enforces limits during agent execution.
This middleware monitors the number of tool calls made and can terminate or restrict execution when limits are exceeded. It supports both thread-level (persistent across runs) and run-level (per invocation) call counting.
ToolCallLimitMiddleware(
self,
*,
tool_name: str | None = None,
thread_limit: int | None = None,
run_limit: int | None = None,
exit_behavior: ExitBehavior = 'continue'
)Configuration:
exit_behavior: How to handle when limits are exceeded'continue': Block exceeded tools, let execution continue (default)'error': Raise an exception'end': Stop immediately with a ToolMessage + AI message for the single
tool call that exceeded the limit (raises NotImplementedError if there
are other pending tool calls (due to parallel tool calling).| Name | Type | Description |
|---|---|---|
tool_name | str | None | Default: NoneName of the specific tool to limit. If |
thread_limit | int | None | Default: NoneMaximum number of tool calls allowed per thread.
|
run_limit | int | None | Default: NoneMaximum number of tool calls allowed per run.
|
exit_behavior | ExitBehavior | Default: 'continue'How to handle when limits are exceeded.
|
| Name | Type |
|---|---|
| tool_name | str | None |
| thread_limit | int | None |
| run_limit | int | None |
| exit_behavior | ExitBehavior |
The name of the middleware instance.
Includes the tool name if specified to allow multiple instances of this middleware with different tool names.
Start the shell session and run startup commands.
Async start the shell session and run startup commands.
Check model call limits before making a model call.
Async check model call limits before making a model call.
Update the system message to include the todo system prompt.
Update the system message to include the todo system prompt.
Run shutdown commands and release resources when an agent completes.
Async run shutdown commands and release resources when an agent completes.
Intercept tool execution for retries, monitoring, or modification.
Intercept and control async tool execution via handler callback.