Logic to run before the agent execution starts.
Async logic to run before the agent execution starts.
Logic to run before the model is called.
Async logic to run before the model is called.
Intercept and control model execution via handler callback.
Intercept and control async model execution via handler callback.
Logic to run after the agent execution completes.
Async logic to run after the agent execution completes.
Intercept tool execution for retries, monitoring, or modification.
Intercept and control async tool execution via handler callback.
| 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: None |
exit_behavior | ExitBehavior | Default: 'continue' |
| Name | Type |
|---|---|
| tool_name | str | None |
| thread_limit | int | None |
| run_limit | int | None |
| exit_behavior | ExitBehavior |
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.
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 for each exceeded tool
call, a ToolMessage explaining any other pending calls were skipped,
and an AI message explaining why execution stoppedMaximum number of tool calls allowed per run.
None means no limit.
How to handle when limits are exceeded.
'continue': Block exceeded tools with error messages, let other
tools continue. Model decides when to end.'error': Raise a ToolCallLimitExceededError exception'end': Stop execution immediately with a ToolMessage for each
tool call that exceeded the limit and an AI message explaining
why. Any other pending tool call on the same AIMessage (e.g.
from parallel tool calling) is given an explanatory
ToolMessage instead of being executed.