Skip to content

Enhanced retry to wait returned rate limit if present#179

Open
m4t7w wants to merge 4 commits intoGewoonJaap:mainfrom
PorcoDio00033:enhance-retry
Open

Enhanced retry to wait returned rate limit if present#179
m4t7w wants to merge 4 commits intoGewoonJaap:mainfrom
PorcoDio00033:enhance-retry

Conversation

@m4t7w
Copy link
Copy Markdown
Contributor

@m4t7w m4t7w commented Apr 1, 2026

Changes

  • enhanced auto-retry to wait the returned rate limit from gemini api (if present).
  • added parseQuotaResetTime to parse rate limit from gemini api response.
    NOTE: implementation uses a regex on error message because gemini api doesn't return a rate-limit header

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces dynamic rate limit handling for the Gemini API by parsing quota reset times from error messages. It adds a utility to extract reset durations and updates the client to wait for these periods or trigger fallbacks for long resets. Feedback recommends using a named constant for the reset threshold and adding a radix to parseInt calls for improved code quality.

Comment thread src/gemini-client.ts Outdated
Comment thread src/helpers/auto-model-switching.ts Outdated
Comment thread src/gemini-client.ts Outdated
Comment thread src/gemini-client.ts
Comment on lines +633 to +636
if (dynamicDelay !== null && attempt < retryDelays.length) {
console.log(`Got ${response.status} for ${currentModel}, waiting for quota reset: ${dynamicDelay}ms`);
await new Promise((resolve) => setTimeout(resolve, dynamicDelay));
continue;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Bug: The retry loop's final iteration incorrectly skips the API-provided dynamic delay because the loop condition attempt <= retryDelays.length is inconsistent with the internal check attempt < retryDelays.length.
Severity: MEDIUM

Suggested Fix

Align the loop's boundary with the internal checks. Change the for loop condition from attempt <= retryDelays.length to attempt < retryDelays.length. This ensures the loop does not execute an extra iteration where the delay logic would be skipped.

Prompt for AI Agent
Review the code at the location below. A potential bug has been identified by an AI
agent.
Verify if this is a real issue. If it is, propose a fix; if not, explain why it's not
valid.

Location: src/gemini-client.ts#L633-L636

Potential issue: In `src/gemini-client.ts`, the retry loop is defined to run from
`attempt = 0` to `retryDelays.length` inclusive. However, the conditional logic inside
the loop, such as `if (dynamicDelay !== null && attempt < retryDelays.length)`, uses a
strict less-than comparison. On the final iteration, when `attempt` is equal to
`retryDelays.length`, this condition becomes false. As a result, the code skips the
`await` for the `dynamicDelay` returned by the API, immediately breaks from the loop,
and proceeds to model fallback or error throwing. This defeats the purpose of honoring
the API's rate limit reset time on the last retry attempt.

@GewoonJaap GewoonJaap closed this Apr 1, 2026
@GewoonJaap
Copy link
Copy Markdown
Owner

Thanks for the PR can you please addresses the issues from the Gemini bot?

@GewoonJaap GewoonJaap reopened this Apr 1, 2026
@m4t7w
Copy link
Copy Markdown
Contributor Author

m4t7w commented Apr 1, 2026

Thanks for the PR can you please addresses the issues from the Gemini bot?

hi, the last 2 commits resolve both gemini and the first sentry bot suggestions.

Last sentry bot message is wrong, retryDelays.length is checked at the top of the loop

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.

2 participants