Error Handling & Fallback
How CoreValue AI Gateway handles errors and automatically falls back between billing methods
CoreValue AI Gateway automatically tries multiple billing methods to ensure your requests succeed. When one method fails, it falls back to alternatives and returns the most actionable error to help you fix issues quickly.
How Fallback Works
The AI Gateway supports two billing methods:
Pass-Through Billing (PTB)
Pay-as-you-go with CoreValue credits. Simple, no provider account needed.
Bring Your Own Keys (BYOK)
Use your own provider API keys. You're billed directly by the provider.
Automatic Fallback: When you configure both methods, the gateway tries PTB first. If it fails (e.g., insufficient credits), it automatically falls back to BYOK.
Error Priority Logic
When both billing methods fail, the gateway returns the most actionable error to help you resolve the issue:
Priority Order
- 403 Forbidden → Critical access issue, contact support
- 401 Unauthorized → Fix your provider API key
- 400 Bad Request → Fix your request format
- 500 Server Error → Provider issue or configuration problem
- 429 Rate Limit → Only shown if all attempts hit rate limits
Why this order? If you configured BYOK, errors from your provider keys (401, 500) are more actionable than PTB's "insufficient credits" (429). You chose BYOK for a reason!
Common Error Scenarios
| Error Code | What It Means | Action Required | Example |
|---|---|---|---|
| 401 | Authentication failed | Check your provider API key in settings | Invalid OpenAI API key |
| 403 | Access forbidden | Contact support@corevalue.dev | Wallet suspended, model blocked |
| 400 | Invalid request format | Fix your request body or parameters | Missing required field |
| 429 | Insufficient credits | Add credits OR configure provider keys | No CoreValue credits, no BYOK |
| 500 | Upstream provider error | Check provider status or retry | Provider API timeout |
| 503 | Service unavailable | Provider temporarily down, retry later | Provider maintenance |
Fallback Scenarios
Understanding Error Sources
When you see an error, you can determine which billing method it came from:
PTB Errors:
- 429: "Insufficient credits" → Add credits at
/credits - 403: "Wallet suspended" → Contact support
BYOK Errors:
- 401: "Invalid API key" → Check provider keys in
/settings/providers - 500: "Provider error" → Check provider status
- 503: "Service unavailable" → Provider having issues
Best Practices
Configure Both Methods
Set up both PTB and BYOK for maximum reliability. If one fails, the other serves as backup.
Monitor Credit Balance
Keep track of your CoreValue credits to avoid 429 errors during critical requests.
Automatic Retries
The gateway automatically retries transient errors (429, 500, 502, 503, 504, 529) with exponential backoff and jitter. No configuration needed.
Log Error Details
Log the full error response to debug provider-specific issues quickly.
Error Handling in Code
Retries are built-in: The gateway automatically retries transient failures (429, 500, 502, 503, 504, 529) with exponential backoff and jitter. You generally do not need to implement your own retry logic — the X-Cova-Retry-Count response header tells you how many retries were attempted.
Retry Logic Example
import OpenAI from "openai";
const client = new OpenAI({
baseURL: "https://gateway.corevalue.dev/v1",
apiKey: process.env.COVA_API_KEY,
});
async function callWithRetry(maxRetries = 3) {
for (let i = 0; i < maxRetries; i++) {
try {
const response = await client.chat.completions.create({
model: "gpt-4o-mini",
messages: [{ role: "user", content: "Hello!" }],
});
return response;
} catch (error: any) {
const status = error?.status || 500;
// Don't retry auth errors or bad requests
if (status === 401 || status === 403 || status === 400) {
throw error;
}
// Don't retry insufficient credits unless it's the last attempt
if (status === 429 && i === maxRetries - 1) {
throw error;
}
// Retry transient errors (500, 503) with exponential backoff
if (status >= 500 || status === 429) {
await new Promise(resolve => setTimeout(resolve, Math.pow(2, i) * 1000));
continue;
}
throw error;
}
}
}Error Classification
function classifyError(error: any) {
const status = error?.status || 500;
if (status === 401) {
return {
type: "authentication",
action: "Check your API keys in settings",
retryable: false
};
}
if (status === 429) {
return {
type: "rate_limit",
action: "Add credits or wait before retrying",
retryable: true
};
}
if (status >= 500) {
return {
type: "server_error",
action: "Retry with exponential backoff",
retryable: true
};
}
return {
type: "unknown",
action: "Check error message for details",
retryable: false
};
}Related Resources
- Provider Routing - Learn how routing and retries work
- Fallback Chains - Configure model-level fallbacks with the
Cova-Fallbacksheader - Settings: Provider Keys - Add your provider API keys
- Credits - Add CoreValue credits for Pass-Through Billing
Need Help? If you're seeing unexpected errors or need assistance configuring fallback, contact us at support@corevalue.dev