Guides

Error Handling

Handle errors gracefully and implement retries so your application stays resilient.

Error response format

All errors return a JSON body with a detail field:

JSON
{"detail": "insufficient wallet balance"}

Which errors to retry

429YesRate limited. Wait and retry with exponential backoff.
502YesUpstream error. All available routes for the model failed. Retry after a short wait.
503YesGateway temporarily unavailable. Retry with backoff.
402NoWallet empty. Top up at the dashboard before retrying.
401NoInvalid API key. Check the key and do not retry.
422NoBad request body. Fix the payload before retrying.

Retry with exponential backoff

retry.py
from inferexai import InferexAI, InferexAIError
import time

client = InferexAI(api_key="sk-live-your-key")

def chat_with_retry(messages, max_retries=3):
    for attempt in range(max_retries):
        try:
            return client.chat.completions.create(
                model="default",
                messages=messages,
            )
        except InferexAIError as e:
            if e.status == 402:
                raise RuntimeError("Wallet empty — top up at the dashboard") from e
            if e.status == 429 or e.status >= 500:
                wait = 2 ** attempt          # 1s, 2s, 4s
                print(f"Retry {attempt + 1} after {wait}s...")
                time.sleep(wait)
                continue
            raise                            # 401, 422 — don't retry
    raise RuntimeError("Max retries exceeded")