Middleware that automatically retries failed tool calls with configurable backoff.
Supports retrying on specific exceptions and exponential backoff.
ToolRetryMiddleware(
self,
*,
max_retries: int = 2,
tools: list[BaseTool | str] | None = None,
retry_on: RetryOn = (Exception,),
on_failure: OnFailure = 'continue',
backoff_factor: float = 2.0,
initial_delay: float = 1.0,
max_delay: float = 60.0,
jitter: bool = True
)| Name | Type | Description |
|---|---|---|
max_retries | int | Default: 2Maximum number of retry attempts after the initial call. Must be |
tools | list[BaseTool | str] | None | Default: NoneOptional list of tools or tool names to apply retry logic to. Can be a list of If |
retry_on | RetryOn | Default: (Exception,)Either a tuple of exception types to retry on, or a callable
that takes an exception and returns Default is to retry on all exceptions. |
on_failure | OnFailure | Default: 'continue'Behavior when all retries are exhausted. Options:
Deprecated values (for backwards compatibility):
|
backoff_factor | float | Default: 2.0Multiplier for exponential backoff. Each retry waits Set to |
initial_delay | float | Default: 1.0Initial delay in seconds before first retry. |
max_delay | float | Default: 60.0Maximum delay in seconds between retries. Caps exponential backoff growth. |
jitter | bool | Default: TrueWhether to add random jitter ( |
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.
Logic to run after the model is called.
Async logic to run after 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.