Codex silently switches to API key auth when environment variable is present, causing unintended billing
What version of the Codex App are you using (From “About Codex” dialog)?
Latest (Updated several times) 26.422.62136
What subscription do you have?
ChatGPT Pro
What platform is your computer?
Windows (also observed in Debian-based environments via OpenClaw)
---
What issue are you seeing?
When developing with the OpenAI SDK, I have an API key configured in my environment variables. The Codex app intermittently switches from my ChatGPT plan to API key authentication without clear indication. This results in unintended usage of my paid API credits.
This issue is not limited to Windows. It also occurs in OpenClaw instances running on Debian, where Codex CLI tools are installed and an API key is present in the system PATH/environment. In these environments, the same silent fallback to API key authentication is observed.
---
What steps can reproduce the bug?
- Set an
OPENAI_API_KEYin system environment variables (e.g. for SDK development) - Log into Codex using a ChatGPT account with an active plan
- Start using Codex normally
- At some point (e.g. restart, session change, CLI/tool invocation, or running within OpenClaw), Codex begins using the API key instead of the ChatGPT session
---
What is the expected behavior?
- Codex should default to ChatGPT session authentication when the user is logged in
OR
- Clearly prompt the user before switching authentication modes
OR
- Provide a persistent, highly visible indicator of which billing mode is active
---
What is the actual behavior?
- Codex silently switches to API key authentication
- No obvious warning or confirmation
- Leads to unintended API spend
---
Additional information
Impact
- High — causes unexpected financial cost
- Easy to miss during normal development flow
- Particularly problematic for developers who must have API keys set locally
- Amplified in OpenClaw / remote CLI environments, where auth context is less visible
---
Suggested Fixes
- Explicit auth mode toggle (ChatGPT vs API)
- Persistent UI indicator (e.g. “Using API key – billed per token”)
- Confirmation dialog before switching auth modes
- Option to disable API key auto-detection entirely
- Project-level or session-level auth locking
---
Environment
- Windows
- Debian (OpenClaw instances)
- OpenAI SDK in use (API key required in environment variables)
- Codex app
- Codex CLI tools installed (in Debian/OpenClaw environments)
---
Additional Context
This is especially problematic in workflows where developers are actively switching between SDK usage and Codex, as the auth mode can change without user awareness.
In OpenClaw-based setups running on Debian, where Codex CLI tooling is installed and API keys are necessarily present for automation, this behaviour becomes even more dangerous due to the lack of visibility into which auth mode is active at any given time.
3 Comments
Potential duplicates detected. Please review them and close your issue if it is a duplicate.
Powered by Codex Action
Codex does not pay any attention to the
OPENAI_API_KEYenv variable. It does supportCODEX_API_KEYwhen it's invoked ascodex exec(which is what the SDK uses). Are you perhaps defining aCODEX_API_KEYconfig key?The only other theory I have for the behavior you're seeing is that your
auth.jsonfile contains both an API key and a ChatGPT auth token. Early versions of Codex allowed both of these to be written to auth.json. Many months ago, we changed the logic to support only one or the other. I wonder if yourauth.jsonfile still has both. Open~/.codex/auth.jsonin an editor to validate this theory. If you see both, then delete the OPENAI_API_KEY entry. Or you can use the cli tocodex logoutand then runcodexto log in again. This will rewrite theauth.jsonfile.potentially related: no-active-thread-019f4c2c-a00a-7643-aca3-a92a5f0f05c4