Skip to content

fix(llm): retry on transient API errors (429, 50x) instead of crashing - #18

Open
tejasprasad2008-afk wants to merge 4 commits into
ni5arga:mainfrom
tejasprasad2008-afk:split/3-llm-resilience
Open

fix(llm): retry on transient API errors (429, 50x) instead of crashing#18
tejasprasad2008-afk wants to merge 4 commits into
ni5arga:mainfrom
tejasprasad2008-afk:split/3-llm-resilience

Conversation

@tejasprasad2008-afk

Copy link
Copy Markdown

Description:

This PR makes the LLM client more resilient to common API failures without changing the CLI interface.

Changes:

  • Automatic Retries: The OpenAIClient now identifies retryable errors (429 Rate Limits, 504 Timeouts, etc.) and automatically retries the request up to 2 times.
  • 429 Throttling: Added a small 2-second delay before retrying 429 errors to increase the chance of success on the second attempt.
  • Stability: This effectively prevents the tool from crashing midway through a long analysis run when a provider has a brief hiccup.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Improves the OpenAI-compatible LLM client’s robustness by adding automatic retries on transient failures, aiming to prevent long CLI runs from failing due to brief provider hiccups.

Changes:

  • Add retry loop around OpenAI chat completions with retryable-error detection (429, 5xx, network-ish messages).
  • Add a 2-second backoff before retrying HTTP 429 responses.
  • Improve handling of “no choices”/error-payload responses and add tests covering retries and missing choices.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 5 comments.

File Description
src/llm/openai.ts Adds retry logic, retryable error detection, and stronger validation/error wrapping for empty/invalid responses.
src/llm/openai.test.ts Updates/extends tests to cover “no choices”, missing choices, and retry behavior.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/llm/openai.ts Outdated
Comment on lines +9 to +10
/** HTTP status codes worth retrying with a different model. */
const RETRYABLE_CODES = new Set([429, 500, 502, 503, 504]);
Comment thread src/llm/openai.ts Outdated
Comment on lines +36 to +38
* When `fallbackModels` are configured, a retryable failure (504, 429, empty
* choices, etc.) on the primary model triggers an automatic retry with the
* next fallback before the error propagates.
Comment thread src/llm/openai.ts Outdated
Comment on lines +63 to +68
if (isRetryableError(error) && attempt < MAX_RETRIES) {
process.stderr.write(
pc.yellow(
` ⚠ Request failed (attempt ${attempt + 1}/${MAX_RETRIES + 1}), retrying...\n`,
),
);
Comment thread src/llm/openai.ts
Comment on lines +105 to +112
if (!resp.choices || resp.choices.length === 0) {
const respStr = JSON.stringify(resp);
throw Object.assign(
new Error(
`${this.label} returned no choices or an error payload: ${respStr}`,
),
{ status: 502 },
);
Comment thread src/llm/openai.test.ts
Comment on lines +119 to +125
if (callCount === 1) {
const err = new Error("rate limit exceeded") as Error & {
status: number;
};
err.status = 429;
throw err;
}
@tejasprasad2008-afk

Copy link
Copy Markdown
Author

YEAH THIS ONES ALL GOOD AND FIXED......

HOPEFULLY

@ni5arga

ni5arga commented Jun 5, 2026

Copy link
Copy Markdown
Owner

cool, will run claude code review/copilot and get back. thanks for the PR!

@tejasprasad2008-afk

Copy link
Copy Markdown
Author

bro istg sth wrong with the reviewer. i checked the code like a 100 times its in good shape

@tejasprasad2008-afk

Copy link
Copy Markdown
Author

k thats gone now. just some miscallaneous typescript issues and one extra line space

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

Comment thread src/llm/openai.ts
@tejasprasad2008-afk

Copy link
Copy Markdown
Author

that was a lot of trust issues but umm is it good now 😭

@tejasprasad2008-afk

Copy link
Copy Markdown
Author

ive fixed this one also most issues is github failing to run it if you wanna test it in here itself u can do it in the GitHub Codespace. should i open a pr with these 3 combined? so tht u can try it out easier? its already there i just make it a pr

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants