Add `ModelSettings['max_retries']` to set how many times the provider SDK retries a request

Merged
DO
2mo ago
+495-43

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.

ModelHowAttempts (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 Decisionsclient.with_options(max_retries=...)3 / 1 / 2
Anthropic, including AsyncAnthropicBedrock and AsyncAnthropicVertexwith_options3 / 1 / 2
Groqwith_options3 / 1 / 2
Cohereper-request request_options={'max_retries': ...}3 / 1 / 2
Googleper-request http_options.retry_options with attempts = max_retries + 11 / 1 / 2, or 4 / 1 / 2 with provider retry_options
Mistralper-request retries=None for 0 onlyclient retry_config: several / 1 / several
TypeSafeper-request retry=RetryPolicy(max_retries=...)3 / 1 / 2
Bedrock, xAI, Hugging Face, MCP Sampling, System Oneignored (accept-and-ignore, like timeout)n/a

Some details:

  • Stainless with_options() and Bedrock. AsyncAnthropicBedrock.copy() passes http_client=http_client rather than http_client or self._client, so a plain with_options(max_retries=...) would build a new HTTP client per request, dropping the caller's transport and connection pool. It also leaves out aws_profile, so the copy would sign requests with the default AWS credential chain. The shared helper with_max_retries() passes the original HTTP client explicitly, and AnthropicModel passes aws_profile back to Bedrock copies. Without these, the AnthropicModel[bedrock] probe case and test_max_retries_keeps_the_bedrock_client_aws_profile fail.
  • 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_options replace the client's outright in google-genai, so the request carries the provider's retry_options (delays, backoff, http_status_codes) with only attempts changed. That keeps max_retries about the count, like it is on every other SDK. Without provider retry_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. 0 turns off a retry_config set on the client; other values are ignored and documented as such. Mistral makes no retries by default.
  • TypeSafe takes a whole RetryPolicy per 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 the Supported by: entry.
  • No runtime warning on models that ignore it. This follows the timeout precedent (the Supported 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 and FallbackModel (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 FallbackModel note in models/overview.md now point to it.
  • The agent skill's FallbackModel and transport-retry guidance mention it.

Flagged for review

  1. 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.
  2. No warning on unsupported models. I followed timeout. A warning for Bedrock and xAI would be easy to add, but RetryOnModelErrors will set max_retries=0 on every model through get_model_settings, so a warning would fire on every Bedrock and xAI request for users who didn't set anything themselves.

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