feat: implement smart exponential backoff with rate limit headers for OpenAI embedder #6068
+564
−90
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
This PR implements intelligent rate limit handling for the OpenAI embedder by utilizing response headers to calculate optimal retry delays. The implementation ensures no embedding batches are lost due to rate limiting while minimizing unnecessary wait times.
Problem
Previously, the OpenAI embedder used a simple exponential backoff strategy with a fixed retry limit (3 attempts). This approach had several issues:
Solution
1. Smart Backoff Using Rate Limit Headers
x-ratelimit-limit-requests
/x-ratelimit-limit-tokens
x-ratelimit-remaining-requests
/x-ratelimit-remaining-tokens
x-ratelimit-reset-requests
/x-ratelimit-reset-tokens
2. Infinite Retries for Rate Limits
3. Global Rate Limit Coordination
async-mutex
4. Reduced Logging
Code Changes
Modified Files:
src/services/code-index/embedders/openai.ts
src/services/code-index/embedders/__tests__/openai.spec.ts
Testing
All existing tests pass with the following updates:
Example Log Output
Before:
After:
Benefits
Breaking Changes
None. The API remains the same, only the internal retry behavior has changed.
Related Issues
Checklist