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

Resolved 💬 7 comments Opened Sep 9, 2025 by diegochine Closed Nov 9, 2025
💡 Likely answer: A maintainer (aibrahim-oai, contributor) responded on this thread — see the highlighted reply below.

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.

View original on GitHub ↗

7 Comments

aibrahim-oai contributor · 10 months ago

did you set preferred_auth_method to 'apikey'?

diegochine · 10 months ago

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

euforic · 10 months ago

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

ggerve · 10 months ago

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...

rr- · 10 months ago
laiso · 9 months ago

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.

etraut-openai contributor · 8 months ago

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.