# AI Gateway Error Codes

URL: https://www.elchi.io/docs/ai-gateway/reference/error-codes

> Every error a client of Elchi AI Gateway can receive, with its HTTP status, body code, the component that emits it, and the JSON-RPC errors on /mcp.

This page lists every answer a client of an AI listener can get that is not a normal model response, and which component sent it. For the causes and fixes, see [Troubleshooting](https://www.elchi.io/docs/ai-gateway/troubleshooting).

## Who answers

A request passes through these components on the edge, in order. Each can answer the request itself:

```mermaid
flowchart LR
  C[Client] --> E1["Envoy<br/>host / route"]
  E1 --> K["authorization<br/>key phase"]
  K --> X["extproc<br/>(Agent Router)"]
  X --> M["authorization<br/>model or MCP phase"]
  M --> R["Envoy router"]
  R --> P["Provider / MCP server"]
```

| Emitter | When it runs | Body shape |
|---|---|---|
| **Envoy** (generated routes) | Every request: host selection, the model lists, the catch-all routes, body limits | OpenAI error shape, or empty |
| **Authorization, key phase** | Gateways with **Require key** on | OpenAI error shape |
| **Extproc** | Every LLM request: parses the body, picks the model, translates to the provider | The extproc's own |
| **Authorization, model phase** | Keyed gateways, and every gateway whose sensitive-data policy reads the request body | OpenAI error shape |
| **Authorization, MCP phase** | `POST /mcp` on a gateway that serves MCP (always keyed) | JSON-RPC error |
| **MCP result scan** | `/mcp` answers on a gateway whose policy scans tool results | JSON-RPC error inside a `200` |
| **Provider** | The request reached the provider | The provider's own, passed through |

## The error body

The authorization answers in OpenAI's error shape, so OpenAI and Anthropic SDKs surface `error.code` and `error.message`:

```json
{
  "error": {
    "message": "consumer team-a used its quota of 1000 requests per day; it resets at 2026-12-06T00:00:00Z",
    "type": "rate_limit_error",
    "code": "quota_exceeded"
  }
}
```

`type` follows the HTTP status:

| Status | `type` |
|---|---|
| 401 | `authentication_error` |
| 404 | `invalid_request_error` |
| 429 | `rate_limit_error` |
| 503 | `server_error` |
| any other (400, 403, 413) | `permission_error` |

Every refusal from the authorization carries `content-type: application/json`. Codes that tell the client when to try again add a `retry-after` header, in seconds.

## Authorization: key phase

This phase runs **before** the extproc, only on gateways with **Require key** on. Clients send the consumer key as `Authorization: Bearer <key>` (OpenAI SDKs), `x-api-key` (Anthropic SDKs) or `api-key` (Azure OpenAI SDKs).

| Status | `code` | Message (verbatim, with values filled in) | Cause | `retry-after` |
|---|---|---|---|---|
| 401 | `invalid_api_key` | `a valid elchi virtual key is required for gateway <gw>` | No key, an unknown key, a revoked key, or a **disabled** consumer | — |
| 403 | `gateway_not_allowed` | `consumer <c> may not use gateway <gw>` | The consumer has a gateway allowlist that does not name this gateway | — |
| 403 | `files_not_allowed` | `the Files API is not available on gateway <gw>: files belong to the provider's account and would be shared by every consumer` | A path containing `/v1/files` on a keyed gateway | — |
| 429 | `quota_exceeded` | `consumer <c> used its quota of <limit>; it resets at <RFC3339 UTC>` | A request or token quota is used up (see the quota table below) | seconds until the reset |
| 503 | `gateway_not_ready` | `this edge's AI gateway configuration does not have gateway "<gw>" as one that requires a key yet; retry shortly` | Envoy already routes to a keyed gateway that this edge's configuration does not have as keyed. The edge's set lags behind Envoy: a new gateway, a push not yet applied, or a push the edge refused. | 5 |
| 503 | `service_unavailable` | `the AI gateway has no configuration yet` | The gateway on this edge has applied no configuration | — |
| 404 | `not_found` | `this route is not served by an AI gateway` | The request matched a route that names no gateway (an operator's own route on an AI HCM) | — |

`<limit>` in the quota message reads, for example: `60 requests per minute`, `1000 requests per day`, `2000000 tokens per day`, `50000000 tokens per month`, `25.00 per day`, `500.00 per month`. The request and token quotas are checked here. A request is **counted** in the model phase, so a request the extproc or the model phase refuses does not use the quota.

## Authorization: model phase

This phase runs **after** the extproc has named the model (`x-ai-eg-model`). It runs on keyed gateways, and on gateways whose sensitive-data policy reads the request body.

| Status | `code` | Message | Cause | `retry-after` |
|---|---|---|---|---|
| 401 | `invalid_api_key` | `a valid elchi virtual key is required for gateway <gw>` | The key phase did not admit a consumer | — |
| 403 | `model_not_allowed` | `consumer <c> may not use model <m> (provider <p>)` | The consumer's model or provider allowlist excludes it. For a model several providers serve, every provider that may serve it must be allowed. | — |
| 403 | `model_not_priced` | `consumer <c> has a cost quota and model <m> has no price at provider <p> (asked for <name>)` | The consumer has a cost quota (`costPerDay` / `costPerMonth`) and the price table has no price for this model at this provider | — |
| 403 | `sensitive_data` | `the request carries sensitive data the gateway <gw> does not allow: <types>; remove it and retry` | The policy **blocks** a type found in the body. The message names only the blocking types. | — |
| 403 | `sensitive_scan_overflow` | `the request carries more values that look like sensitive data than gateway <gw> checks; it cannot be checked for the types the gateway blocks` | The scan hit its candidate bound, and the policy blocks a type | — |
| 413 | `sensitive_scan_too_large` | `the request is larger than gateway <gw> checks for sensitive data; send a smaller request (its administrator may raise the policy's maxScanBytes)` | The body is larger than the scan reads, and the policy blocks a type | — |
| 503 | `sensitive_scan_busy` | `the AI gateway is busy checking other requests for sensitive data; retry shortly` | No scan slot was free before Envoy's 2 s check timeout | 1 |
| 503 | `sensitive_scan_incomplete` | `the request could not be checked for sensitive data in time; retry shortly` | The scan started but did not finish in time. What was not read is never let through. | 1 |
| 429 | `quota_exceeded` | as in the key phase | The request would cross a quota | seconds until the reset |
| 503 | `gateway_not_ready` | `this edge's AI gateway configuration does not serve model "<m>" on gateway <gw> yet; retry shortly` | Envoy routes the model, but this edge's set does not have it yet | 5 |
| 403 | `unknown_phase` | `the authorization phase "<p>" is unknown` | An Envoy filter was configured with a phase the gateway does not know. This does not happen with generated configuration. | — |

Order inside the phase: the allowlist and price checks run first, then the sensitive-data scan, then the quota. A request refused by the scan uses no quota. A policy that only **flags** a type lets the request through and records it.

## Envoy: generated routes

| Status | Body `code` | Message | Cause |
|---|---|---|---|
| 404 | `gateway_not_found` | `no AI gateway serves this host on listener <listener>` | The Host matches no gateway bound to this listener, and the listener has no default gateway |
| 404 | `model_not_found` | `the model is not served by gateway <gw>; GET /v1/models lists the ones it serves` | The model in the body is not in the gateway's model list |
| 404 | (empty body) | — | The request reached the router without the extproc naming its model (the gateway's unrouted route) |
| 413 | (Envoy's) | — | The request body is larger than 50 MiB, the most a gateway buffers |
| 308 | — | — | A plain-HTTP port in **redirect** mode. Every request is redirected to the listener's HTTPS port, with method and body kept. |

`GET /v1/models` and `GET /anthropic/v1/models` are answered by Envoy from the gateway's model list (`200`). On a keyed gateway they still need a valid key: without one they get the key phase's `401`.

## Extproc

The Agent Router extproc answers these itself. Its bodies are the engine's own, not elchi's shape.

| Status | Cause |
|---|---|
| 400 | The request body is not valid JSON |
| 422 | `unsupported API schema`: the endpoint cannot be translated to the model's provider type. Example: `/v1/chat/completions` to a native `anthropic` provider. See the endpoint × provider matrix in [Calling the Gateway](https://www.elchi.io/docs/ai-gateway/calling-the-gateway). |
| 500 | An MCP call: `call to <server> failed …`. The server was unreachable, or it answered an error (its body is relayed as `text/plain`). |
| 503 | An MCP answer or SSE stream took longer than the gateway's MCP write timeout (default 15 minutes) |

Requests that arrive while the gateway or its extproc restarts fail (measured as `500`). The AI filters are fail-closed, so no request goes out without its checks.

## Provider answers

What a provider answers is passed to the client. The gateway records each answer per minute in `elchi_ai_gateway_upstream` with a class: `ok`, `auth_error` (401/403), `rate_limited` (429), `client_error`, `server_error` (5xx), `unreachable`, `timeout`, `reset`. A `401` or `429` that **the provider** sent is not one of the codes above. It has no elchi `code`, and the provider status on the **Providers** tab shows it.

For a model several providers serve, Envoy retries the gateway's failover failures on the other providers before the client sees anything. See [Gateways and Models](https://www.elchi.io/docs/ai-gateway/gateways-and-models).

## JSON-RPC errors on `/mcp`

On a gateway's `/mcp` endpoint, the authorization answers in JSON-RPC so MCP clients can read it. Elchi's code is inside `error.data`:

```json
{
  "jsonrpc": "2.0",
  "id": 7,
  "error": {
    "code": -32000,
    "message": "consumer team-a may not call tool github__delete_repo on gateway dev",
    "data": { "code": "tool_not_allowed", "type": "permission_error", "status": 403 }
  }
}
```

- `id` is the id of the refused message, or `null` when no id was read.
- `error.code` is always `-32000`.
- For the MCP phase, the HTTP status is `data.status`. For the result scan, the HTTP status is `200` and the error replaces the blocked message.

### MCP phase (requests the client sends)

| HTTP / `data.status` | `data.code` | Message | Cause |
|---|---|---|---|
| 401 | `invalid_api_key` | `a valid elchi virtual key is required for gateway <gw>` | No valid consumer key |
| 400 | `invalid_request` | `the request is not a JSON-RPC message` | Not JSON-RPC, or a body that two parsers could read differently (a key differing only in case, a duplicate key, data after the message) |
| 403 | `tool_not_allowed` | `consumer <c> may not call tool <server>__<tool> on gateway <gw>` | A `tools/call` for a tool the consumer was not granted |
| 413 | `mcp_request_too_large` | `the MCP request is larger than gateway <gw> reads` | Envoy passed only part of the body (MCP requests are read up to 4 MiB) |
| 429 | `quota_exceeded` | `consumer <c> used its quota of <n> MCP calls per minute` (or `per day`) `; it resets at …` | An MCP call quota is used up. A batch counts each `tools/call` it holds. |
| 403 / 413 / 503 | `sensitive_data`, `sensitive_scan_overflow`, `sensitive_scan_too_large`, `sensitive_scan_busy`, `sensitive_scan_incomplete` | as in the model phase | The policy scans MCP arguments (`sensitive.mcp.arguments`) |
| 503 | `gateway_not_ready` | `this edge's AI gateway configuration does not serve MCP on gateway <gw> yet; retry shortly` | The edge's set has no MCP for this gateway yet |
| 503 | `gateway_not_ready` | `the gateway <gw> did not pass the MCP request's body to its check; retry shortly` | A `POST` reached the check without its body |

`initialize`, `tools/list` and other methods are not counted against quotas. The extproc lists only the tools the consumer was granted.

### MCP result scan (answers from the server)

These run only where the gateway's policy scans tool results (`sensitive.mcp.results`):

| `data.status` | `data.code` | When |
|---|---|---|
| 403 | `sensitive_data` | The answer carries a type the policy blocks. The answer is replaced by this error. A blocked request or notification from the server is dropped. A blocked server request answers the client's call with this error. |
| 403 | `sensitive_scan_unreadable` | The policy blocks, and the answer cannot be read: content-encoded, or a content type that is not JSON, SSE or text |
| 413 | `sensitive_scan_too_large` | The policy blocks, and the answer (or one SSE event) is larger than the scan reads |
| 403 | `sensitive_scan_overflow` | The policy blocks, and the scan hit its candidate bound |

The message of the unreadable and too-large refusals is: `the tool's result could not be checked for sensitive data by gateway <gw> (it blocks some types); ask for a smaller result`. Where the policy only flags, such answers pass unscanned.

## Where refusals are recorded

| Record | What |
|---|---|
| Decision log, ClickHouse `elchi_ai_gateway_decisions` | Every denial and every flagged request: gateway, consumer, phase (`key`, `model`, `mcp`, `mcp_result`), status, code, and the sensitive types with counts (never values). A denial that names no consumer is recorded at 5 per second per gateway and code (burst 20). The rest are counted in the metric `elchi_ai_gateway_decision_rows_suppressed_total` on `127.0.0.1:9002/metrics`. |
| Journal of `elchi-ai-gateway` | A known consumer's denial is logged at info as `request denied` with consumer, gateway, phase, status, code and message, at most once a minute per consumer and code. The line carries `suppressed` = how many were not logged. |
| UI | **AI Gateway → Sensitive data** shows the sensitive-data decisions. **AI Gateway → Usage** shows denials per window. |
