Skip to content

Error Codes

When an API request fails, xmAI returns a JSON error response with an appropriate HTTP status code.

Error Response Format

All errors follow a consistent format:

json
{
  "error": {
    "type": "error_type",
    "message": "Human-readable description of the error",
    "code": 401
  }
}
FieldTypeDescription
typestringMachine-readable error type
messagestringHuman-readable error description
codenumberHTTP status code

Error Types

HTTP StatusTypeDescription
401unauthorizedInvalid or missing API key
402insufficient_balanceInsufficient balance — please top up
403forbiddenNo permission to access this resource
429rate_limitedToo many requests — rate limit exceeded
503model_unavailable / service_unavailableModel or service is temporarily unavailable
500internal_errorInternal server error

Error Response Examples

401 Unauthorized

json
{
  "error": {
    "type": "unauthorized",
    "message": "Invalid API key provided",
    "code": 401
  }
}

402 Insufficient Balance

json
{
  "error": {
    "type": "insufficient_balance",
    "message": "Insufficient balance, please top up",
    "code": 402
  }
}

403 Forbidden

json
{
  "error": {
    "type": "forbidden",
    "message": "You do not have permission to access this resource",
    "code": 403
  }
}

429 Rate Limit Exceeded

json
{
  "error": {
    "type": "rate_limited",
    "message": "Rate limit exceeded, please retry after 60 seconds",
    "code": 429
  }
}

503 Model Unavailable

The type field is model_unavailable when a specific model cannot be reached, or service_unavailable for general service errors.

json
{
  "error": {
    "type": "model_unavailable",
    "message": "Model gpt-4o is temporarily unavailable, please try again later",
    "code": 503
  }
}

500 Internal Error

json
{
  "error": {
    "type": "internal_error",
    "message": "An unexpected error occurred",
    "code": 500
  }
}

Handling Errors

python
from openai import OpenAI, APIError

client = OpenAI(
    api_key="sk-your-xmai-key",
    base_url="https://api.xmai.sg/v1",
)

try:
    response = client.chat.completions.create(
        model="gpt-4o",
        messages=[{"role": "user", "content": "Hello!"}],
    )
except APIError as e:
    print(f"Error {e.status_code}: {e.message}")
javascript
import OpenAI from 'openai'

const client = new OpenAI({
  apiKey: 'sk-your-xmai-key',
  baseURL: 'https://api.xmai.sg/v1',
})

try {
  const response = await client.chat.completions.create({
    model: 'gpt-4o',
    messages: [{ role: 'user', content: 'Hello!' }],
  })
} catch (error) {
  if (error instanceof OpenAI.APIError) {
    console.log(`Error ${error.status}: ${error.message}`)
  }
}

Best Practices

  • 401: Check that your API key is correct and included in the Authorization header
  • 402: Top up your balance at console.xmai.sg/wallet
  • 429: Implement exponential backoff and retry logic
  • 503: Retry with a different model or wait and retry
  • 500: Contact support if the error persists

The Unified API for LLMs