Skip to content

Fix: 429 rate limit error crash in agent-loop.ts (working but not optimized) - #357

Closed
pascudder wants to merge 7 commits into
openai:mainfrom
pascudder:main
Closed

pascudder wants to merge 7 commits into
openai:mainfrom
pascudder:main

Conversation

@pascudder

@pascudder pascudder commented Apr 18, 2025 •

Copy link
Copy Markdown

Fix for 429 rate limit CLI crash during streaming in AgentLoop

This Draft PR introduces retry logic to handle HTTP 429 (rate limit) errors encountered in the streaming loop of AgentLoop.run in the Codex CLI. The fix prevents abrupt stream failures by wrapping stream consumption in a while loop, retrying up to 5 times with a 15-second delay.

The code is likely not production-ready due to lack of testing and potential edge cases, and the suboptimal static 15 second wait between retries. However, it does fix the issue for me.

Changes:

  • Added retry logic for 429 errors in src/utils/agent/agent-loop.ts:
    • Wrapped the streaming for await loop in a while loop to retry on rate limit errors.
    • Used a fixed 15-second delay between retries with setTimeout.
    • Introduced MAX_STREAM_RETRIES = 5 and streamRetryAttempt to control retries.
    • Implemented isRateLimitError function to detect 429 errors via code: "rate_limit_exceeded".

Implemented retry logic for HTTP 429 (rate limit) errors encountered during the streaming loop in AgentLoop.run. The fix ensures that streaming requests interrupted by rate limits are retried up to 5 times with a 15-second delay between attempts, reinitializing pending abort outputs and recreating the stream to maintain context.
@github-actions

github-actions Bot commented Apr 18, 2025 •

Copy link
Copy Markdown
Contributor

All contributors have signed the CLA ✍️ ✅
Posted by the CLA Assistant Lite bot.

@pascudder

Copy link
Copy Markdown
Author

I have read the CLA Document and I hereby sign the CLA

@tibo-openai

Copy link
Copy Markdown
Collaborator

Have you seen #176?

@pascudder

Copy link
Copy Markdown
Author

Have you seen #176?

I took a quick look. It doesn't address the issue that the stream consumption for await (const event of stream) { is exiting without retrying

@pourhadi

Copy link
Copy Markdown

I've tried the various proposals to fix the rate-limit problem; this is the only one that seems to actually work.

@tibo-openai

Copy link
Copy Markdown
Collaborator

Really appreciate the fix, but there is a lot going on in the change and the logic looks quite convoluted, making it hard to follow and review.

Would you be able to simplify things to the be more minimal, possibly extracting some of the logic and adding tests, also while removing some of the extra functionality that introduces complexity?

Thanks working on making codex more stable when facing rate limits!

tibo-openai pushed a commit that referenced this pull request Apr 22, 2025
#506)

As requested by @tibo-openai at
#357 (comment), this
attempts a more minimal implementation of #357 that preserves as much as
possible of the existing code's exponential backoff logic.

Adds a small retry wrapper around the streaming for‑await loop so that
HTTP 429s which occur *after* the stream has started no longer crash the
CLI.

Highlights
• Re‑uses existing RATE_LIMIT_RETRY_WAIT_MS constant and 5‑attempt
limit.
• Exponential back‑off identical to initial request handling. 

This comment is probably more useful here in the PR:
// The OpenAI SDK may raise a 429 (rate‑limit) *after* the stream has
// started. Prior logic already retries the initial `responses.create`
        // call, but we need to add equivalent resilience for mid‑stream
        // failures.  We keep the implementation minimal by wrapping the
// existing `for‑await` loop in a small retry‑for‑loop that re‑creates
        // the stream with exponential back‑off.
@tibo-openai

Copy link
Copy Markdown
Collaborator

Closing as we have merged #506

@github-actions github-actions Bot locked and limited conversation to collaborators Apr 22, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants