TTodAPI

Blog

429 vs 402: rate limit or empty balance

Both return a 4xx, both look like "the API said no", and both are routinely lumped into the same retry branch. They are opposites: one clears by itself, the other stays broken until a human tops up the balance.

What each one means

StatusCauseClears on its own?Correct reaction
429Requests per window, or open concurrencyYes, in secondsBack off with jitter, retry
402Prepaid balance exhaustedNoTop up, then retry once

The failure mode is symmetric: treat 402 as retryable and you get a tight loop that never succeeds; treat 429 as fatal and you drop requests that would have worked 800 ms later.

How they differ in your logs

A 429 usually arrives in bursts, clustered in time, and correlates with concurrency. A 402 arrives all at once and then never stops — the same key failing consistently while other keys on the account fail too, because balance is account-level.

SELECT status, count(*), min(ts), max(ts)
FROM requests
WHERE ts > now() - interval '1 hour'
GROUP BY status
ORDER BY count(*) DESC;

If your 4xx column is dominated by one status and the timestamps span the whole hour, it is 402. If they are clustered, it is 429.

The retry wrapper that handles both

import random, time
from openai import OpenAI, RateLimitError, APIStatusError

client = OpenAI(api_key="sk-...", base_url="https://todapi.work/v1")

def call(**kwargs):
    delay = 0.5
    for attempt in range(6):
        try:
            return client.chat.completions.create(**kwargs)
        except RateLimitError:              # 429
            if attempt == 5:
                raise
            time.sleep(delay + random.uniform(0, delay / 2))
            delay = min(delay * 2, 8)
        except APIStatusError as exc:
            if exc.status_code == 402:      # balance — retrying is pointless
                raise SystemExit("balance exhausted, top up before retrying")
            raise

Most SDKs already treat 4xx as permanent, but hand-rolled retry helpers usually do not — they match on "not 2xx" and retry everything.

Alerting, not retrying

The only useful automated response to 402 is a page: the balance crossed a threshold. Alert on the first 402 of the hour, not the thousandth. For 429, alert on the ratio — a small percentage is healthy backpressure, a sustained 20% means the client or the quota is wrong.

Why this matters for cost

Every retry is a round trip you paid for in latency. A retry loop against an empty balance produces no tokens, no revenue, and a log full of noise that hides the next real incident. In a prepaid model, the balance is a resource you monitor like disk space.

FAQ

Is 402 the same as payment required?

Yes — HTTP 402 is "Payment Required", and here it means the prepaid balance is zero.

Can a 429 ever be permanent?

Not on this gateway; it is a window. If it never clears, you are looking at a 402 or a 401 that your client is mislabelling.

Do failed retries get billed?

Rejected requests cost nothing. You pay for tokens the model actually produced.

429 reference

Concurrency, backoff and what not to do.

402 reference

Balance semantics and top-up methods.

Streaming and limits

Why long answers consume concurrency.