stream error: exceeded retry limit, last status: 401 Unauthorized
Resolved 💬 38 comments Opened Aug 29, 2025 by rahulkarajgikar Closed Sep 3, 2025
💡 Likely answer: A maintainer (gpeal, contributor)
responded on this thread — see the highlighted reply below.
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_
38 Comments
found the bug:
it was because
.envin my repository was configured to an invalid keyit 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
.envfile 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
.envin my workspace, but I was still exportingOPENAI_API_KEYin~/.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.
<img width="1058" height="392" alt="Image" src="https://github.com/user-attachments/assets/b0074a33-c606-431b-80eb-f571980b8b66" />
@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 <Your-API-KEY>
(dont add quotes to the API key)
echo %OPENAI_API_KEY%
codex
Select:
Pay for what you use
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_KEYfrom.zprofile, even though I'm signed in via ChatGPT. I'm on latest version (0.31).If you still encounter this error when you are using the latest version of Codex, it's probably because your key in auth.json is expired. Just try re-login or update the key.
"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.jsonand login again definitely resolved the issue.I had the same issue. In my case, the issue was resolved by executing the
/logoutcommand once and then/loginvia the browser. Another possible cause could simply be that the authentication session had expired.Nothing that I tried in this issue worked for. I believe that my issue is because we have recently switched to using API keys in an OpenAI project with US data residency constraints. I could not get codex to work with that API key, but when I logged out and then logged in with an old API key without the data residency restriction, it worked.
If you haven't, try to login first:
I had the same issue (WSL2/Arch):
The issue disappeared after
/logoutand adding the API key to my env.I had to delete
~/.codex/auth.jsontoday for it to workIt doesn't work with the current version (0.45.0). I tried this:
Fails with above 401 error.
this worked for me as of 8th october 2025. When i typed /status it showed that i was logged in as normal but kept receiving 401 request error. I logged out in the cli then retyped codex. Once in it prompted me to use either my pro account or api key and now all cylinders are firing.
try all the things above and not work, the only way to solve this issue to me is:
put OPENAI_API_KEY on .bashrc
then login like:
$ printenv OPENAI_API_KEY | codex login --with-api-key
done, smooth
@openai/codex@0.36.0- Issue solved by re-login:Updating VS Code fixed this issue
That seems to be the most reliable solution.
still having this issue in 0.47.
and it seems to have something to do with projects path 'cause while some of my projects works normally, other reconnects all the time
I face the same issue
I use codex 0.47 (macOS or Linux), fix it with:
export OPENAI_API_KEY=<YOUR_OPENAI_API_KEY>``codex logout`` to restart unauthorized sessioncodex`` to create sessionnow if it success, you choose second login option using API key, it will fill API key automatically. And now after your codex should working and authorized.
---
I don't know why such openai/codex can have this silly bug look like my custom gemini-llm-cli. they probably vibe code it lol.
I experienced the same issue.
In my case, I was logged in to ChatGPT through OAuth using the VSCode extension, not an API key.
After selecting “Log out” from the extension and logging in again, it started working normally.
my version is OpenAI Codex (v0.50.0) and occurs exceeded retry limit, last status: 401 Unauthorized,
"Error: exceeded retry limit, last status: 401 Unauthorized, request id: ”
I used codex cli in IntelliJ IDEA on a Windows operating system this afternoon. After asking a question, I got the above error message. Code 401 is caused by Invalid Authentication, so I found the folder named ".codex", which is under the path of "C:\Users\user\.codex". And the folder contains the information of key and token. Because I activated the GPT Plus service, and used codex cli by logging into my GPT account, so I simply deleted the entire folder. Then, I entered the command "codex login" , and reconfigured the command information.
If you use codex cli by key, maybe you should modify the key of "auth.json" file.
<img width="807" height="241" alt="Image" src="https://github.com/user-attachments/assets/fbe06d65-bc15-4a0d-a8dc-29b6652669b5" />[](url)
<img width="372" height="109" alt="Image" src="https://github.com/user-attachments/assets/f285d9b9-6bab-46a9-907c-c84b1fa123cc" />
I found I had to generate a new API key to get codex to work. The length of the new one was significantly longer than my API key that was failing.
牛逼兄弟,我看了半天,直接用你的成了