From 7f85c67b4d6f9031b06ed97a3fe28b3589735098 Mon Sep 17 00:00:00 2001 From: Andrew Leech Date: Wed, 30 Apr 2025 21:33:14 +1000 Subject: [PATCH] feat: Add retry to agent-loop for mid-stream errors. --- codex-cli/src/utils/agent/agent-loop.ts | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/codex-cli/src/utils/agent/agent-loop.ts b/codex-cli/src/utils/agent/agent-loop.ts index 53da69798..9f52443f5 100644 --- a/codex-cli/src/utils/agent/agent-loop.ts +++ b/codex-cli/src/utils/agent/agent-loop.ts @@ -1032,16 +1032,22 @@ export class AgentLoop { }; if ( - isRateLimitError(err) && streamRetryAttempt < MAX_STREAM_RETRIES ) { streamRetryAttempt += 1; - const waitMs = - RATE_LIMIT_RETRY_WAIT_MS * 2 ** (streamRetryAttempt - 1); - log( - `OpenAI stream rate‑limited – retry ${streamRetryAttempt}/${MAX_STREAM_RETRIES} in ${waitMs} ms`, - ); + let waitMs; + if (isRateLimitError(err)) { + waitMs = RATE_LIMIT_RETRY_WAIT_MS * 2 ** (streamRetryAttempt - 1); + log( + `OpenAI stream rate‑limited – retry ${streamRetryAttempt}/${MAX_STREAM_RETRIES} in ${waitMs} ms`, + ); + } else { + waitMs = RATE_LIMIT_RETRY_WAIT_MS; + log( + `OpenAI stream error "${err}" – retry ${streamRetryAttempt}/${MAX_STREAM_RETRIES} in ${waitMs} ms`, + ); + } // Give the server a breather before retrying. // eslint-disable-next-line no-await-in-loop