Error Response Format
When the gateway can return a normal HTTP response, errors use a shared JSON envelope:Error response
error.code. Display error.message to developers, but do not build application logic by matching message text. When debugging, log the complete response and the X-Request-ID or Planck-Id response header.
How Fallback Works
The AI Gateway supports two billing methods:Pass-Through Billing (PTB)
Pay-as-you-go with Planck credits. Simple, no provider account needed.
Bring Your Own Keys (BYOK)
Use your own provider API keys. You’re billed directly by the provider.
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” (402). You chose BYOK for a reason!
Common Error Responses
Fallback Scenarios
Scenario 1: BYOK Succeeds
Scenario 1: BYOK Succeeds
Setup: You have a valid provider key configured or providedResult: ✅ Request completes using Bring Your Own KeysError: None - successful response
Scenario 2: BYOK Fails, PTB Succeeds
Scenario 2: BYOK Fails, PTB Succeeds
Setup: Your provider key fails, but you have Planck credits and a managed route is availableResult: ✅ Request completes using Pass-Through BillingError: None - successful response
Scenario 3: BYOK Fails, PTB Fails
Scenario 3: BYOK Fails, PTB Fails
Setup: Your provider key fails and PTB is unavailable or also failsResult: ❌ Request failsError Returned: The most actionable error from the failed attemptsWhy: If your provider key is invalid, that error is usually more actionable than a generic fallback failure.Example:
Scenario 4: No BYOK Configured, PTB Fails
Scenario 4: No BYOK Configured, PTB Fails
Setup: No Planck credits and no provider keys configuredResult: ❌ Request failsError Returned: 402 Insufficient creditsWhy: No alternative billing method availableExample:Solutions:
- Add Planck credits
- Configure provider keys
- Enable automatic retries with
Planck-Retry-Enabled: trueto handle transient failures
Retries can help! If you’re experiencing temporary rate limits or server errors, use Planck retry headers to automatically retry failed requests with exponential backoff.
Understanding Error Sources
When you see an error, you can determine which billing method it came from: PTB Errors:- 402: “Insufficient credits” → Add credits
- 403: “Wallet suspended” → Contact support
- 401: “Invalid API key” → Check provider keys
- 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 Planck credits to avoid 402 errors during critical requests.
Enable Automatic Retries
Use Planck retry headers to automatically retry transient errors (429, 500, 503) with exponential backoff.
Log Error Details
Log the full error response to debug provider-specific issues quickly.
Errors During a Stream
Before streaming begins, failures return a non-2xx HTTP response using the JSON envelope above. After SSE headers are committed, the HTTP status can remain200; Chat Completions sends an error object in a data: frame and then terminates the stream:
Chat Completions stream error
error event. See Native Anthropic streaming.
If partial output is already visible, retry only if your UI first removes or replaces that output. Otherwise, users can receive duplicated text.
Error Handling in Code
Retry Logic Example
Error Classification
Related Resources
- Automatic Retries - Configure retry headers for handling transient failures
- Provider Routing - Learn how to configure fallback providers
- Settings: Provider Keys - Add your provider API keys
- Credits - Add Planck credits for Pass-Through Billing
Need Help? If you’re seeing unexpected errors or need assistance configuring fallback, contact us at support@inquantum.ai.