CoderPlanCoderPlan
HomePricingLotteryAbout
Docs/问题排查

codex unexpected status 401 unauthorized 排查教程

Codex CLI 返回 401 时,按 auth.json、config.toml、额度和模型权限的顺序排查。

Browse docs

Docs

  • 开始使用
    • 快速上手
    • API Key 接入配置Important
    • Claude Code 配置
    • Codex CLI 配置
    • Gemini CLI 配置
    • Cursor 配置
    • OpenClaw 配置
    • Hermes Agent 配置
    • Cline 配置
  • 问题排查
    • Anthropic 连接失败
    • Anthropic err_bad_request
    • Codex 401 unauthorized
  • 使用优化
    • RTK 上下文管理
    • Context7 提高准确性
    • 模型选择
    • 成本控制
    • AI 生图教程

Docs

  • 开始使用
    • 快速上手
    • API Key 接入配置Important
    • Claude Code 配置
    • Codex CLI 配置
    • Gemini CLI 配置
    • Cursor 配置
    • OpenClaw 配置
    • Hermes Agent 配置
    • Cline 配置
  • 问题排查
    • Anthropic 连接失败
    • Anthropic err_bad_request
    • Codex 401 unauthorized
  • 使用优化
    • RTK 上下文管理
    • Context7 提高准确性
    • 模型选择
    • 成本控制
    • AI 生图教程

This article is not available in English yet

This article is not available in English yet. You can view the Chinese source for now.

Codex 401 unauthorized

codex unexpected status 401 unauthorized 表示 Codex CLI 当前请求没有通过认证。按顺序检查:`~/.codex/auth.json` 是否为合法 JSON 且 OPENAI_API_KEY 是完整的 CoderPlan `sk-` 密钥;`~/.codex/config.toml` 语法是否正确、base_url 是否以 `/v1` 结尾;是否残留旧的 OPENAI_BASE_URL 环境变量;账户 API 使用额度与模型权限是否可用。

Setup steps

  1. 1

    检查 auth.json 里的密钥

    Codex CLI 的密钥从 `~/.codex/auth.json` 读取(config.toml 里 `requires_openai_auth = true` 时生效)。文件必须是合法 JSON,OPENAI_API_KEY 为完整的 CoderPlan `sk-` 密钥——少复制字符、多空格或旧密钥都会 401。

    {  "OPENAI_API_KEY": "sk-your-api-key"}
  2. 2

    检查 config.toml 的语法和 base_url

    TOML 语法错误会让整段配置静默失效,Codex 退回默认行为。确认 base_url 以 `/v1` 结尾且不带尾部斜杠,供应商表里保留 `wire_api = "responses"` 和 `requires_openai_auth = true`。

    base_url = "https://api.coderplan.ai/v1"wire_api = "responses"requires_openai_auth = true
  3. 3

    清理旧教程的环境变量残留

    新版 Codex 以 config.toml 为准,但如果之前按旧教程 export 过 OPENAI_BASE_URL / OPENAI_API_KEY,建议从 `~/.zshrc`、`~/.bashrc` 和项目 `.env` 里删掉,排除干扰后重开终端再试。

  4. 4

    确认 API 使用额度和模型权限

    如果认证字段正确,再检查 CoderPlan API 使用额度、API Key 状态和目标模型是否可用。

FAQ

Codex 401 一定是 API Key 错了吗?

API Key 是最常见原因,但 auth.json 不是合法 JSON、config.toml 语法错误、API 使用额度不可用、模型权限不匹配,也可能表现为 401。

为什么我已经 export 了新 key,Codex 还是 401?

新版 Codex CLI 不再从环境变量读取供应商和密钥,export 对它无效。密钥要写入 `~/.codex/auth.json`,路由要写入 `~/.codex/config.toml`;改完后重开终端再试。

Search and article contents

Search docs

Article contents

  • 检查 auth.json 里的密钥
  • 检查 config.toml 的语法和 base_url
  • 清理旧教程的环境变量残留
  • 确认 API 使用额度和模型权限
  • 常见问题

Related links

  • Codex CLI 接入教程配置 config.toml 与 auth.json 两个文件接入 CoderPlan。
  • 开始使用首次使用前,先准备账号、额度、API Key 和 endpoint。
  • 定价与模型费用查看模型价格、充值 API 使用额度、缓存复用和按量扣费说明。
  • 问题排查遇到认证失败、endpoint 错误或 401 时,按顺序检查配置。
CoderPlanCoderPlan

An AI coding service platform for developers, making common tools easier to connect and manage.

[email protected]

Product

  • Pricing
  • Sign up

Docs

  • Quickstart
  • API Key setup

Supported services

  • Claude Code
  • OpenAI Codex / GPT
  • Gemini

About

  • About
  • Contact us
  • Service Status

© 2026 CoderPlan. All rights reserved.

Privacy Policy|Terms of Service|Refund Policy
Checking...We only process service access and usage records; we do not store your code content or conversation bodies.