Skip to main content

Error Response Format

All errors return a JSON object with this structure:
The HTTP status code matches the error type. Request validation errors return non-200 status codes. Errors occurring during generation may be returned in the response body or as SSE events (for streaming).

HTTP Status Codes

400 Bad Request

Invalid request parameters, malformed JSON, or missing required fields.
Common causes:
  • Missing required fields (model, messages)
  • Invalid parameter values (e.g., temperature > 2.0)
  • Malformed JSON
  • Invalid message format

401 Unauthorized

Invalid, expired, or missing API key.

402 Payment Required

Insufficient credits or account balance.

403 Forbidden

Content moderation failure. Input was flagged by safety systems.

408 Request Timeout

Request exceeded timeout limit.

429 Too Many Requests

Rate limit exceeded.
Check rate limits via GET /v1/key. See Limits documentation.

502 Bad Gateway

Model provider error or invalid response.

503 Service Unavailable

No available provider meets routing requirements.

Error Types

invalid_request_error

Request validation failed. Check parameter types and required fields.

authentication_error

API key authentication failed. Verify key is valid and active.

insufficient_quota_error

Account lacks sufficient credits. Add credits to continue.

rate_limit_error

Request rate exceeds limits. Implement exponential backoff.

content_filter_error

Input flagged by content moderation. Review input content.

timeout_error

Request exceeded timeout. Retry with shorter context or simpler request.

provider_error

Model provider returned an error. May be transient - retry request.

service_unavailable_error

No providers available matching routing requirements. Adjust provider preferences or retry later.

Error Metadata

Some errors include metadata for debugging:

Moderation Errors

Provider Errors

Error Handling Examples

Python

TypeScript

Retry Logic

Implement exponential backoff for transient errors:

Streaming Errors

In streaming responses, errors are sent as SSE events:
Parse and handle errors in your SSE event loop.

Best Practices

  1. Check Status Codes: Always verify HTTP status before parsing JSON
  2. Handle 429: Implement exponential backoff for rate limits
  3. Log Metadata: Include error metadata in logs for debugging
  4. User-Friendly Messages: Translate technical errors to user-friendly messages
  5. Retry Transient Errors: Retry 502, 503, and 408 errors
  6. Monitor Credits: Track 402 errors and alert when credits are low

See Also