# Errors

> Status codes, the error body, rate limits and request size limits.

Source: https://openpipe.health/docs/api/errors/

Every error is JSON with an `error` message:

```json
{ "error": "NPI must be 10 digits." }
```

Some errors add a machine-readable `code`:

```json
{ "error": "Too many requests. Try again in 23s.", "code": "RATE_LIMITED" }
```

`code` is present on `429` (`RATE_LIMITED`, or `REFRESH_BUSY` when a workspace already has two [refreshes](https://openpipe.health/docs/api/saved.md#refreshing-is-free-but-rationed) running), on `413` (`TOO_LARGE`), on `502` from Ask (`ASK_FAILED`) and on errors from the [sign-in endpoints](https://openpipe.health/docs/authentication.md#browser-sign-in) under `/api/auth/`. Other errors have only `error`.

The message is written to be shown to a person. Branch on the status code, and on `code` where there is one, not on the text.

| Status | Meaning | What to do |
|---|---|---|
| `400` | The request is malformed, has a field the endpoint does not take, or an export could not run | Fix the request; the message says how. Not charged. |
| `401` | Missing, unknown, revoked or expired token | See [Authentication](https://openpipe.health/docs/authentication.md#failures) |
| `402` | Out of credits for the month | Wait for the monthly reset or raise your limit. See [Credits](https://openpipe.health/docs/credits.md#running-out). |
| `403` | Not a member of the workspace named in `X-Oph-Workspace`; key management called with an API key or by a member; a dashboard saved, renamed or deleted by an API key, or changed by a member who does not own it; `/api/profile` called with an API key | See [Authentication](https://openpipe.health/docs/authentication.md#failures), [Roles](https://openpipe.health/docs/workspaces.md#roles) and [who can do what](https://openpipe.health/docs/api/saved.md#who-can-do-what) |
| `404` | No provider, hospital, group, key, dashboard, dashboard tile or catalog dataset with that id or name. A dashboard in another workspace answers `404` too. So do the removed endpoints: `POST /api/sql` and everything under `/api/queries`. | Check the id. Lookups that miss are not charged. |
| `409` | The workspace already has 20 active API keys or 200 saved dashboards; a dashboard with no tiles was refreshed | Remove one first, or see [Saved dashboards](https://openpipe.health/docs/api/saved.md) |
| `413` | Request body larger than 64 KB, or 8 KB under `/api/profile` | Send a smaller body. See [Request size](#request-size). |
| `429` | Rate limited | Wait the number of seconds in `Retry-After`. See [Rate limits](#rate-limits). |
| `502` | [Ask](https://openpipe.health/docs/api/ask.md#json-mode) in JSON mode: the model could not plan an answer. `code` is `ASK_FAILED` and `charged` is `false`. | Rephrase the question, or name the procedure, drug or state you mean. Nothing was charged. |
| `500` | `Internal error.` | Retry once. If it repeats, check [`/api/health`](https://openpipe.health/docs/api/meta.md#health). |

The [MCP server](https://openpipe.health/docs/mcp.md#errors) reports errors differently: JSON-RPC errors for protocol problems, and tool results marked `isError` for failed tool calls.

## Order of checks

A request under `/api/` passes these checks in order, and stops at the first that fails:

1. Rate limit per client address (`429`)
2. Request size (`413`)
3. Authentication and workspace (`401`, `403`)
4. Rate limits per workspace (`429`)
5. Credit pre-check, on metered endpoints only (`402`)
6. The endpoint's own validation and work (`400`, `404`, and `402` if this call would pass the limit)

Two consequences:

- A workspace that has already spent its allowance gets `402` from every metered endpoint before the request is validated or anything runs. See [Running out](https://openpipe.health/docs/credits.md#running-out).
- Requests that fail authentication still count against the per-address limit.

## SQL is not accepted

No endpoint and no MCP tool takes SQL. `POST /api/sql` and `/api/queries` answer `404`, [`POST /api/exports`](https://openpipe.health/docs/api/exports.md#errors) answers `400` to a body with a `sql` field, and the endpoint that renames or saves a dashboard answers `400` to any field other than `name` and `saved`. The `sql` fields in responses are [receipts](https://openpipe.health/docs/receipts.md).

## Errors inside an Ask stream

Once [`/api/ask`](https://openpipe.health/docs/api/ask.md) starts streaming, the HTTP status is already `200`. Failures arrive as events:

- `tile_error`: one tile failed. Others still arrive, and `done` reports the count in `failed`. A free [refresh](https://openpipe.health/docs/api/saved.md#refresh-a-dashboard) of the stored dashboard retries it.
- `error`: the answer could not start. With `code: "ASK_FAILED"` and `charged: false`, the model could not plan and nothing was charged; with `status: 402`, the balance ran out before the charge. The stream ends. See [errors inside the stream](https://openpipe.health/docs/api/ask.md#errors-inside-the-stream).

In [JSON mode](https://openpipe.health/docs/api/ask.md#json-mode) the same failures are ordinary responses: failed tiles are listed in `failed`, and a failed plan is a `502`.

## Rate limits

Requests are rate limited. The limits stop bursts and runaway loops; the hard cap on spend is still your [credits](https://openpipe.health/docs/credits.md).

| Applies to | Limit | Counted per |
|---|---|---|
| Every request under `/api/`, including public and sign-in endpoints | 900 per minute | Client address |
| Every authenticated request under `/api/` | 300 per minute | Workspace |
| `/api/ask`, together with the MCP `ask` tool | 10 per minute | Workspace |
| `POST /api/exports` (listing with `GET` counts only toward the 300) | 10 per minute | Workspace |
| [Refreshing](https://openpipe.health/docs/api/saved.md#refreshing-is-free-but-rationed) a dashboard | 20 per minute, and 2 running at once | Workspace |
| [`/api/catalog`](https://openpipe.health/docs/data/catalog.md#limits) | 240 per minute | Workspace |
| `/api/catalog/search` | 180 per minute | Workspace |
| `/api/catalog/:table/sample` | 40 per minute | Workspace |
| [`/api/profile`](https://openpipe.health/docs/api/profile.md#limits): 120 updates, 90 lookups, 12 market summaries, 6 suggestion builds per minute | See left | Signed-in person |
| [`/mcp`](https://openpipe.health/docs/mcp.md#limits) | 120 per minute | API key and client address together |

How they combine:

- **Limits stack.** A call to `/api/ask` counts against the per-address limit, the workspace's 300 and the workspace's 10. It must be under all three.
- **Workspace limits are shared.** Every key and every signed-in member of a workspace draws on the same counters.
- **Rejected requests count.** A request that gets `429` still counts toward the limits checked before it and the one that rejected it, though it cannot extend the window.
- **Windows are fixed.** A window starts at the first request and lasts 60 seconds. The count returns to zero when it ends; it is not a sliding average.

### Headers

Responses carry the state of one limit:

| Header | Meaning |
|---|---|
| `RateLimit-Limit` | Requests allowed per window |
| `RateLimit-Remaining` | Requests left in the current window |
| `RateLimit-Reset` | Seconds until the window ends |
| `Retry-After` | Only on `429`: seconds to wait. Same value as `RateLimit-Reset`. |

When several limits apply, the headers describe the last one checked. On a successful call that is the most specific: the 10 on `/api/ask`, the 300 on `/api/providers`, the 900 on a public endpoint such as `/api/health`. The refresh limit is the exception: a successful refresh reports the 300, and only its `429` reports the 20. On a `429` they describe the limit that rejected the request. On a `401` they describe the per-address limit, because the workspace is not yet known.

### The 429 response

```http
HTTP/1.1 429 Too Many Requests
RateLimit-Limit: 10
RateLimit-Remaining: 0
RateLimit-Reset: 23
Retry-After: 23
```

```json
{ "error": "Too many requests. Try again in 23s.", "code": "RATE_LIMITED" }
```

Nothing is charged for a `429`. Wait `Retry-After` seconds, then retry.

### Sign-in limits

The [sign-in](https://openpipe.health/docs/authentication.md#browser-sign-in) and [workspace](https://openpipe.health/docs/workspaces.md#workspace-endpoints) endpoints under `/api/auth/` have their own limits, counted over 10 minutes. Where an email is involved, the limit applies per client address and per email address separately.

| Action | Per address | Per email |
|---|---|---|
| Password sign-up | 20 | 8 |
| Password sign-in | 20 | 8 |
| Sending a code (`POST /api/auth/code`) | 10 | 4 |
| Checking a code (sign-in, verify and reset together) | 30 | 10 |
| Creating a workspace | 10 | |
| Creating an invitation | 30 | |
| Accepting or rejecting an invitation | 30 | |

Past one of these, the response is `429` with `Retry-After` and a body such as `{ "error": "Too many attempts. Try again in 412s.", "code": "RATE_LIMITED" }`. The `RateLimit-*` headers on these endpoints describe the general per-address limit, not these.

### If you run the server

- **Counters are in memory, per server instance.** On one long-running server the limits are exact. With several instances behind a load balancer, a client can get up to that many times each limit, and a restart clears the counters. The credit ledger is in Postgres and is exact everywhere.
- **The client address comes from `X-Forwarded-For`, counted from the right.** Set `TRUSTED_PROXY_HOPS` to the number of proxies in front of the server. The default is `1`: the address is the last entry in the header, the one your own proxy appended. With `0` the header is ignored and the socket address is used. A wrong value either lets clients choose their own address or puts every client behind one address.

## Request size

A request body under `/api/` can be at most 64 KB. A larger one gets:

```json
{ "error": "Request body is too large (64 KB limit).", "code": "TOO_LARGE" }
```

with status `413`. This is checked before authentication. The [MCP endpoint](https://openpipe.health/docs/mcp.md#limits) has its own limit of 256 KB, and the [profile endpoints](https://openpipe.health/docs/api/profile.md) a smaller one of 8 KB, checked after authentication.
