Your AI API is failing at 11pm? Here's what's really happening

python dev.to

401 at 11pm? Before blaming the provider and going to bed angry, read this: the three villains are always the same — and each has a quick fix.

Nothing stalls a project like an API error at 11pm. Here are the three most common ones when calling LLM APIs — and the fix for each.

1. HTTP 401 — Unauthorized
Invalid or expired API key (a few providers also report balance issues as 401; most use 429/402/403 instead). Verify the key, the base_url, and test with curl first.

2. HTTP 429 — Rate limit / quota
You hit requests-per-minute or tokens-per-minute limits. Respect Retry-After and retry with exponential backoff:

import time, random

def retry(fn, attempts=5):
    for i in range(attempts):
        try:
            return fn()
        except Exception:
            time.sleep(min(2 ** i, 60) + random.random())
    raise RuntimeError("failed after 5 attempts")
Enter fullscreen mode Exit fullscreen mode

3. Timeout / Connection reset
Flaky network or a too-long synchronous call. Raise the timeout and use stream=True so you don't wait for the full response.

Bonus: 400 errors are usually malformed payloads — missing role, context longer than supported. Print the error body; most providers return a readable message.


Want to try these models in your project? **ModelKiwi* gives you access to GPT, Claude and Gemini with PIX payment (no international credit card needed) and free credits to start: https://www.modelkiwi.com. WhatsApp: +5521999500402 — and join our channel: https://t.me/ModelkiwiOfficial.*

Source: dev.to

arrow_back Back to Tutorials