A failed request returns a failure envelope and charges nothing. Sample (the request id is an example from the errors guide):
{
"ok": false,
"error": { "code": "not_found", "message": "The platform says this profile does not exist.", "retryable": false },
"credits_charged": 0,
"request_id": "req_0123456789abcdef01234567"
}Branch on error.code and error.retryable. The message is for people and can change. The code stays.
Retry only when retryable is true. If a retry-after header is present, wait that many seconds. Otherwise back off, with a little jitter. Do not retry the rest. The same request will fail the same way. Retrying a failure costs nothing.
A POST you might retry, because a network error leaves you unsure it ran, needs an Idempotency-Key. The same key and the same body return the first answer again and do not charge twice. See batch and jobs.
When you write to support, include the request_id.
Full detail: Errors.