Bug: OPENAI_API_KEY env var silently overrides OAuth token, causing misleading 401 error
Bug Description
When OPENAI_API_KEY is set in the shell environment (e.g., in .zshrc) and you log in to Codex CLI via OAuth (e.g., a corporate/enterprise ChatGPT account), Codex CLI silently uses OPENAI_API_KEY from the environment instead of the OAuth token for API requests.
This causes a misleading 401 error that looks like a permissions/scope issue with your OAuth account, but the real cause is that the wrong credentials are being used entirely.
Steps to Reproduce
- Set
OPENAI_API_KEYin your shell environment (e.g., a personal/restricted API key in.zshrc) - Run
codex loginand complete OAuth login with a corporate/enterprise account - Send any message in Codex CLI
Expected Behavior
After a successful OAuth login, Codex CLI should use the OAuth token for API requests. The OPENAI_API_KEY environment variable should either be ignored when an OAuth session is active, or at minimum a warning should be shown when both are present.
Actual Behavior
Codex CLI uses OPENAI_API_KEY from the environment instead of the OAuth token, resulting in:
Unexpected status 401 Unauthorized: You have insufficient permissions for this operation.
Missing scopes: api.responses.write. Check that you have the correct role in your organization
(Reader, Writer, Owner) and project (Member, Owner), and if you're using a restricted API key,
that it has the necessary scopes.
url: https://api.openai.com/v1/responses
The error message mentions "restricted API key" and "missing scopes" — which correctly describes the env var API key being used — but gives no indication that OAuth credentials are being bypassed. This makes diagnosis extremely difficult.
Why This Is Confusing
The error leads users to believe their OAuth account lacks permissions. Typical debugging steps (logout → delete ~/.codex/auth.json → re-login → reinstall) all fail because the root cause is the environment variable, not the OAuth session.
Workaround
# Temporary fix
unset OPENAI_API_KEY && codex
# Permanent fix via alias
alias codex='unset OPENAI_API_KEY && command codex'
Environment
- Codex CLI version: 0.114.0
- OS: macOS
- Auth method: OAuth (ChatGPT Enterprise workspace via Okta/SAML)
6 Comments
Potential duplicates detected. Please review them and close your issue if it is a duplicate.
Powered by Codex Action
Codex doesn't use
OPENAI_API_KEYin the way you're describing here. The only place theOPENAI_API_KEYenv variable is used in the Codex CLI is in the login flow when you choose "API Key". The CLI uses theOPENAI_API_KEYenv variable to pre-populate the API Key value.Codex does support the
CODEX_API_KEYenv variable, but only in the non-interactivecodex execsubcommand.So this does not explain the 401 errors you're seeing.
The exact mechanism may be different from what I inferred, but
unset OPENAI_API_KEY && codexreliably fixes the issue for me.i'm having this same issue too and deleting the
OPENAI_API_KEYenv var fixed itcodex exec completely fails in the latest version with an api-key-only.. the websockets get 500 errors.
Adding another confirmed repro from macOS, with one extra clue that made this especially confusing.
What we observed:
~/.codex/config.tomlhad no OpenAI API key setting~/.codex/auth.jsonwas inchatgptauth mode~/.codex/auth.jsonhadOPENAI_API_KEY: nullOPENAI_API_KEYin the parent shell environment.Why this is worse than a normal auth mixup:
~/.codex/*) and makes the product feel like it has silently switched billing/auth mode.Expected behavior:
OPENAI_API_KEYshould not silently take precedence.Environment:
OPENAI_API_KEYpresent in inherited environment, but not saved in Codex config/auth filesSo this is not just "401 because wrong credentials". It also creates the false impression that Codex is persisting API-key auth on its own.