You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Codex: stream error when OPENAI_API_KEY and/or OPENAI_BASE_URL are set (ChatGPT signin) #3367
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.
changed the title [-]Codex using OPENAI_API_KEY with ChatGPT signin[/-][+]Codex: stream error when OPENAI_API_KEY and/or OPENAI_BASE_URL are set (ChatGPT signin)[/+]on Sep 12, 2025
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.
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.
Activity
aibrahim-oai commentedon Sep 10, 2025
did you set
preferred_auth_methodto 'apikey'?diegochine commentedon Sep 10, 2025
@aibrahim-oai no.
~/.codex/config.tomlcontains only a couple of projects with trust level, no other config.[-]Codex using OPENAI_API_KEY with ChatGPT signin[/-][+]Codex: stream error when OPENAI_API_KEY and/or OPENAI_BASE_URL are set (ChatGPT signin)[/+]euforic commentedon Sep 16, 2025
Same issue here with a fresh container but v0.34.0 works fine with the api key.
ggerve commentedon Sep 16, 2025
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- commentedon Sep 16, 2025
Related: #3002
Solution that worked for me: #1243 (comment)
laiso commentedon Sep 24, 2025
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 commentedon Nov 9, 2025
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-keyor going through the TUI-based login menu.