INVALID_API_KEY after switching providers in Codex
Quick fix
Upgrade to v3.20.1 or later; third-party switching no longer writes to auth.json, preventing provider key/authentication mismatches.
Symptom
Section titled “Symptom”切换模型厂家,提示INVALID_API_KEYSwitching model providers shows INVALID_API_KEY.
Multiple reports describe that after switching providers in Codex, `~/.codex/config.toml` had switched to the target provider, but the `OPENAI_API_KEY` in `~/.codex/auth.json` was still the previous provider's key. Editing a provider could also refill values from `auth.json`, overwriting the key. Another report stated that `experimental_bearer_token` could overwrite the provider-stored `auth.OPENAI_API_KEY`, causing later switches to write back the wrong key.
For earlier reports that other providers could not be switched, the maintainer said the issue should have been resolved in v3.16.1. However, related reports continued in v3.16.5, v3.17.0, and later versions mentioned in comments. The maintainer later explained that after the config-only refactor in v3.20.1, switching third-party providers no longer writes to `auth.json`, and two-way writing between the two locations no longer exists. If it still reproduces after upgrading, open a new issue as requested in the original post and attach logs.
Upgrade CC Switch to v3.20.1 or later. The maintainer stated that switching third-party providers no longer writes to `auth.json` from this version onward.
If you use multiple official Codex ChatGPT accounts, use accounts managed by the authentication center and bind provider cards to accounts, as instructed by the maintainer. For existing managed accounts, click “re-login” once on the account row.
If the error persists after upgrading, open a new issue as requested by the maintainer and include logs plus redacted before-and-after switching differences.
Affected Versions
Section titled “Affected Versions”Source Issues
Section titled “Source Issues”This page is distilled from 5 real issues
- After switching a Codex provider, why is `config.toml` updated but the key in `auth.json` is still from the previous provider?
- Reports stated that switching correctly wrote only to `config.toml`, while `OPENAI_API_KEY` in `auth.json` was not updated, causing an endpoint/key mismatch. The maintainer stated that third-party switching no longer writes to `auth.json` from v3.20.1.
- Can login authorizations for multiple official Codex configurations overwrite each other?
- One report stated that adding an official configuration overwrote the previous configuration's authorization information. The maintainer explained that accounts managed by the authentication center should be used and bound to provider cards; from v3.20.1, multiple accounts in the same Team workspace can coexist.
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.