auth.json OPENAI_API_KEY not synced - Codex
Quick fix
Upgrade cc-switch to v3.20.1. Switching Codex providers no longer overwrites auth.json, fully resolving the key retention issue.
Symptom
Section titled “Symptom”auth.json OPENAI_API_KEY → 匹配 SuperAPI(上一个 provider)✗
config.toml base_url / experimental_bearer_token → 匹配 Vibe Coding ✓auth.json OPENAI_API_KEY → matches SuperAPI (previous provider) ✗ config.toml base_url / experimental_bearer_token → matches Vibe Coding ✓
Before cc-switch v3.20.1, a configuration write logic flaw existed when switching Codex providers. The core cause was that cc-switch failed to keep the active provider's `settings_config.auth` content (specifically `OPENAI_API_KEY` and `auth_mode`) synchronized with `~/.codex/config.toml` when writing the live configuration. Specifically, after switching providers, `config.toml` correctly updated `base_url` and `experimental_bearer_token`, but `~/.codex/auth.json` still retained the previous provider's key. This caused Codex to read an inconsistent configuration combination on startup, using incorrect authentication for actual requests.
Additionally, in proxy takeover mode, cc-switch only explicitly extracted the `OPENAI_API_KEY` field when writing `auth.json`, discarding other necessary fields like `auth_mode`, causing third-party API to malfunction. In the config-only refactor of v3.20.1, these issues were fixed by rewriting the storage location management: switching third-party providers no longer writes `auth.json` at all, keys follow the provider's own provider table, and `auth.json` returns to being a pure official login file.
Upgrade cc-switch to v3.20.1 or higher. This version performs a config-only refactor of Codex configuration storage, no longer overwriting auth.json when switching third-party providers.
~/.codex/auth.json // 修复前(被错误覆写){"OPENAI_API_KEY": "sk-旧供应商的key"}// 修复后(不再被覆写,保持官方登录状态){"OPENAI_API_KEY": "","auth_mode": "chatgpt","token": "..."}If the issue persists after upgrading, check if OPENAI_API_KEY is set in system environment variables. cc-switch detects system environment variable conflicts and may affect configuration application; remove this setting from system environment variables.
系统环境变量 // 删除或注释掉环境变量// OPENAI_API_KEY=sk-xxxxFor users using proxy takeover, if you manually edited auth.json, ensure it contains the auth_mode field. After upgrading to v3.20.1, this field is automatically populated with the provider configuration, requiring no manual maintenance.
Affected Versions
Section titled “Affected Versions”Source Issues
Section titled “Source Issues”This page is distilled from 10 real issues
- #92奇怪json格式:config.json 的 codex 部分
- #1248Codex OpenAI Official 无法参与故障转移,无法和第三方api丝滑切换
- #2296Codex proxy takeover/hot switch does not preserve Common Config in live ~/.codex/config.toml
- #2561codex无法切换其它供应商
- #3270提示:检测到系统环境变量冲突
- #3556Codex 供应商切换时 auth.OPENAI_API_KEY 被 experimental_bearer_token 覆盖
- #4941Codex 官方配置多账号切换时 ~/.codex/auth.json 授权信息被覆盖,导致账号切换失败
- #4944切换 Codex provider 时 auth.json 的 OPENAI_API_KEY 未同步更新
- #5218codex导入更新
- #5539编辑供应商时 apikey 从 auth.json 回填,而非该供应商自身记录,导致保存会覆盖成当前生效的 key
- Why does Codex report INVALID_API_KEY after switching providers?
- Because the old version of cc-switch only rewrote config.toml when switching providers, without updating OPENAI_API_KEY in ~/.codex/auth.json, causing Codex to read the previous provider's expired key. This issue is fixed after upgrading to v3.20.1.
- Why is auth.json overwritten to contain only a single OPENAI_API_KEY field?
- In proxy takeover mode, the old version of cc-switch only explicitly extracted the OPENAI_API_KEY field when serializing auth.json, discarding other fields like auth_mode. Starting from v3.20.1, switching third-party providers no longer writes auth.json at all, so this issue no longer occurs.
- Will previous configurations still work after upgrading to the latest version?
- Yes. The config-only refactor in v3.20.1 rewrote the storage location management. The database side backfill uniformly strips the bearer token from the config text and stores it in a single key slot. Existing managed accounts may need to click "Re-login" once on the account row; see the release notes for details.
- What to do when adding a custom provider shows an error and cannot be added?
- When adding, auth.json (JSON) must be set to {"OPENAI_API_KEY": "any"}. After adding, setting it to empty {} during modification is fine. This is because the old version performed a non-empty check on OPENAI_API_KEY.
- Why does cc-switch prompt a detected system environment variable conflict?
- This is triggered by having OPENAI_API_KEY set in system environment variables. It is recommended to remove this setting from system environment variables to avoid conflicts with cc-switch managed configurations.
Related problems
Section titled “Related problems”This is an unofficial community wiki with no affiliation to the cc-switch authors or the project itself. Its content is compiled from the project's public GitHub issues. This site distributes no software.