You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/models/index.md
+86Lines changed: 86 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -268,6 +268,7 @@ When you are using the OpenAI Responses API, several request fields already have
268
268
|`prompt_cache_retention`| Keep cached prompt prefixes around longer, for example with `"24h"`. |
269
269
|`response_include`| Request richer response payloads such as `web_search_call.action.sources`, `file_search_call.results`, or `reasoning.encrypted_content`. |
270
270
|`top_logprobs`| Request top-token logprobs for output text. The SDK also adds `message.output_text.logprobs` automatically. |
271
+
|`retry`| Opt in to runner-managed retry settings for model calls. See [Runner-managed retries](#runner-managed-retries). |
271
272
272
273
```python
273
274
from agents import Agent, ModelSettings
@@ -285,6 +286,91 @@ research_agent = Agent(
285
286
)
286
287
```
287
288
289
+
#### Runner-managed retries
290
+
291
+
Retries are runtime-only and opt in. The SDK does not retry general model requests unless you set `ModelSettings(retry=...)` and your retry policy chooses to retry.
292
+
293
+
```python
294
+
from agents import Agent, ModelRetrySettings, ModelSettings, retry_policies
|`max_retries`|`int \| None`| Number of retry attempts allowed after the initial request. |
324
+
|`backoff`|`ModelRetryBackoffSettings \| dict \| None`| Default delay strategy when the policy retries without returning an explicit delay. |
325
+
|`policy`|`RetryPolicy \| None`| Callback that decides whether to retry. This field is runtime-only and is not serialized. |
326
+
327
+
A retry policy receives a [`RetryPolicyContext`][agents.retry.RetryPolicyContext] with:
328
+
329
+
-`attempt` and `max_retries` so you can make attempt-aware decisions.
330
+
-`stream` so you can branch between streamed and non-streamed behavior.
331
+
-`error` for raw inspection.
332
+
-`normalized` facts such as `status_code`, `retry_after`, `error_code`, `is_network_error`, `is_timeout`, and `is_abort`.
333
+
-`provider_advice` when the underlying model adapter can supply retry guidance.
334
+
335
+
The policy can return either:
336
+
337
+
-`True` / `False` for a simple retry decision.
338
+
- A [`RetryDecision`][agents.retry.RetryDecision] when you want to override the delay or attach a diagnostic reason.
339
+
340
+
The SDK exports ready-made helpers on `retry_policies`:
341
+
342
+
| Helper | Behavior |
343
+
| --- | --- |
344
+
|`retry_policies.never()`| Always opts out. |
345
+
|`retry_policies.provider_suggested()`| Follows provider retry advice when available. |
346
+
|`retry_policies.network_error()`| Matches transient transport and timeout failures. |
347
+
|`retry_policies.http_status([...])`| Matches selected HTTP status codes. |
348
+
|`retry_policies.retry_after()`| Retries only when a retry-after hint is available, using that delay. |
349
+
|`retry_policies.any(...)`| Retries when any nested policy opts in. |
350
+
|`retry_policies.all(...)`| Retries only when every nested policy opts in. |
351
+
352
+
When you compose policies, `provider_suggested()` is the safest first building block because it preserves provider vetoes and replay-safety approvals when the provider can distinguish them.
353
+
354
+
##### Safety boundaries
355
+
356
+
Some failures are never retried automatically:
357
+
358
+
- Abort errors.
359
+
- Requests where provider advice marks replay as unsafe.
360
+
- Streamed runs after output has already started in a way that would make replay unsafe.
361
+
362
+
Stateful follow-up requests using `previous_response_id` or `conversation_id` are also treated more conservatively. For those requests, non-provider predicates such as `network_error()` or `http_status([500])` are not enough by themselves. The retry policy should include a replay-safe approval from the provider, typically via `retry_policies.provider_suggested()`.
363
+
364
+
##### Runner and agent merge behavior
365
+
366
+
`retry` is deep-merged between runner-level and agent-level `ModelSettings`:
367
+
368
+
- An agent can override only `retry.max_retries` and still inherit the runner's `policy`.
369
+
- An agent can override only part of `retry.backoff` and keep sibling backoff fields from the runner.
370
+
-`policy` is runtime-only, so serialized `ModelSettings` keep `max_retries` and `backoff` but omit the callback itself.
371
+
372
+
For fuller examples, see [`examples/basic/retry.py`](https://github.com/openai/openai-agents-python/tree/main/examples/basic/retry.py) and [`examples/basic/retry_litellm.py`](https://github.com/openai/openai-agents-python/tree/main/examples/basic/retry_litellm.py).
373
+
288
374
Use `extra_args` when you need provider-specific or newer request fields that the SDK does not expose directly at the top level yet.
289
375
290
376
Also, when you use OpenAI's Responses API, [there are a few other optional parameters](https://platform.openai.com/docs/api-reference/responses/create) (e.g., `user`, `service_tier`, and so on). If they are not available at the top level, you can use `extra_args` to pass them as well.
0 commit comments