Allow configuring retry policy and honor `Retry-After` for TPM rate limits in Codex
What variant of Codex are you using?
IDE Extension
What feature would you like to see?
Summary
When Codex encounters an OpenAI TPM (Tokens Per Minute) rate limit during a long-running streaming task, it performs a fixed number of reconnect attempts (currently 5) and then terminates the agent.
For large repositories and long-running coding sessions, this frequently aborts otherwise recoverable tasks.
It would be very helpful if Codex exposed a configurable retry policy and honored the server's Retry-After guidance instead of terminating after a fixed reconnect count.
Current behavior
Example:
stream disconnected before completion:
Rate limit reached for gpt-5.6-sol
Limit: 500000 TPM
Used: 452786
Requested: 135822
Please try again in 10.632s
Codex then shows:
Reconnecting 1/5
Reconnecting 2/5
...
Reconnecting 5/5
After the fifth reconnect, the agent stops and requires manual intervention.
Expected behavior
Instead of aborting after a fixed number of reconnects, Codex should continue retrying until the request can be admitted, or until a configurable timeout/retry budget is reached.
Ideally:
- Honor the server's
Retry-After(or equivalent) delay when provided. - Fall back to exponential backoff if no retry delay is available.
- Allow users to configure retry behavior.
Example configuration:
[retry_policy]
max_retries = 50
max_retry_duration = "30m"
retry_delay_seconds = 30
honor_retry_after = true
or simply:
retry_policy = "adaptive"
Why this matters
Large repositories and long conversations can easily produce requests exceeding 100k tokens.
Example:
Limit: 500000 TPM
Used: 451244
Requested: 137781
Please try again in 10.683s
These are usually temporary rolling-window TPM limits.
The agent already knows when it can retry, but instead exhausts its reconnect budget and exits.
Keeping the agent alive would allow long-running investigations and refactoring sessions to continue automatically without user intervention.
Environment
- Codex CLI: 0.144.6
- VS Code Codex extension
- OpenAI API key authentication
- Model: gpt-5.6-sol
- Ubuntu 24.04
- Large repository (~130k token requests)
Additional notes
I investigated the configuration options and confirmed that:
request_max_retries/stream_max_retriescannot be applied to the built-inopenaiprovider.- There does not appear to be a supported way to configure reconnect count or reconnect delay for the built-in provider.
- The server already returns a retry delay (
Please try again in X seconds), which seems like a good candidate for adaptive retry behavior.
This would significantly improve reliability for users working with large repositories and long-running agent sessions.
Additional information
I spent some time investigating this before opening the feature request.
Things I verified:
- Codex CLI 0.144.6 (latest available)
- VS Code Codex extension
- OpenAI API key authentication
codex doctorreports a healthy installation- Configuration loads successfully
- I confirmed that
request_max_retriesandstream_max_retriescannot be configured for the built-inopenaiprovider because it is reserved.
The server consistently returns messages such as:
- Limit: 500000 TPM
- Used: 451244
- Requested: 137781
- Please try again in 10.683s
However, Codex still performs only five reconnect attempts before terminating the session.
I'm not requesting a way to bypass rate limits. I want Codex to remain alive and automatically retry after the recommended delay (or another configurable delay) until the rolling TPM window has enough capacity again.
This is particularly valuable for long-running investigations on large repositories, where manually restarting the agent interrupts workflow and loses momentum.
Potential duplicates detected. Please review them and close your issue if it is a duplicate.
- #33661
- #33853
- #33257
Powered by Codex Action
I’m hitting the same fixed Reconnecting... 1/5 → 5/5 behavior, but not only for TPM/rate-limit cases. Ordinary transient network / streaming failures such as stream disconnected before completion can also exhaust the hard-coded retry budget and abort a long-running task that would likely recover with a few more attempts.
A particularly useful improvement would be to make the retry budget configurable for the built-in openai provider as well. Today stream_max_retries / request_max_retries are provider-scoped, but the built-in OpenAI provider is reserved, so normal ChatGPT/OpenAI users cannot simply raise the default retry count in config.toml.
For example, it would be helpful to support either a top-level setting such as:
stream_max_retries = 20
that overrides the built-in provider default, or a general retry policy like the one proposed in this issue.
I agree that honoring Retry-After plus bounded backoff is the better long-term design. The key requirement from my perspective is that temporary connectivity/stream interruptions should not automatically kill an otherwise healthy long-running Codex task after exactly five reconnect attempts.
I investigated the retry/fallback path while looking into the Astra compaction stall in #43062. I reproduced a specific client-side retry-delay violation. I have not established that it caused the original stall.
I tested baseline 53c542d944c705f3a66780a19223223bee57cbb6 using real core sessions against synthetic loopback WebSocket and HTTP/SSE servers. The WS server returns response.failed with error.code=rate_limit_exceeded and Please try again in 1.5s.. Ordinary WS retries honor the parsed delay, but the first HTTP fallback request skips the final error's advice.
I measured from immediately before the last scripted WS failure to the first HTTP POST, excluding startup/prewarm, seed turns and earlier backoffs:
| Case | Configured stream retries | Observed fallback delay | Expected |
|---|---|---|---|
| Manual remote compaction | 0 | 7.8819 ms | ≥1500 ms |
| Ordinary Sampling | 0 | 7.5986 ms | ≥1500 ms |
| Manual remote compaction | 2 | 5.9103 ms | ≥1500 ms |
All three baseline regression tests failed that timing assertion. I then applied only a three-line local change: wait for the existing err.retry_delay() before returning from the already-selected fallback branch. The identical command and unchanged assertions passed all three tests afterward. Inner HTTP request retries were disabled in this fixture.
The mechanism is visible in the shared retry helper: the exhausted-retry branch switches transport, resets the counter and returns before consulting the delay. This concerns the parser's message advice, not an HTTP Retry-After header. I did not change fallback eligibility, retry budgets, model selection, idle timeouts or error classification.
I also ran 57 focused tests successfully, covering Sampling, cancellation during the fallback wait, successful compaction without fallback, failure of both transports, sticky HTTP state after interruption/failure, existing rate-limit/capacity handling and rejection of partial checkpoint markers. Clippy and formatting passed. The full core suite is not green: it includes missing helper binaries and Code Mode failures. Two representative failures also reproduce on baseline; I am not claiming every broader failure has been diagnosed.
I rechecked main at ee6814bfa4889fe9b2b3dcc9cc8bdd91effa8ab8; the relevant retry, compaction, client and parser files are unchanged. #44482 handles exhausted advice for Guardian after this branch, and #44311 concerns remote-control transport.
My conclusion is that the early return explains the measured premature fallback. If a real compaction attempt is rate-limited, this can send the next request before the advised wait expires. Whether that contributes to the Astra stall still needs the failed request trace; I have not demonstrated improved wall time, usage, cost or live-session recovery.
Could you check this retry boundary and correlate #43062's backend trace to distinguish rate-limit advice from socket silence or a stream that never completes? I would keep that investigation separate from changing the general retry budget or introducing earlier fallback.
I ran into an additional problem with the custom-provider workaround on Windows, where the ChatGPT desktop application and Codex share configuration. I wanted a larger retry budget for transient network and streaming failures while keeping OpenAI authentication.
I used:
model_provider = "openai-retry"
[model_providers.openai-retry]
name = "OpenAI"
wire_api = "responses"
requires_openai_auth = true
supports_websockets = true
request_max_retries = 10
stream_max_retries = 10
An existing chat then refused to resume with this message:
Model provider mismatch. This chat uses OpenAI, but your current provider is OpenAI. Switch to OpenAI to continue.
Reverting to model_provider = "openai" fixed resume. The identical display names make the diagnostic confusing; the behavior suggests that the distinct provider IDs (openai and openai-retry) matter here.
I also tried putting the retry settings under [model_providers.openai]. That prevented the application from starting because openai is a reserved provider ID and built-in providers cannot be overridden.
Could retry settings be overridden for the built-in OpenAI provider without changing its provider ID? A top-level retry configuration or dedicated retry-policy section would avoid this compatibility problem. It would also help if mismatch diagnostics showed the actual provider IDs rather than telling me to switch from OpenAI to OpenAI.
I did not test whether the higher retry counts improved reliability. The observations here are the resume failure with the custom provider, successful resume after reverting to openai, and startup rejection of the built-in provider override. This adds a desktop/shared-config use case to the request, separate from the original TPM rate-limit report.