errors

read ok, not the status

Every json body the api sends has ok, except the 422 for a missing parameter. Branch on that, not on the status code.

a failure
{
  "ok": false,
  "detail": "Insufficient credits. Please top up to your account."
}

what each status means

statuswhen
200it worked
400everything else, including running out of credits
401the key is unknown
422a required parameter is missing - this body has no ok
429you are over your requests-per-minute limit
500our bug - the body carries a request_id, send it to support
503we are overloaded - retry after Retry-After (1 second), it costs nothing

A 5xx without a json body comes from a proxy or from load shedding, not from the api. Treat it like a 503: wait, then retry.

noteOut of credits is a 400. There is no 402. Clients that special-case 402 will never see it.

A 400 we could not avoid still costs a fraction of a credit - the attempt was made. Credits lists it. A 429 costs nothing.