API Reference
Errors & Retries
Errors & Retries
What the API returns when something goes wrong, and how to recover.
Error shape
Every error has the same envelope, whatever its cause. request_id is always present, and retry_after_ms appears when waiting will help.
{
"error": {
"type": "rate_limit_exceeded",
"code": "rate_limit_exceeded",
"message": "Too many requests.",
"request_id": "req_...",
"retry_after_ms": 12000
}
}Status codes
| Status | Meaning | What to do |
|---|---|---|
| 400 / 422 | Something in the request is wrong. For 422, details names each field. | Fix the request; don't retry unchanged. |
| 401 | The API key is missing, invalid or expired. | Check the X-API-Key header and that the key is Active on the API Keys page. |
| 402 | Your credit balance is empty (code insufficient_credit). | Add credit in AI Studio, then retry. |
| 403 | The key isn't allowed to do this. | Check the key and the endpoint. |
| 413 | The request is too large. | Send less, or fewer or smaller files. |
| 429 | Too many requests, a quota was reached, or your own spend limit was (code spend_limit_reached). | Wait for the Retry-After time, then retry. See below. |
| 502 | Aivana couldn't complete the answer. You aren't charged for it. | Safe to retry. |
| 504 | The request took too long. | Retry, possibly with a lower effort or a shorter request. |
JSON Schema (Strict) errors
| Code | Status | Meaning | What to do |
|---|---|---|---|
| invalid_response_schema | 422 | Your schema is malformed or uses a feature Aivana doesn't support. It is refused before anything runs, and nothing is charged. | Fix the schema; see Parameters. Retrying unchanged won't help. |
| structured_output_streaming_not_supported | 422 | A JSON Schema response was sent to the streaming endpoint. | Use Generate instead. |
| structured_output_failed | 502 | Your schema was valid, but Aivana couldn't produce a response that matches it. You get an error rather than a response that doesn't match, and you aren't charged. | A retry can succeed. If it keeps failing, loosen the schema (fewer constraints, nullable fields) or give the request more room with a higher max_tokens. |
Rate limits
When your workspace sends requests or tokens faster than its limits allow, the API answers 429 with a Retry-After header (in seconds) and retry_after_ms in the body. What the limits are, how they rise, and how to see yours are on Rate Limits.
Your purchased credit spend limit
You can set a purchased credit spend limit on your account, and on each API key, on the Billing and API Keys pages. Choose a daily, weekly or monthly limit (all in UTC; a week runs Monday to Sunday; Monthly is the first choice offered). It protects you from a bug that sends requests in a loop. It counts only what you spend from purchased credit: usage paid for with promotional tokens is never limited and does not count. Once what you have spent from purchased credit in that period reaches it, the API answers 429 with the code spend_limit_reached and a Retry-After header counting down to when the period starts again: 00:00 UTC tomorrow, next Monday, or the 1st of the next month. Requests already running when the limit is reached still finish and are charged, so a period can end a little over it. Raise or remove the limit and requests are accepted again within a minute. Retrying sooner than Retry-After won't help. Only you can set the limit; Aivana never does.
Retrying
- Retry only 429, 502 and 504, and network failures. Other 4xx errors won't succeed unchanged.
- Wait for the time in Retry-After (or retry_after_ms) when it is given; otherwise back off exponentially, for example 1, 2, 4, then 8 seconds, with a little random jitter.
- Stop after a few attempts and surface the error, with its request_id, to your user or logs.
- A request that failed on Aivana's side isn't charged, so a retry doesn't cost you twice.
from aivana import RateLimitError, UpstreamError
try:
res = aivana.generate("Summarize our Q3 churn drivers.")
except (RateLimitError, UpstreamError) as err:
print("Retry later:", err.message, err.request_id)Learn more
Managing API Keys
Learn how to create and revoke API keys in Aivana AI Studio.
Quickstart Guide
Get your key and make your first request in minutes.
Parameters
Every option the Generate API accepts.
Still have questions? We are here to help!