# Codex says "unexpected status …": read the line, then fix the cause

> Markdown mirror of https://router.one/codex-unexpected-status for AI assistants and crawlers. Router One is a unified, OpenAI-compatible LLM API gateway.
> Last updated: 2026-11-30

`unexpected status <code reason>: <body…>` is Codex CLI's general line for non-2xx HTTP responses without a dedicated handler (openai/codex rust-v0.172.0, checked 2026-11-30). Read `url:` first to see where Codex sent the request, then check the body after the colon and the status table below. On standard Responses API calls, HTTP 400, 429, 500, and overloaded 503 use separate Codex messages instead of `unexpected status`.

## How to read an unexpected status line

In the Codex TUI, the final error appears in red after ■. Per `protocol/src/error.rs` (rust-v0.172.0, checked 2026-11-30), `UnexpectedResponseError` formats fields in this order, appending each trailing field only when present:

`Codex`

```text
■ unexpected status <status>: <error.message or body>, url: <request url>, cf-ray: <ray>, request id: <id>, auth error: <header>, auth error code: <code>
```

- Body: JSON `error.message` if present; otherwise trimmed raw body cut at 1,000 bytes (`...`), or `Unknown error` if empty.
- `url:` is the endpoint Codex called (`base_url` + `/responses`, or `wss://` when a WebSocket upgrade failed; wrapped WebSocket error events omit it). `api.openai.com/v1/responses` = OpenAI's API, `chatgpt.com/backend-api/codex/responses` = ChatGPT sign-in, `api.router.one/v1/responses` = Router One; any other host comes from a custom `base_url` or `openai_base_url` (which sends the built-in credentials, ChatGPT sign-in tokens included, there).
- The other fields come from response headers and appear only when the server sent them: `cf-ray:` (Cloudflare's ray ID), `request id:` (`x-request-id`, else `x-oai-request-id`), `auth error:` (`x-openai-authorization-error`) and `auth error code:` (the code inside `x-error-json`).
- Cloudflare 403: if a 403 body contains both `Cloudflare` and `blocked`, Codex rewrites the prefix to `Access blocked by Cloudflare. This usually happens when connecting from a restricted region (status 403 Forbidden)` while keeping `url:` and `cf-ray:`.

## Status codes, causes, and fixes

Match the status code with `url:` before editing config:

| Status | Common meaning | First check | Guide |
| --- | --- | --- | --- |
| 401 Unauthorized | Server rejected or received no valid key or token. | Check `url:` and `env_key` (set `ROUTER_ONE_API_KEY` in the shell or in `~/.codex/.env`, which wins over the shell). If the variable named by `env_key` is unset or empty, Codex prints `Missing environment variable` instead. | https://router.one/codex-401-unauthorized |
| 402 Payment Required | Gateway funds or key cap: wallet funds (including in-flight reservations), cumulative `maxSpend`, or daily/monthly period budget. | Read the message after the colon, then check the balance in Dashboard → Deposit and the key's caps in Dashboard → API Keys. `X-Billing-Funds-Reason`, when present, is a response header that Codex does not print, so keep the message and the `request id:` for support. Plan-covered calls draw plan quota. | https://router.one/llm-api-error-codes and https://router.one/llm-api-key-budgets |
| 403 Forbidden | Region or Cloudflare block, ChatGPT workspace denial, or, on Router One, a key with a model allowlist (`model="auto"` is refused with 403). | Check `cf-ray:`, region/workspace codes, or the key's model allowlist on Router One. | https://router.one/unsupported-country-region-territory and https://router.one/llm-api-key-budgets |
| 404 Not Found | Wrong path (missing/doubled `/v1`, or relay without `POST /v1/responses`), or unknown/retired model ID. | Check `url:` for `/v1/v1` or missing `/v1`, confirm `wire_api = "responses"` and native `/v1/responses`, then check `/models`. | https://router.one/codex-responses-api and https://router.one/openai-model-not-found |
| 413 Payload Too Large | Usually an intermediate reverse proxy, CDN, or custom gateway body-size cap — hit by large images or huge threads. | `url:` names the endpoint, not the hop that refused: it can be that server or a proxy in front of it. Drop large images, run `/compact` or start a new thread, or raise the limit on a proxy you run. | https://router.one/codex-context-window |
| 502 / 503 / 504 | Transient upstream, gateway, or proxy failure (503 `server_is_overloaded` prints a separate capacity line). | Wait out `Reconnecting... n/5`, check https://router.one/status or your provider's status page, and check local proxies. | https://router.one/llm-api-error-codes and https://router.one/codex-stream-disconnected |

- Most of these lines appear only after retries (rust-v0.172.0, checked 2026-11-30). A 5xx is retried at the HTTP layer first (`request_max_retries`, default 4), then every `unexpected status` is retried as a whole request (`stream_max_retries`, default 5; both capped at 100). Not retried: a 403 that denies your workspace or access program, and a 401 or 403 that rejects a runtime credential. So 401, 402, 404 and 413 also show `Reconnecting...` before the red line.

## Which HTTP status codes do NOT print unexpected status

On Responses API calls (`codex-api/src/api_bridge.rs`, rust-v0.172.0, checked 2026-11-30), four statuses have their own messages:

| Status | What Codex prints instead | Retried? | Guide |
| --- | --- | --- | --- |
| 400 Bad Request | Raw response body (or dedicated text for `context_length_exceeded`, `model_not_found`, policy codes, or invalid image) — never `unexpected status 400`. | No | Read the body; https://router.one/codex-responses-api |
| 429 Too Many Requests | One of these, never `unexpected status 429`: `You’ve hit your usage limit…` (ChatGPT plan), `Quota exceeded. Check your plan and billing details.` (API quota), a spend-limit line, `Flex capacity unavailable.`, an `upgrade to Plus` line, or `exceeded retry limit, last status: 429 Too Many Requests` (rate limit). | Limits/quota: no; rate-limit 429: only with `Retry-After` | https://router.one/codex-usage-limit and https://router.one/codex-exceeded-retry-limit |
| 500 Internal Server Error | `We’re currently experiencing high demand, which may cause temporary errors.` — never `unexpected status 500`. | Yes (HTTP + sampling backoff) | Retry later; check your provider's status page (Router One: https://router.one/status) |
| 503 (overloaded / slow_down) | `Selected model is at capacity. Please try a different model.` (`server_is_overloaded`), `Policy-restricted capacity is unavailable. Please try again later.`, or `rate limit exceeded: …` (`slow_down`). Plain 503s print `unexpected status 503`. | HTTP retries 503; sampling retries `server_is_overloaded` only with server advice | https://router.one/codex-model-at-capacity |

- 404 exception: JSON `error.code == "model_not_found"` on 404/400/403 prints only the message and does not retry; path 404s, empty 404s, and Router One's `RESOURCE_NOT_FOUND` print `unexpected status 404 Not Found`.

## Router One status codes and Codex config.toml

When `url:` is `https://api.router.one/v1/responses`, the provider block should look like this. The list under it gives the status meanings: they follow https://router.one/llm-api-error-codes, plus the per-key period budget (402) and model allowlist (403).

`~/.codex/config.toml`

```toml
# ~/.codex/config.toml
model = "gpt-6.2-sol"
model_provider = "router"

[model_providers.router]
name = "router"
base_url = "https://api.router.one/v1"
env_key = "ROUTER_ONE_API_KEY"
wire_api = "responses"

# in the shell that starts codex (or in ~/.codex/.env):
# export ROUTER_ONE_API_KEY="sk-your-router-one-key"
```

- 401: invalid or missing API key (revoked, expired, or unknown keys return 401, never 403).
- 402: insufficient wallet funds (or in-flight reservations), `maxSpend`, or period budget. A key's spend cap (maxSpend) limits what that key can charge to your wallet: once its cumulative wallet charges reach the cap, its wallet-billed requests return HTTP 402. Requests covered by your plan's quota draw from the account's shared plan quota and do not count toward any key's cap. A key can also carry a daily or monthly budget. It limits what that key can charge to your wallet in the period, resets at midnight Beijing time (UTC+8; monthly budgets on the 1st), and works alongside maxSpend: whichever runs out first stops the key's wallet-billed requests with HTTP 402. Requests covered by your plan's quota do not count toward it.
- 403: usually not a bad key. A key can be limited to a model allowlist: it can only call the models you list, model="auto" is refused with HTTP 403, and channel ids such as aws/ or azure/ have to be added separately.
- 404: missing/doubled `/v1` or a model ID not in `/models` (`RESOURCE_NOT_FOUND`).
- 502 / 503 / 504: upstream failed (502), route unavailable (503 `PROVIDER_UNAVAILABLE`, not proof of an unknown model), or upstream wait timed out (normalized 504 `PROVIDER_UNAVAILABLE` does not prove upstream returned 504). Check https://router.one/status.
- Dashboard → Logs records each request the gateway forwards to a model (model, tokens, wallet charge or plan quota used, latency and status). Requests refused before any model is called — a 401 for the key, a 402 for funds, a 429 from the key's or account's own limits, or a 400 or 404 for the model ID or endpoint — leave no record; a streamed request that failed before any output may have none either. Keep `request id:` (`X-Request-ID`) for support@router.one.

## FAQ

### Why does Codex say "unexpected status 404 Not Found"?

Check `url:` first: a 404 means a wrong path (missing or doubled `/v1`, or a relay that only speaks Chat Completions) or an unknown or retired `model` ID. In `~/.codex/config.toml`, put `model = "gpt-6.2-sol"` and `model_provider = "router"` above the first `[table]`, and `base_url = "https://api.router.one/v1"`, `env_key = "ROUTER_ONE_API_KEY"` and `wire_api = "responses"` in `[model_providers.router]`; leave `requires_openai_auth` off. A project `.codex/config.toml` ignores both `model_provider` and `model_providers` (Codex docs, checked 2026-11-30). See https://router.one/codex-responses-api and https://router.one/openai-model-not-found.

### Why does Codex say "unexpected status 502 Bad Gateway", including with DeepSeek or a proxy?

502 means a proxy or gateway got no valid response from the server behind it. `url:` names the endpoint Codex called, not the proxy that answered: in a user report (openai/codex #56919) the 502 came from the macOS system proxy while `url:` showed a local provider. Test without local proxies, confirm the endpoint serves `/v1/responses` natively, and check https://router.one/status when `url:` is Router One.

### What does "unexpected status 403 Forbidden" mean in Codex?

A 403 is usually a policy or permission refusal rather than a bad key. On OpenAI or ChatGPT endpoints it can mean a region block (https://router.one/unsupported-country-region-territory), a Cloudflare block or a workspace denial. On Router One an invalid key returns 401; if the key has a model allowlist, check it first: the key can only call the listed models, and `model="auto"` is refused with HTTP 403 (https://router.one/llm-api-key-budgets).

### Why does Codex say "unexpected status 413 Payload Too Large"?

413 means the request body exceeded a byte cap — usually on a reverse proxy, CDN, or custom gateway rather than a model token limit (400 `context_length_exceeded`). Drop large images, run `/compact` or open a new thread (https://router.one/codex-context-window), or raise your proxy's limit.

### What causes "unexpected status 402 Payment Required" in Codex?

A billing gateway refused the call for funds or budget. On Router One, read the message after the colon: the wallet's available funds may be below the reservation (in-flight requests hold part of them), or the key's `maxSpend` or its daily or monthly budget is used up (daily budgets reset at midnight Beijing time, UTC+8; monthly ones on the 1st). Check Dashboard → Deposit and Dashboard → API Keys. Plan-covered calls draw plan quota (https://router.one/llm-api-key-budgets).

### Why did my HTTP 400, 429, or 500 error not say "unexpected status"?

Per `codex-api/src/api_bridge.rs` (rust-v0.172.0, checked 2026-11-30), Responses API calls check known error codes first (`model_not_found`, `context_length_exceeded` and others have their own messages). Otherwise a 400 prints the response body and is not retried; a 429 becomes a usage-limit, quota, spend-limit or `exceeded retry limit, last status: 429` line (https://router.one/codex-usage-limit, https://router.one/codex-exceeded-retry-limit); a 500 prints `We’re currently experiencing high demand, which may cause temporary errors.`; and an overloaded 503 prints `Selected model is at capacity. Please try a different model.` (https://router.one/codex-model-at-capacity).

### What does "Unknown error" mean, and does Codex retry unexpected status?

Per `protocol/src/error.rs` (rust-v0.172.0, checked 2026-11-30), Codex prints `Unknown error` when the error body was empty or could not be read. It retries `UnexpectedStatus` up to `stream_max_retries` (default 5) as a whole request, except for runtime credential rejections and 403 workspace or access-program denials, and retries 5xx up to `request_max_retries` (default 4) at the HTTP layer. A 4xx configuration error therefore shows `Reconnecting... n/5` first and then prints the red line; retrying cannot fix it.

## See also

- Codex 401 and missing variable fix: https://router.one/codex-401-unauthorized
- Codex stream disconnected & Reconnecting 5/5 fix: https://router.one/codex-stream-disconnected
- Codex Responses API: https://router.one/codex-responses-api
- Codex exceeded retry limit (429): https://router.one/codex-exceeded-retry-limit
- Codex model at capacity: https://router.one/codex-model-at-capacity
- Codex usage limit: https://router.one/codex-usage-limit
- Codex context window: https://router.one/codex-context-window
- OpenAI model_not_found fix: https://router.one/openai-model-not-found
- Region error fix (403): https://router.one/unsupported-country-region-territory
- API key budgets & model allowlist: https://router.one/llm-api-key-budgets
- LLM API error codes: https://router.one/llm-api-error-codes
- Live model status: https://router.one/status
- Canonical page: https://router.one/codex-unexpected-status
- Models and per-model token rates: https://router.one/models (markdown: https://router.one/models.md)
- Pricing: https://router.one/pricing
- API docs (markdown): https://router.one/docs.md
- Company facts: https://router.one/facts/company.md
