Add `ModelSettings['max_retries']` to set how many times the provider SDK retries a request
Adds ModelSettings['max_retries'], the per-request sibling of ModelSettings['timeout']: the number of retries the provider SDK's own client makes for one request. 0 makes a single attempt. Without the setting, nothing changes.
Because it's a setting, it works per model, per agent, per run, and per FallbackModel candidate, and you no longer have to build (and own) the SDK client just to turn its retries off. A future RetryOnModelErrors (#9334) can set it to 0 through get_model_settings to be the only retry owner.
How each model forwards it
Checked against the installed SDK source, and pinned by a new attempt-counting probe in tests/models/test_model_settings_support.py: each model is answered with a retryable error, and the probe counts how many times the SDK sends the request with no setting, max_retries=0 and max_retries=1. A test also checks that the Supported by: list matches the models whose count changed.
| Model | How | Attempts (none / 0 / 1) |
|---|---|---|
| OpenAI (Chat, Responses) and the classes built on it (Codex, Cerebras, Crusoe, GitHub Copilot, Ollama, OpenRouter, Snowflake, Z.AI, Bedrock Mantle), OpenAI Decisions | client.with_options(max_retries=...) | 3 / 1 / 2 |
Anthropic, including AsyncAnthropicBedrock and AsyncAnthropicVertex | with_options | 3 / 1 / 2 |
| Groq | with_options | 3 / 1 / 2 |
| Cohere | per-request request_options={'max_retries': ...} | 3 / 1 / 2 |
per-request http_options.retry_options with attempts = max_retries + 1 | 1 / 1 / 2, or 4 / 1 / 2 with provider retry_options | |
| Mistral | per-request retries=None for 0 only | client retry_config: several / 1 / several |
| TypeSafe | per-request retry=RetryPolicy(max_retries=...) | 3 / 1 / 2 |
| Bedrock, xAI, Hugging Face, MCP Sampling, System One | ignored (accept-and-ignore, like timeout) | n/a |
Some details:
- Stainless
with_options()and Bedrock.AsyncAnthropicBedrock.copy()passeshttp_client=http_clientrather thanhttp_client or self._client, so a plainwith_options(max_retries=...)would build a new HTTP client per request, dropping the caller's transport and connection pool. It also leaves outaws_profile, so the copy would sign requests with the default AWS credential chain. The shared helperwith_max_retries()passes the original HTTP client explicitly, andAnthropicModelpassesaws_profileback to Bedrock copies. Without these, theAnthropicModel[bedrock]probe case andtest_max_retries_keeps_the_bedrock_client_aws_profilefail. - Cohere has a per-request knob. #9331 lists Cohere as unsupported, but Cohere's Fern-generated SDK takes
request_options['max_retries']per request (cohere/core/http_client.py), so it's supported. The Cohere docs page said that knob didn't exist, so this PR corrects it. - Google: merge, not override. Per-request
retry_optionsreplace the client's outright ingoogle-genai, so the request carries the provider'sretry_options(delays, backoff,http_status_codes) with onlyattemptschanged. That keepsmax_retriesabout the count, like it is on every other SDK. Without providerretry_options, the SDK's defaults apply (first delay of 1s; status 408/429/500/502/503/504). Open question 1 on #9331, flagged below. - Mistral can only express
0. Its Speakeasy retry config is bounded by elapsed time, not by count.0turns off aretry_configset on the client; other values are ignored and documented as such. Mistral makes no retries by default. - TypeSafe takes a whole
RetryPolicyper call and keeps no readable copy of the client's, so the request uses the SDK's default policy with that count. This is documented on theSupported by:entry. - No runtime warning on models that ignore it. This follows the
timeoutprecedent (theSupported by:list plus the docs table), and #9331's open question 2. See below.
Docs
docs/retries.md: a new "Setting the SDK retry count per request" section, covering composition with transports, durable engines andFallbackModel(precedence: agent/run settings override a candidate's own), plus a per-provider table.- Provider pages (OpenAI, Anthropic, Groq, Cohere, Google, TypeSafe, xAI, Bedrock) and the
FallbackModelnote inmodels/overview.mdnow point to it. - The agent skill's
FallbackModeland transport-retry guidance mention it.
Flagged for review
- Google: merge vs override. I merged, keeping the provider's delays and status codes and changing only
attempts. The issue comment suggested override. Overriding would silently reset a user's tuned backoff and status codes to the SDK's defaults whenever they set the count, which no other provider does. - No warning on unsupported models. I followed
timeout. A warning for Bedrock and xAI would be easy to add, butRetryOnModelErrorswill setmax_retries=0on every model throughget_model_settings, so a warning would fire on every Bedrock and xAI request for users who didn't set anything themselves.
- Closes #9331
Checklist
- No breaking changes in accordance with the version policy.
- Any permitted compatibility impact has a label, warning, migration, release note, and exact API-check waiver.
- PR title is fit for the release changelog.
š¤ Generated with Claude Code