# API errors and rate limits

> What each AskVault API error status means, how to retry safely, and the per-key request limits, so your integration handles failures without losing answers.

AskVault API errors return a JSON body with a `detail` field (a string, or an object with an `error_code` for a 402), and the HTTP status tells you what to do. Retry rate limits and server errors with backoff, and fix the request for 401, 402, 403, 404 and 422.

## Error shape

```json
{ "detail": "Invalid or revoked API key." }
```

There is no machine-readable error code field, so branch on the HTTP status and treat `detail` as a message for people. A 402 from a question endpoint is the exception: its `detail` is an object with an `error_code` of `messages_exhausted` or `model_locked`.

Every response, successful or not, carries an `X-Request-ID` header. Log it and include it when you contact support. Unhandled server errors repeat it in the body as `request_id`.

## Status codes

| Status | When | What to do |
|---|---|---|
| **401** | The API key is missing, malformed, revoked or inactive. | Check the `Authorization: Bearer ak_...` header. Do not retry. |
| **402** | Your account is out of message credits, or the workspace's model is not on your plan. | Buy credits, upgrade or change the model. Do not retry until fixed. |
| **403** | Your plan does not include API access. | The API starts on Starter. |
| **404** | The key's workspace or its owner no longer exists, or a conversation id does not belong to your workspace. | Check the id, or create a new key. |
| **409** | A rating was already recorded for that conversation. | Nothing to do. The first rating wins. |
| **422** | The body failed validation, such as a missing or over-long `query`, or a `top_k` outside 1 to 20. | Fix the body. Do not retry unchanged. |
| **429** | The key's per-minute or per-day limit was hit, or your monthly message quota is used up. | Read `detail`. Back off on a rate limit. A quota block does not clear by retrying. |
| **500** | Something failed on our side. | Retry with backoff. Contact support if it persists. |
| **502, 503, 504** | An upstream service or the network failed. | Retry with backoff. |

## Retry safely

Retry only 429 (rate limit), 500, 502, 503 and 504. Wait longer after each attempt, add a little random jitter, and stop after a handful of tries.

```python

def ask(query: str, key: str, attempts: int = 5):
    for attempt in range(attempts):
        r = requests.post(
            "https://api.askvault.co/v1/query",
            headers={"Authorization": f"Bearer {key}"},
            json={"query": query},
            timeout=60,
        )
        if r.status_code < 400:
            return r.json()
        if r.status_code not in (429, 500, 502, 503, 504):
            r.raise_for_status()
        time.sleep(2 ** attempt + random.uniform(0, 1))
    r.raise_for_status()
```

A 429 can mean your monthly message quota is used up. Retrying will not help until the quota resets or you add credits, so check `detail` before looping.

## Rate limits

Limits belong to the API key, not to your plan. The only plan requirement is Starter or above.

| | Requests per minute | Requests per day |
|---|---|---|
| A new key | 60 | 1,000 |
| The most a key can be set to | 1,000 | 100,000 |

- Two keys on one workspace have separate budgets, so give each integration or tenant its own key.
- Each question request (`/v1/query`, `/v1/query/stream`) and each handoff call reserves one unit of the key's minute and day limits when it arrives. Server errors give the unit back. Other rejections can still use it.
- Message credits are separate. They are used only when an answer is written. See [Usage and message credits](/settings/usage/).
- Preflight `OPTIONS` requests do not count. Webhook deliveries from AskVault to you do not count either.

When you go over a limit, you get a 429 with a message naming the limit, such as "Per-minute rate limit exceeded (60/min). Try again shortly." These per-key 429 responses do not carry a Retry-After header or rate-limit headers, so keep your own count and read `detail`. After a per-minute limit, back off for up to a minute. After a per-day limit, wait for the 24-hour window, which starts with the first request after the last reset.

To change a key's limits, see [API keys](/developers/api-keys/).

## Good habits

- **Back off on 429.** Most limit trouble comes from tight retry loops.
- **Pace yourself.** Count requests per minute and slow down before the cap.
- **One key per consumer.** A noisy script then cannot starve your production traffic.
- **Do not send warm-up questions.** Each one uses your limits and credits.

## If it doesn't work

**401 with a key that looks right.** The header must be exactly `Bearer ak_...`. See [API keys](/developers/api-keys/).

**Constant 429 at low traffic.** Several processes share one key. Give each its own.

**Empty `answer`.** No text was generated. Retry once, then contact support with the `query_id`.

## Related

- [Ask a question with the API](/developers/query/)
- [AskVault API: get started](/developers/)
- [Webhooks](/developers/webhooks/)

Source: https://docs.askvault.co/developers/errors-and-limits/
