stream error: exceeded retry limit, last status: 401 Unauthorized
What version of Codex is running?
0.27.0
Which model were you using?
gpt-5-medium (default)
What platform is your computer?
Darwin 24.1.0 arm64 arm
What steps can reproduce the bug?
log in with API key
run any command
What is the expected behavior?
should work properly and respond back
What do you see instead?
this error
⚠ stream error: exceeded retry limit, last status: 401 Unauthorized; retrying 1/5 in 211ms…
⚠ stream error: exceeded retry limit, last status: 401 Unauthorized; retrying 2/5 in 410ms…
⚠ stream error: exceeded retry limit, last status: 401 Unauthorized; retrying 3/5 in 864ms…
⚠ stream error: exceeded retry limit, last status: 401 Unauthorized; retrying 4/5 in 1.755s…
⚠ stream error: exceeded retry limit, last status: 401 Unauthorized; retrying 5/5 in 3.087s…
i have confirmed the API key works fine
Additional information
No response
found the bug:
it was because .env in my repository was configured to an invalid key
it was unclear because there were no logs and no clear way to debug this, echo $OPENAI_API_KEY was showing the correct key
once i fixed the key in my .env, it worked fine
I had Claude diagnose the problem, it turns out that while /status showed that I was logged in, the credentials had expired, so you need to log-in again.
This should be fixed! I logged in with my ChatGPT account, but the .env file was used for application/project development. It shouldn’t automatically use it, as it consumes my application/project token.
At the very least, it should notify me that I’m using the API key instead of my ChatGPT account. Alternatively, it could simply give me a choice at the start-up.
I agree it shouldn't use .env and should use configured environment variable instead
At least /status should display "Hey, I am getting my config from here"
Same issue here, connected Codex via ChatGPT account but getting 401.
I had no .env in my workspace, but I was still exporting OPENAI_API_KEY in ~/.zprofile. Removing the export solved the issue.
In this case, it definitely shouldn't use the OPENAI_API_KEY, given I signed in using the ChatGPT account.
@diegochine What version of the CLI are you using? The latest version of the CLI is 0.29. Codex will ask you if you want chatgpt/api key login if you have an api key set on first launch.
@gpeal I'm on 0.29. I did get that question when I first launched codex and chose ChatGPT sign in. Still It just didn't work until I manually unset the OPENAI_API_KEY.
For context, I just set it again and codex is working fine now, so it probably just messed up during setup somehow.
Same problem here!! Very bizarre and I am using it with Pro subscription!!
Just come across this myself, using OPENAI_API_KEY in my .env for the application, and getting the issue with codex trying to use this, even when I have the Pro subscription.
`⚠️ stream error: exceeded retry limit, last status: 401 Unauthorized; retrying 1/5 in 201ms…
⚠️ stream error: exceeded retry limit, last status: 401 Unauthorized; retrying 2/5 in 372ms…
⚠️ stream error: exceeded retry limit, last status: 401 Unauthorized; retrying 3/5 in 859ms…
⚠️ stream error: exceeded retry limit, last status: 401 Unauthorized; retrying 4/5 in 1.561s…
⚠️ stream error: exceeded retry limit, last status: 401 Unauthorized; retrying 5/5 in 3.112s…
🖐 exceeded retry limit, last status: 401 Unauthorized
/status 📂 Workspace • Path: ~/Coding/linux-audit • Approval Mode: on-request • Sandbox: workspace-write • AGENTS files: (none)
👤 Account • Signed in with ChatGPT`
same problem with codex-cli 0.30.0 when trying to use my subscription. i've tried cli, vscode and windsurf.
Solution for using API Key (Windows)
Verify that your API key is working.
setx OPENAI_API_KEY
You should not get that error anymore
@gpeal update: codex started throwing the same error once again. Again, the fix was to remove OPENAI_API_KEY from .zprofile, even though I'm signed in via ChatGPT. I'm on latest version (0.31).
"codex logout", then "codex" in terminal and now works.
"codex logout", then "codex" in terminal and now works.
This works at least with the last upgrade
None of those things worked for me. I unset the api key, logged out, logged in, nothing worked. Still 401 error
The issue here is not that you can sign out or have the API key not set - but that Open AI are not picking up that the user is not signed in or the key is not set and giving you an appropriate message instead of a connection message which is useless and confusing! Sort it our Open AI - the is UX 101 ...
I am facing a similar issue. I have the correct api key, logged out logged in. codex is so frustrating.
did they vibe code this?
I had the same problem. Logging out and back into Codex doesn't work. Deleting auth.json and login again definitely resolved the issue.
I had the same issue. In my case, the issue was resolved by executing the /logout command once and then /login via the browser. Another possible cause could simply be that the authentication session had expired.