Skip to content

codex crashes on rate limit error instead of retrying #303

Closed

Description

@pascudder

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

Activity

  1. jonchurch commented on Apr 18, 2025

    @jonchurch
    Contributor

    Dupe of #157

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

  2. pascudder commented on Apr 18, 2025

    @pascudder
    Author

    @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.

  3. jonchurch commented on Apr 18, 2025

    @jonchurch
    Contributor

    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

  4. robbiehammond commented on Apr 18, 2025

    @robbiehammond
    Contributor

    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.

  5. pascudder commented on Apr 18, 2025

    @pascudder
    Author

    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.

  6. guidedways commented on Apr 18, 2025

    @guidedways
    Contributor

    Also: #311

  7. greendrake commented on Apr 18, 2025

    @greendrake

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

  8. jrjones commented on Apr 18, 2025

    @jrjones

    +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.

  9. calvinlacock commented on Apr 18, 2025

    @calvinlacock

    +1

  10. pascudder commented on Apr 18, 2025

    @pascudder
    Author

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

  11. calvinlacock commented on Apr 18, 2025

    @calvinlacock

    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 !

  12. pascudder commented on Apr 18, 2025

    @pascudder
    Author

    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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions