Skip to content

stream error: exceeded retry limit, last status: 401 Unauthorized #2896

Closed

Description

@rahulkarajgikar

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

Activity

  1. added
    bug
    on Aug 29, 2025
  2. changed the title [-]codex CLI keeps saying unauthorized[/-] [+]stream error: exceeded retry limit, last status: 401 Unauthorized[/+] on Aug 29, 2025
  3. rahulkarajgikar commented on Aug 29, 2025

    @rahulkarajgikar
    Author

    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

  4. migueldeicaza commented on Aug 30, 2025

    @migueldeicaza

    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.

  5. acgxv commented on Sep 2, 2025

    @acgxv

    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.

  6. rahulkarajgikar commented on Sep 2, 2025

    @rahulkarajgikar
    Author

    I agree it shouldn't use .env and should use configured environment variable instead

  7. migueldeicaza commented on Sep 2, 2025

    @migueldeicaza

    At least /status should display "Hey, I am getting my config from here"

  8. diegochine commented on Sep 3, 2025

    @diegochine

    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.

  9. gpeal commented on Sep 3, 2025

    @gpeal
    Contributor

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

    Image
  10. diegochine commented on Sep 4, 2025

    @diegochine

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

  11. RazeBerry commented on Sep 4, 2025

    @RazeBerry

    Same problem here!! Very bizarre and I am using it with Pro subscription!!

  12. mattw-services commented on Sep 6, 2025

    @mattw-services

    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`

  13. passingby000 commented on Sep 7, 2025

    @passingby000

    same problem with codex-cli 0.30.0 when trying to use my subscription.
    i've tried cli, vscode and windsurf.

  14. hegdeguruprasad commented on Sep 7, 2025

    @hegdeguruprasad

    Solution for using API Key (Windows)

    Verify that your API key is working.
    setx OPENAI_API_KEY
    (dont add quotes to the API key)
    echo %OPENAI_API_KEY%
    codex
    Select:
    2. Continue using API key
    Pay for what you use

    You should not get that error anymore

  15. 10 remaining items

  16. frixaco commented on Oct 2, 2025

    @frixaco

    I had the same issue (WSL2/Arch):

    • no env variable set
    • selected second login option - API key
    • copy pasted the API key

    The issue disappeared after /logout and adding the API key to my env.

  17. luislobo commented on Oct 3, 2025

    @luislobo

    I had to delete ~/.codex/auth.json today for it to work

  18. ML-Marco commented on Oct 7, 2025

    @ML-Marco

    It doesn't work with the current version (0.45.0). I tried this:

    rm -rf ~/.codex/
    echo "API-KEY" | codex login --with-api-key
    codex
    

    Fails with above 401 error.

  19. PeterP22 commented on Oct 7, 2025

    @PeterP22

    "codex logout", then "codex" in terminal and now works.

    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.

  20. therzv commented on Oct 8, 2025

    @therzv

    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

  21. DavidLee528 commented on Oct 12, 2025

    @DavidLee528

    @openai/[email protected] - Issue solved by re-login:

    codex logout
    codex login
  22. vedantdere commented on Oct 12, 2025

    @vedantdere

    Updating VS Code fixed this issue

  23. chans97 commented on Oct 19, 2025

    @chans97

    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.

    That seems to be the most reliable solution.

  24. fallonou1999 commented on Oct 19, 2025

    @fallonou1999

    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

  25. VArtzy commented on Oct 21, 2025

    @VArtzy

    I face the same issue

    I use codex 0.47 (macOS or Linux), fix it with:

    • export OPENAI_API_KEY=<YOUR_OPENAI_API_KEY>
    • if you are logged in, codex logout to restart unauthorized session
    • then run codex to create session

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

  26. SSugar310 commented on Oct 22, 2025

    @SSugar310

    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.

  27. 2u35eeee commented on Oct 26, 2025

    @2u35eeee

    my version is OpenAI Codex (v0.50.0) and occurs exceeded retry limit, last status: 401 Unauthorized,

  28. 200105060414pys commented on Oct 30, 2025

    @200105060414pys

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

    Image

    Image
  29. binarybana commented on Nov 3, 2025

    @binarybana

    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.

  30. joker123-wpx commented on Jan 6, 2026

    @joker123-wpx

    “错误:超过重试限制,最后状态:401 未授权,请求编号:”我今天下午在Windows作系统上用了IntelliJ IDEA的codex cli。提问后,我收到了上面的错误提示。代码401是由认证无效引起的,所以我找到了一个名为“.codex”的文件夹,它位于“C:\Users\user.codex”路径下。文件夹里包含密钥和令牌的信息。因为我激活了GPT Plus服务,并通过登录我的GPT账号使用codex cli,所以我干脆把整个文件夹都删掉了。然后,我输入了命令“codex login”,重新配置了命令信息。如果你用codex cli逐键作,也许你应该修改“在auth.json”文件里的键。

    图片 图片

    牛逼兄弟,我看了半天,直接用你的成了

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions