Add exponential backoff retry logic for OpenAI rate limit errors (429)
This commit is contained in:
+17
-2
@@ -501,6 +501,13 @@ FORMAT: Always respond with "ANSWER: " followed by the LETTER(s) of the correct
|
|||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
const error = await response.json();
|
const error = await response.json();
|
||||||
console.error('❌ OpenAI API error:', error);
|
console.error('❌ OpenAI API error:', error);
|
||||||
|
|
||||||
|
// Check for rate limit error (status 429 or error code)
|
||||||
|
if (response.status === 429 || error?.error?.type === 'rate_limit_exceeded' || error?.error?.code === 'rate_limit_exceeded') {
|
||||||
|
console.log('⚠️ Rate limit detected, will retry with backoff');
|
||||||
|
return 'RATE_LIMITED';
|
||||||
|
}
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -571,11 +578,19 @@ FORMAT: Always respond with "ANSWER: " followed by the LETTER(s) of the correct
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Retry wrapper with better backoff
|
// Retry wrapper with exponential backoff for rate limits
|
||||||
async function getAnswerWithRetry(question, answerChoices, inputType, expectedCount, retries = 2) {
|
async function getAnswerWithRetry(question, answerChoices, inputType, expectedCount, retries = 5) {
|
||||||
for (let i = 0; i <= retries; i++) {
|
for (let i = 0; i <= retries; i++) {
|
||||||
try {
|
try {
|
||||||
const result = await getAnswerFromOpenAI(question, answerChoices, inputType, expectedCount);
|
const result = await getAnswerFromOpenAI(question, answerChoices, inputType, expectedCount);
|
||||||
|
|
||||||
|
if (result === 'RATE_LIMITED') {
|
||||||
|
const backoffMs = Math.min(1000 * Math.pow(2, i), 60000); // Max 60 seconds
|
||||||
|
console.log(`⏳ Rate limited. Waiting ${backoffMs / 1000}s before retry ${i + 1}/${retries}...`);
|
||||||
|
await new Promise(resolve => setTimeout(resolve, backoffMs));
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
if (result && result.length > 0) {
|
if (result && result.length > 0) {
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user