Codex 报「unexpected status …」怎么 解决
unexpected status <状态码>: <正文…> 是 Codex CLI 对没有专用提示的非 2xx HTTP 响应的通用输出(按 openai/codex 源码 rust-v0.172.0,2026-11-30 核对)。先看同一行里的 url:,确认请求发往哪里(OpenAI 官方 API、ChatGPT 后端还是自定义 base_url),再读冒号后的正文并对照下表排查。在标准 Responses API 请求里,HTTP 400、429、500 以及带 server_is_overloaded 的 503 有各自的专用提示,不会显示成 unexpected status。
先读懂 unexpected status 这一行
在 Codex 终端界面里,最终报错以红色显示、开头带 ■。按 protocol/src/error.rs(rust-v0.172.0,2026-11-30 核对),UnexpectedResponseError 按固定顺序拼出整行,后面的字段有值才会附上:
■ unexpected status <状态码>: <error.message 或正文>, url: <请求地址>, cf-ray: <ray>, request id: <id>, auth error: <头内容>, auth error code: <错误码>
- 冒号后的正文:JSON 里有非空
error.message时打印它;否则打印去空白后的原始正文,超 1,000 字节截断并加...;响应体为空时显示Unknown error。 url:是 Codex 请求的地址(base_url加/responses,WebSocket 握手失败时为wss://;WebSocket 封装 JSON 错误省略url:)。api.openai.com/v1/responses是 OpenAI 的 API,chatgpt.com/backend-api/codex/responses是 ChatGPT 登录,api.router.one/v1/responses是 Router One;其他地址来自自定义base_url或openai_base_url(它会把内置 provider 的凭据,包括 ChatGPT 登录令牌,发往该地址)。- 末尾可选字段:
cf-ray:取自cf-ray,request id:优先x-request-id(否则x-oai-request-id),auth error:取自x-openai-authorization-error,auth error code:解码自 base64x-error-json里的error.code。 - Cloudflare 403 特例:403 正文同时含
Cloudflare和blocked时,前缀改写为Access blocked by Cloudflare. This usually happens when connecting from a restricted region (status 403 Forbidden),同时保留url:与cf-ray:。
各状态码 常见 含义与 第一步 排查
先对照 url: 认出返回方,再按状态码排查:
| 状态码 | 常见含义 | 第一步先查什么 | 对应专页 |
|---|---|---|---|
| 401 Unauthorized | url: 对应服务端拒绝了 Key,或请求未带有效凭据。 | 核对 url: 和 env_key(在 shell 或 ~/.codex/.env 设 ROUTER_ONE_API_KEY,后者覆盖 shell)。env_key 指向的变量没设或为空时,报的是 Missing environment variable。 | https://router.one/zh/codex-401-unauthorized |
| 402 Payment Required | 网关余额或预算不足:钱包可用余额不够(含在途预留)、累计 maxSpend 触顶或每天/每月周期预算用完。 | 读冒号后的正文,再到控制台 → 充值看余额、到 API 密钥看累计 maxSpend 和周期预算。X-Billing-Funds-Reason 是响应头(有时才带),Codex 不会打印,联系支持时附上正文和 request id:。套餐覆盖的请求扣套餐次数。 | https://router.one/zh/llm-api-error-codes 与 https://router.one/zh/llm-api-key-budgets |
| 403 Forbidden | 地区限制、Cloudflare 拦截、ChatGPT 工作区无权限,或 Router One 上这把 Key 设了模型白名单(model="auto" 会被拒绝,返回 403)。 | 看 cf-ray:、地区/工作区错误码,或在 Router One 检查该 Key 的模型白名单。 | https://router.one/zh/unsupported-country-region-territory 与 https://router.one/zh/llm-api-key-budgets |
| 404 Not Found | 路径错(漏/重复 /v1,或中转只支持 Chat Completions、无 POST /v1/responses),或模型 ID 不存在/已退役。 | 检查 url: 是否有 /v1/v1 或缺 /v1,确认 wire_api = "responses" 及服务端原生支持,再核对 /models 模型 ID。 | https://router.one/zh/codex-responses-api 与 https://router.one/zh/openai-model-not-found |
| 413 Payload Too Large | 通常来自中间反向代理、CDN 或第三方中转的请求体体积上限,常见于大图/截图或超长对话。 | url: 只说明请求发往哪里,拒收的可能是该服务端,也可能是它前面的代理。去掉大图、执行 /compact 或新开线程,或调高自建代理的体积上限。 | https://router.one/zh/codex-context-window |
| 502 / 503 / 504 | 上游、网关或本机代理暂时失败、不可用或超时。(带 server_is_overloaded 的 503 走单独满载提示。) | 先等 Reconnecting... n/5 自动重试,看 https://router.one/zh/status 或供应方状态页,并检查本机代理。 | https://router.one/zh/llm-api-error-codes 与 https://router.one/zh/codex-stream-disconnected |
- 关于重试(rust-v0.172.0,2026-11-30 核对):HTTP 层只重试
5xx和传输错误(request_max_retries默认 4,上限 100);采样循环对UnexpectedStatus默认重试stream_max_retries次(默认 5,上限 100),只有运行时认证拒绝与 403 工作区/访问计划拒绝不重试。因此 401/402/404/413 也会先闪Reconnecting...再报红。
哪些 状态码 不会 显示成 unexpected status
在 Responses API 的模型请求里(codex-api/src/api_bridge.rs,rust-v0.172.0,2026-11-30 核对),四个常见状态码有各自的专用文案:
| HTTP 状态码 | Codex 实际打印什么 | 是否重试 | 对应页面 |
|---|---|---|---|
| 400 Bad Request | 直接打印响应正文(或 context_length_exceeded、model_not_found、策略拦截、图片无效的专用提示),从不打印 unexpected status 400。 | 不重试 | 看正文;Router One 见 https://router.one/zh/codex-responses-api |
| 429 Too Many Requests | 以下之一,从不打印 unexpected status 429:You’ve hit your usage limit…(ChatGPT 套餐额度)、Quota exceeded. Check your plan and billing details.(API 配额)、消费上限提示、Flex capacity unavailable.、升级提示,或 exceeded retry limit, last status: 429 Too Many Requests(限流)。 | 用量/配额/上限不重试;其他 429 仅在带 Retry-After 时重试 | https://router.one/zh/codex-usage-limit 与 https://router.one/zh/codex-exceeded-retry-limit |
| 500 Internal Server Error | We’re currently experiencing high demand, which may cause temporary errors.,从不打印 unexpected status 500。 | 会(HTTP 与采样退避重试) | 稍后重试;看供应方状态页(Router One 为 https://router.one/zh/status ) |
| 带特定错误码的 503 | server_is_overloaded 显示 Selected model is at capacity. Please try a different model.;policy_capacity_unavailable 显示策略容量不可用;slow_down 显示 rate limit exceeded: …。普通 503 仍打印 unexpected status 503。 | HTTP 重试 503;采样循环对满载仅在服务端给重试时间时重试 | https://router.one/zh/codex-model-at-capacity |
- 404 细节:JSON 正文
error.code为model_not_found时,Codex 直接打印错误消息且不重试;路径 404、空响应体及 Router One 的RESOURCE_NOT_FOUND则显示unexpected status 404 Not Found。
Router One 上的 状态码 含义与 Codex 配置
当 url: 为 https://api.router.one/v1/responses 时,provider 配置应当如下。下面的列表是各状态码的含义:以 https://router.one/zh/llm-api-error-codes 为准,另加每把 Key 的周期预算(402)与模型白名单(403)。
# ~/.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" # 在启动 codex 的 shell 里(或写入 ~/.codex/.env): # export ROUTER_ONE_API_KEY="sk-your-router-one-key"
- 401:API Key 无效或缺失(已吊销、已过期或不存在的 Key 都返回 401,不是 403)。
- 402:可用钱包余额不足(含在途预留)、累计上限或周期预算已用完。每把 Key 的消费上限(maxSpend)限的是这把 Key 能从钱包扣的钱:累计扣费到上限后,这把 Key 上需要钱包付费的请求返回 HTTP 402。套餐额度覆盖的请求从账户共用的套餐额度里扣,不计入任何 Key 的上限。每把 Key 还可以设每天或每月的周期预算:限的是这把 Key 在当期能从钱包扣的钱,北京时间 0 点重置(每月预算在 1 日重置),与 maxSpend 累计上限同时生效,哪个先用完,这把 Key 上需要钱包付费的请求就返回 HTTP 402。套餐额度覆盖的请求不计入。
- 403:通常不是 Key 无效。每把 Key 还可以设模型白名单:只能调用列表里的模型,model="auto" 会被拒绝(HTTP 403),aws/、azure/ 等渠道版要单独添加。
- 404:路径漏写/重复
/v1,或模型 ID 不在/models目录里(RESOURCE_NOT_FOUND)。 - 502 / 503 / 504:上游响应失败(502)、线路暂不可用(503
PROVIDER_UNAVAILABLE,不能据此认定模型 ID 不存在)或等待上游超时(归一化的 504PROVIDER_UNAVAILABLE不能证明上游返回 HTTP 504)。先看 https://router.one/zh/status 。 - 控制台 → 日志记录网关转发给模型的请求,展示模型、Token、钱包扣费或套餐扣减次数、耗时和状态。在调用任何模型之前就被网关拦下的请求(Key 校验失败的 401、余额不足或 Key 达到消费上限的 402、Key 或账户限额触发的 429,以及模型 ID 或端点不对的 400 / 404)不会留下记录;在输出任何内容之前就失败的流式请求也可能没有记录。联系 support@router.one 时请附上
request id:(X-Request-ID)。
常见 问题
Codex 报错 unexpected status 404 Not Found 怎么 解决?
先看 url::404 多半是路径不对(漏了 /v1、重复 /v1/v1,或中转不支持 POST /v1/responses)或 model 不存在、已退役。在 ~/.codex/config.toml 里,把 model = "gpt-6.2-sol" 和 model_provider = "router" 写在第一个 [table] 之前,把 base_url = "https://api.router.one/v1"、env_key = "ROUTER_ONE_API_KEY"、wire_api = "responses" 写在 [model_providers.router] 里,不要打开 requires_openai_auth。项目级 .codex/config.toml 会忽略 model_provider 和 model_providers(Codex 文档,2026-11-30 核对)。见 https://router.one/zh/codex-responses-api 与 https://router.one/zh/openai-model-not-found 。
Codex 报错 502 Bad Gateway(比如 接入 DeepSeek 或开代理时)怎么办?
502 表示代理或网关没从后面的服务拿到有效响应。url: 是 Codex 请求的地址,不一定是作答的那一跳:openai/codex #56919 的用户报告里,502 来自 macOS 系统代理,url: 显示的却是本机 provider。先绕开本机代理测试,再确认该地址原生支持 /v1/responses;url: 是 Router One 时先看 https://router.one/zh/status 。
Codex 报错 403 Forbidden 是什么 原因?
403 是权限或策略拦截,通常不是 Key 错。在 OpenAI / ChatGPT 上常见于地区受限(https://router.one/zh/unsupported-country-region-territory )、Cloudflare 拦截页或工作区无权限。在 Router One 上,无效 Key 返回 401;如果这把 Key 设了模型白名单,先查白名单:只能调用列表里的模型,model="auto" 会被拒绝(HTTP 403,见 https://router.one/zh/llm-api-key-budgets )。
Codex 报错 413 Payload Too Large 怎么 解决?
HTTP 413 是请求体字节数超限,多数由中间反向代理、CDN 或第三方中转触发(token 超限通常报 400 context_length_exceeded)。去掉大图/截图、运行 /compact 或新开线程(https://router.one/zh/codex-context-window ),自建代理则调高请求体上限。
Codex 报错 402 Payment Required 怎么 处理?
402 来自网关计费拦截。在 Router One 上看冒号后的正文:可能是钱包可用余额不够(在途请求会先预留一部分),或这把 Key 的累计 maxSpend、每天或每月的周期预算已用完(每天的预算北京时间 0 点重置,每月的在 1 日重置)。到控制台 → 充值或 API 密钥调整;套餐覆盖的请求扣套餐次数(https://router.one/zh/llm-api-key-budgets )。
为什么 Codex 报错 400、429、500 时没有写 unexpected status?
按 codex-api/src/api_bridge.rs(rust-v0.172.0,2026-11-30 核对),Responses API 调用会先看已知错误码(model_not_found、context_length_exceeded 等有各自的提示)。其余的 400 直接打印正文且不重试;429 显示为用量上限、配额、消费上限或 exceeded retry limit, last status: 429(https://router.one/zh/codex-usage-limit 与 https://router.one/zh/codex-exceeded-retry-limit );500 固定打印 We’re currently experiencing high demand, which may cause temporary errors.;带 server_is_overloaded 的 503 打印 Selected model is at capacity. Please try a different model.(https://router.one/zh/codex-model-at-capacity )。
报错行里写着 Unknown error 是什么 意思?Codex 会自动 重试吗?
按 protocol/src/error.rs(rust-v0.172.0,2026-11-30 核对),响应体为空或没读到时,Codex 打印 Unknown error。除运行时凭据被拒和 403 工作区 / 访问计划被拒之外,UnexpectedStatus 会整条请求重试最多 stream_max_retries 次(默认 5),5xx 还会先在 HTTP 层重试最多 request_max_retries 次(默认 4)。配置或余额类的 4xx 会先显示 Reconnecting... n/5 再报红,重试不会变好,要先修原因。