Tool call limit middleware for agents.
Annotation used to mark state attributes as purely internal for a given middleware.
How to handle execution when tool call limits are exceeded.
'continue': Block exceeded tools with error messages, let other tools continue
(default)'error': Raise a ToolCallLimitExceededError exception'end': Stop execution immediately, injecting a ToolMessage and an AIMessage for
the single tool call that exceeded the limit. Raises NotImplementedError if there
are other pending tool calls (due to parallel tool calling).Base middleware class for an agent.
Subclass this and implement any of the defined methods to customize agent behavior between steps in the main agent loop.
State schema for the agent.
State schema for ToolCallLimitMiddleware.
Extends AgentState with tool call tracking fields.
The count fields are dictionaries mapping tool names to execution counts. This
allows multiple middleware instances to track different tools independently. The
special key '__all__' is used for tracking all tool calls globally.
Exception raised when tool call limits are exceeded.
This exception is raised when the configured exit behavior is 'error' and either
the thread or run tool call limit has been exceeded.
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.