generated from amazon-archives/__template_Apache-2.0
-
Notifications
You must be signed in to change notification settings - Fork 167
Add retry strategy documentation #455
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
zastrowm
merged 3 commits into
strands-agents:main
from
zastrowm:add_retry_strategy_docs
Jan 30, 2026
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| # Retry Strategies | ||
|
|
||
| Model providers occasionally encounter errors: rate limits, service unavailability, or network timeouts. By default, the agent retries `ModelThrottledException` failures automatically with exponential backoff. The `retry_strategy` parameter lets you customize this behavior. | ||
|
|
||
| ## Default Behavior | ||
|
|
||
| Without configuration, agents retry `ModelThrottledException` up to 5 times (6 total attempts) with exponential backoff starting at 4 seconds: | ||
|
|
||
| ``` | ||
| Attempt 1: fails → wait 4s | ||
| Attempt 2: fails → wait 8s | ||
| Attempt 3: fails → wait 16s | ||
| Attempt 4: fails → wait 32s | ||
| Attempt 5: fails → wait 64s | ||
| Attempt 6: fails → exception raised | ||
| ``` | ||
|
|
||
| ## Customizing Retry Behavior | ||
|
|
||
| Use `ModelRetryStrategy` to adjust the retry parameters: | ||
|
|
||
| === "Python" | ||
|
|
||
| ```python | ||
| from strands import Agent, ModelRetryStrategy | ||
|
|
||
| agent = Agent( | ||
| retry_strategy=ModelRetryStrategy( | ||
| max_attempts=3, # Total attempts (including first try) | ||
| initial_delay=2, # Seconds before first retry | ||
| max_delay=60 # Cap on backoff delay | ||
| ) | ||
| ) | ||
| ``` | ||
|
|
||
| {{ ts_not_supported_code() }} | ||
|
|
||
| ### Parameters | ||
|
|
||
| | Parameter | Type | Default | Description | | ||
| |-----------|------|---------|-------------| | ||
| | `max_attempts` | `int` | `6` | Total number of attempts including the initial call. Set to `1` to disable retries. | | ||
| | `initial_delay` | `float` | `4` | Seconds to wait before the first retry. Subsequent retries double this value. | | ||
| | `max_delay` | `float` | `128` | Maximum seconds to wait between retries. Caps the exponential growth. | | ||
|
|
||
| ## Disabling Retries | ||
|
|
||
| To disable automatic retries entirely: | ||
|
|
||
| === "Python" | ||
|
|
||
| ```python | ||
| from strands import Agent, ModelRetryStrategy | ||
|
|
||
| agent = Agent( | ||
| retry_strategy=ModelRetryStrategy(max_attempts=1) | ||
| ) | ||
| ``` | ||
|
|
||
| {{ ts_not_supported_code() }} | ||
|
|
||
| ## When Retries Occur | ||
|
|
||
| `ModelRetryStrategy` handles `ModelThrottledException`, which model providers raise for rate-limiting. Other exceptions propagate immediately without retry. | ||
|
|
||
| ## Custom Retry Logic | ||
|
|
||
| For more complex retry scenarios—retrying on different exception types, validating responses before accepting them, or implementing custom backoff algorithms—use the hooks system. The `AfterModelCallEvent` provides access to any exception that occurred, and setting `event.retry = True` triggers another attempt: | ||
zastrowm marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| === "Python" | ||
|
|
||
| ```python | ||
| from strands import Agent | ||
| from strands.hooks import HookProvider, HookRegistry, AfterModelCallEvent | ||
|
|
||
| class CustomRetry(HookProvider): | ||
| def __init__(self, max_retries: int = 3): | ||
| self.max_retries = max_retries | ||
| self.attempts = 0 | ||
|
|
||
| def register_hooks(self, registry: HookRegistry) -> None: | ||
| registry.add_callback(AfterModelCallEvent, self.maybe_retry) | ||
|
|
||
| async def maybe_retry(self, event: AfterModelCallEvent) -> None: | ||
| if event.exception and self.attempts < self.max_retries: | ||
| self.attempts += 1 | ||
| event.retry = True | ||
|
|
||
| agent = Agent(hooks=[CustomRetry()]) | ||
| ``` | ||
|
|
||
| {{ ts_not_supported_code() }} | ||
|
|
||
| See [Hooks](hooks.md#model-call-retry) for more examples. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.