Invalid request parameters or malformed JSON
Authentication failed - API key invalid
API key lacks permission for this resource
Endpoint or resource does not exist
Too many requests - implement backoff strategy
Unexpected error on our servers
Upstream service temporarily unavailable
Service temporarily overloaded
async function makeRequestWithRetry(requestFn, maxRetries = 3) {
for (let attempt = 1; attempt <= maxRetries; attempt++) {
try {
return await requestFn();
} catch (error) {
console.log(`Attempt ${attempt} failed:`, error.message);
// Check if error is retryable
if (error.status === 429 || error.status >= 500) {
if (attempt === maxRetries) {
throw new Error(`Max retries (${maxRetries}) exceeded: ${error.message}`);
}
// Exponential backoff: 1s, 2s, 4s, 8s...
const delay = Math.min(1000 * Math.pow(2, attempt - 1), 10000);
console.log(`Retrying in ${delay}ms...`);
await new Promise(resolve => setTimeout(resolve, delay));
} else {
// Non-retryable error (4xx except 429)
throw error;
}
}
}
}
// Usage example
try {
const response = await makeRequestWithRetry(async () => {
return await openai.chat.completions.create({
model: 'gpt-4-turbo',
messages: [{ role: 'user', content: 'Hello' }]
});
});
console.log(response.choices[0].message.content);
} catch (error) {
console.error('Request failed permanently:', error.message);
}import time
import random
from openai import OpenAI, APIError, RateLimitError, APIConnectionError
def make_request_with_retry(client, max_retries=3):
for attempt in range(1, max_retries + 1):
try:
response = client.chat.completions.create(
model='gpt-4-turbo',
messages=[{'role': 'user', 'content': 'Hello'}]
)
return response
except RateLimitError as e:
if attempt == max_retries:
raise e
# Rate limit hit, wait longer
delay = min(2 ** attempt + random.uniform(0, 1), 60)
print(f"Rate limited. Waiting {delay:.1f}s...")
time.sleep(delay)
except APIConnectionError as e:
if attempt == max_retries:
raise e
# Network error, retry quickly
delay = min(2 ** (attempt - 1), 10)
print(f"Connection error. Retrying in {delay}s...")
time.sleep(delay)
except APIError as e:
if e.status_code >= 500:
# Server error, retry
if attempt == max_retries:
raise e
delay = min(2 ** attempt, 30)
print(f"Server error {e.status_code}. Retrying in {delay}s...")
time.sleep(delay)
else:
# Client error (4xx), don't retry
raise e
# Usage
client = OpenAI(api_key='your-key', base_url='https://api.coreapi.com/v1')
try:
response = make_request_with_retry(client)
print(response.choices[0].message.content)
except Exception as e:
print(f"Request failed: {e}")https://api.coreapi.com/v1Mori API is an AI model aggregation platform that gives developers a single, unified API to access 50+ AI models across text, image, audio, and video — with transparent pricing, real‑time analytics, and enterprise‑grade reliability.