Tangani Error, Kuota & Rate Limit
Kode error 401/403/429/5xx dan pola retry yang benar.
1. Kenali kode error-nya
Semua error berbentuk {"error": {"message", "type", "code?"}}. 401 = key salah/hilang. 403 = akun disuspend, model eksklusif paket (mimo/minimax), atau model di luar paket Anda. 429 = kuota habis atau rate limit — cek error.code (monthly_exhausted, rate_limit_exceeded, dll) dan reset_in. 5xx = gangguan provider, aman untuk retry. Detail lengkap di API Reference.
2. Jangan retry 401/403 — perbaiki dulu
Retry hanya untuk 429 dan 5xx. Untuk 401 putar key baru di /ai/api-keys; untuk 403 model_requires_package berlangganan paket atau ganti model pay-as-you-go.
3. Pola backoff (Python)
Contoh retry dengan jeda bertambah untuk 429/5xx, maksimal 4x percobaan.
import time, requests
def ask(messages, tries=4):
for i in range(tries):
r = requests.post(
"https://sahabatmobile.com/api/v1/chat/completions",
headers={"Authorization": "Bearer sk-ISI_KEY_ANDA"},
json={"model": "google/gemini-2.5-flash", "messages": messages},
timeout=60,
)
if r.status_code in (429,) or r.status_code >= 500:
time.sleep(2 ** i) # 1, 2, 4, 8 detik
continue
r.raise_for_status()
return r.json()
raise RuntimeError("AI sibuk, coba lagi nanti.")
4. Pantau saldo & kuota
Cek sisa kredit di /ai/billing dan batas paket di /ai/plan-details. Untuk pemakaian besar, aktifkan paket agar tarif per token lebih murah dan limit lebih longgar.