codex crashes on rate limit error instead of retrying

Resolved 💬 12 comments Opened Apr 18, 2025 by pascudder Closed Apr 18, 2025
💡 Likely answer: A maintainer (jonchurch, contributor) responded on this thread — see the highlighted reply below.

What version of Codex is running?

0.1.2504161510

Which model were you using?

o4-mini

What platform is your computer?

Linux 6.8.0-1026-gcp x86_64 x86_64

What steps can reproduce the bug?

Run a complex command that generates a high token load, e.g.

codex --approval-mode full-auto "perform this complicated task"

Wait until a rate limit is reached (e.g. due to high usage of o4-mini model)

What is the expected behavior?

Codex should wait the suggested time (e.g., 6.379 seconds), then retry the request gracefully

What do you see instead?

Codex crashes with an unhandled rate_limit_exceeded error. No retry is attempted.

Here’s the error message:

[Error]: Rate limit reached for o4-mini in organization [REDACTED_ORG_ID] on tokens per min (TPM): Limit 200000, Used 158833, Requested 62431. Please try again in 6.379s.
at e.a [as iterator] (dist/cli.js:449:1514)
at process.processTicksAndRejections (node:internal/process/task_queues:105:5)
at async T0.run (dist/cli.js:466:2933) {
status: undefined,
headers: undefined,
request_id: undefined,
error: {
type: 'tokens',
code: 'rate_limit_exceeded',
message: 'Rate limit reached for o4-mini in organization [REDACTED_ORG_ID] on tokens per min (TPM): Limit 200000, Used 158833, Requested 62431. Please try again in 6.379s.',
param: null
},
code: 'rate_limit_exceeded',
param: null,
type: 'tokens'
}

Node.js v22.14.0

Additional information

_No response_

View original on GitHub ↗

12 Comments

jonchurch contributor · 1 year ago

Dupe of https://github.com/openai/codex/issues/157

You can try to build from source and run the cli, there is something merged in but unreleased which may fix this

pascudder · 1 year ago

@jonchurch

Thanks for the reply, I built from source a couple hours ago - I can confirm I have the regex update that they made, but the issue persists.

jonchurch contributor · 1 year ago

Aww heck, ty for trying the new fix! I haven't been able to reproduce this reliably (the token limit)

If you are able to repro it and want to tackle it, maybe throw some console logs in there to verify it is actually hitting the maxRetry count. Would be curious to know if it just dies before it exhausts retries somehow.

Did it end up sitting for a while? it should eventually wait like a full minute cumulatively for the retries to exhaust

robbiehammond contributor · 1 year ago

To add another datapoint, I'm having the same issue, making codex mostly unusable for me. For testing I added a console.log to the beginning of the loop like so:

        // Retry loop for transient errors. Up to MAX_RETRIES attempts.
        const MAX_RETRIES = 5;
        for (let attempt = 1; attempt <= MAX_RETRIES; attempt++) {
          console.log("attempt number: " + attempt)
          try { ...

And then within the codex codebase itself I prompted it with I want you to analyze 15 files of the codebase and explain them to me. Just choose a few that are vaguely related. just to try and exceed the limit. This did make it exceed the limit (Rate limit reached for o4-mini in [REDACTED] on tokens per min (TPM): Limit 200000, Used 171023, Requested 38760. Please try again in 2.934s. Visit https://platform.openai.com/account/rate-limits to learn more.). The logged attempt count never grew past 1 on any request, so it doesn't seem like it was retrying at all in this case.

pascudder · 1 year ago
Aww heck, ty for trying the new fix! I haven't been able to reproduce this reliably (the token limit) If you are able to repro it and want to tackle it, maybe throw some console logs in there to verify it is actually hitting the maxRetry count. Would be curious to know if it just dies before it exhausts retries somehow. Did it end up sitting for a while? it should eventually wait like a full minute cumulatively for the retries to exhaust

It is failing pretty quickly for me. I believe the issue is, the stream consumption for await (const event of stream) is outside the retry logic, only checking for abort errors. If it gets a 429 error during streaming, it throws a rate limit error with no retry handling. Line 794 throw err; is what I tracked it down to.

guidedways contributor · 1 year ago
greendrake · 1 year ago

+1. The most annoying part is that after restarting it loses the context.

jrjones · 1 year ago

+1 was just about to come here and add the same issue. I'll look at the code later, but seems like it should be pretty easy to wait a few seconds and retry (the response even tells you how long to wait.) We're definitely spamming the API in terms of tokens, this behavior isn't surprising.

calvinlacock · 1 year ago

+1

pascudder · 1 year ago

OK, I figured out a fix: https://github.com/pascudder/codex

calvinlacock · 1 year ago

is it just me but I cant see the fix ? Sorry to be a pain.. Or are you changing code and we update? How do we update ? Thanks again !

pascudder · 1 year ago
is it just me but I cant see the fix ? Sorry to be a pain.. Or are you changing code and we update? How do we update ? Thanks again !

replace this file

https://github.com/pascudder/codex/blob/main/codex-cli/src/utils/agent/agent-loop.ts