Skip to content

Error Handling

Error response format

{
  "error": {
    "message": "Invalid API key",
    "type": "authentication_error",
    "code": 401
  }
}

Common errors

Status Meaning Fix
401 Invalid or missing API key Check your Authorization header
400 Bad request Check required parameters
429 Rate limit exceeded Slow down requests or upgrade your plan
500 Server error Retry after a short delay
503 Service unavailable The model may be loading; retry

Retry with exponential backoff (Python)

import time
import requests

def call_with_retry(api_key, payload, max_retries=3):
    url = "https://api.studiolm.dev/v1/chat/completions"
    headers = {"Authorization": f"Bearer {api_key}"}

    for attempt in range(max_retries):
        try:
            response = requests.post(url, headers=headers, json=payload, timeout=60)
            if response.status_code == 200:
                return response.json()
            if response.status_code in (429, 500, 503):
                wait = 2 ** attempt
                print(f"Status {response.status_code}, retrying in {wait}s…")
                time.sleep(wait)
                continue
            response.raise_for_status()
        except requests.exceptions.Timeout:
            print(f"Timeout on attempt {attempt + 1}")
        except requests.exceptions.ConnectionError:
            print(f"Connection error on attempt {attempt + 1}")

    raise RuntimeError(f"Request failed after {max_retries} attempts")