Skip to content

agent-loop: minimal mid-stream #429 retry loop using existing back-off - #506

Merged
tibo-openai merged 2 commits into
openai:mainfrom
scottleibrand:minimal-357
Apr 22, 2025
Merged

tibo-openai merged 2 commits into
openai:mainfrom
scottleibrand:minimal-357

Conversation

@scottleibrand

Copy link
Copy Markdown
Contributor

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.

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.
• No new helpers or config; ~35 LOC diff.
• Keeps existing error paths untouched and ESLint‑clean.

All tests, lint, build and type‑check pass.
@CaptEmulation

Copy link
Copy Markdown

Tested and works for me. No more 429 on long sessions on a Tier 1 account.

@tibo-openai

Copy link
Copy Markdown
Collaborator

We'll want to simplify all of this in a follow-up as the whole implementation here is starting to have more complexity than desired, however the core logic contributed here looks sound and I've tried it locally without hiccup.

I will merge this as this resolves a large stability issues currently affecting users and we should push on making agent-loop.ts easier to maintain going forward. One thing I would like to see happen in particular is for all the error pattern matching and some helpers to be extracted to agent-error.ts.

@tibo-openai

Copy link
Copy Markdown
Collaborator

Thank you @scottleibrand for the great contribution, no doubt many users will benefit from this!

@tibo-openai
tibo-openai merged commit ee6e176 into openai:main Apr 22, 2025
@github-actions github-actions Bot locked and limited conversation to collaborators Apr 22, 2025
@scottleibrand
scottleibrand deleted the minimal-357 branch October 22, 2026 04:59
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