Middleware that automatically retries failed model calls with configurable backoff.
Supports retrying on specific exceptions and exponential backoff.
ModelRetryMiddleware(
self,
*,
max_retries: int = 2,
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 |
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:
|
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.
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.