codex icon indicating copy to clipboard operation
codex copied to clipboard

Codex: stream error when OPENAI_API_KEY and/or OPENAI_BASE_URL are set (ChatGPT signin)

Open diegochine opened this issue 1 year ago • 7 comments

What version of Codex is running?

codex-cli 0.31.0

Which model were you using?

gpt-5

What platform is your computer?

Darwin 24.6.0 arm64 arm

What steps can reproduce the bug?

  • Login to Codex via ChatGPT
  • Have the OPENAI_API_KEY environment variable set (e.g. in .zprofile)
  • Launch Codex and send any query

What is the expected behavior?

The query should go through normally.

What do you see instead?

Any query to Codex fails with error: ⚠️ stream error: exceeded retry limit, last status: 401 Unauthorized; retrying

Additional information

Very similar and likely related to #2896 .

Sample outputs:

  • When API key is not set:
Last login: Tue Sep  9 14:09:30 on ttys022
➜  ~ if [ -n "$OPENAI_API_KEY" ]; then
echo "OpenAI API key is set"
fi

➜  ~ codex

>_ You are using OpenAI Codex in ~

 To get started, describe a task or try one of these commands:

 /init - create an AGENTS.md file with instructions for Codex
 /status - show current session configuration and token usage
 /approvals - choose what Codex can do without approval
 /model - choose what model and reasoning effort to use

▌test

> Hey! I’m here and ready. What would you like to work on?
Token usage: total=239 input=89 (+ 6144 cached) output=150 (reasoning 128)
  • When API key is set:
Last login: Tue Sep  9 14:10:27 on ttys022
➜  ~ if [ -n "$OPENAI_API_KEY" ]; then
echo "OpenAI API key is set"
fi

OpenAI API key is set
➜  ~ codex

>_ You are using OpenAI Codex in ~

 To get started, describe a task or try one of these commands:

 /init - create an AGENTS.md file with instructions for Codex
 /status - show current session configuration and token usage
 /approvals - choose what Codex can do without approval
 /model - choose what model and reasoning effort to use

▌test

⚠️ stream error: exceeded retry limit, last status: 401 Unauthorized; retrying
1/5 in 212ms…

⚠️ stream error: exceeded retry limit, last status: 401 Unauthorized; retrying
2/5 in 407ms…

⚠️ stream error: exceeded retry limit, last status: 401 Unauthorized; retrying
3/5 in 858ms…

EDIT: I noticed that the OPENAI_BASE_URL environment variable also causes the very same error.

diegochine avatar Sep 09 '25 12:09 diegochine

did you set preferred_auth_method to 'apikey'?

aibrahim-oai avatar Sep 10 '25 01:09 aibrahim-oai

@aibrahim-oai no. ~/.codex/config.toml contains only a couple of projects with trust level, no other config.

diegochine avatar Sep 10 '25 09:09 diegochine

Same issue here with a fresh container but v0.34.0 works fine with the api key.

euforic avatar Sep 16 '25 03:09 euforic

Have same issue with v0.36.0 Env variables OPENAI_API_KEY is set, but codex is not picking up this variable.

v0.34.0 worked fine

Only way to use API Key now it's to use command codex login --api-key...

ggerve avatar Sep 16 '25 15:09 ggerve

Related: #3002 Solution that worked for me: https://github.com/openai/codex/issues/1243#issuecomment-3290984360

rr- avatar Sep 16 '25 20:09 rr-

I was using preferred_auth_method in my GitHub Actions workflow to run Codex on a fresh instance without any manual setup.

Now that preferred_auth_method has been removed, I have to run codex login --api-key beforehand to avoid authentication errors. The API key itself is valid, but without this explicit login step I consistently get a 401 Unauthorized error. This makes the behavior feel odd, since previously the environment variable alone was enough.

laiso avatar Sep 24 '25 12:09 laiso

The login mechanism has been changed since this bug was submitted. The CLI no longer supports implicit login using an OPENAI_API_KEY environment variable. You now need to explicitly log in using codex login --api-key or going through the TUI-based login menu.

etraut-openai avatar Nov 09 '25 18:11 etraut-openai