Not able to connect to codex, says "Reconnecting"

Resolved 💬 8 comments Opened Jan 3, 2026 by sulavtimsina Closed Jan 6, 2026
💡 Likely answer: A maintainer (etraut-openai, contributor) responded on this thread — see the highlighted reply below.

What version of Codex is running?

codex-cli 0.77.0

What subscription do you have?

Plus

Which model were you using?

gpt-5.1-codex-max medium But issue is with any model

What platform is your computer?

Darwin 25.2.0 arm64 arm

What issue are you seeing?

This is the first time I am trying to use Codex on my laptop. When I use the Codex command and then start a session, my first prompt doesn't complete. It says "unexpected status 401 unauthorized". I logged out and logged in again using ChatGPT+ subscription (not the API key) but looks like there is some authentication error. It can't even complete my first prompt request.

What steps can reproduce the bug?

Uploaded thread: 019b8488-1e9f-7101-8812-afc239c99ac5

What is the expected behavior?

It's expected for Codex to give me a response.

Additional information

_No response_

View original on GitHub ↗

8 Comments

etraut-openai contributor · 6 months ago

Thanks for uploading the log data. It seems to indicate that Codex is trying to log in using ChatGPT but receiving an "invalid" API key error from the server backend. That hints at a bug somewhere in the Codex CLI auth code.

You mentioned that this is the first time you're using Codex on this machine. Did you previously log in using an API key? I wonder if Codex has cached both your old (invalid) API key and your newer ChatGPT token. Open the file ~/.codex/auth.json and let me know if you see both an API key and token information in that file. There should be only one or the other. If you see both, that might help explain the problem you're seeing.

You can likely work around the problem by deleting your auth.json file and logging in again.

sulavtimsina · 6 months ago

I deleted the auth.json , re-logged in with Chat GPT (not API key), but I still get unexpected status 401 Unauthorized:.
The auth.json has both OPEN_API_KEY(which is null) and also tokens.

I have OPEN_API_KEY set in my ~/.zshrc for other purpose, not to use with codex. Since I specified to login with Chat GPT, it should not try to fetch that OPEN_API_KEY from the environment.

etraut-openai contributor · 6 months ago

It's fine (expected) that auth.json will contain a null OPENAI_API_KEY if you've logged in with ChatGPT.

And yes, the CLI will not fetch the OPENAI_API_KEY environment variable. In general, it pays no attention to this key. Just to confirm that isn't the problem, could you unset that env variable within a new terminal window and launch codex? I don't think that will fix the problem, but I just want to verify.

Do you have any non-default settings in your config.toml file? For example, have you defined a custom model provider?

Did you recently upgrade from a free ChatGPT account to Plus?

If you have access to another computer, are you able to log in and use Codex on that machine? I'm trying to determine if this is specific to your account or to your machine or local configuration.

sulavtimsina · 6 months ago

This is my config.toml:

```model = "gpt-5.2-codex"
model_reasoning_effort = "medium"
[projects."/Users/---/Documents/---/---/---/repo1"]
trust_level = "trusted"

[notice.model_migrations]
"gpt-5.1-codex-max" = "gpt-5.2-codex"

I have been having Plus for almost more than a year.

I opened a new terminal and unset OPENAI_API_KEY using `unset $OPENAI_API_KEY`
then ran codex. The reply to "hi", is "unexpected status 401 Unauthorized:"

I don't have access to another laptop now, but if above info can't provide you enough hint, i will try to wake up my another battery-dead mac.
sulavtimsina · 6 months ago

I ran some command to uninstall Codex but I think it did not uninstall correctly or something happened and now when I run Codex this is what I get.

<img width="1142" height="397" alt="Image" src="https://github.com/user-attachments/assets/71fbf143-91bb-4704-95d3-3e048fd8c409" />

Does this provide any hint?

sayan-oai contributor · 6 months ago

Hey @sulavtimsina, from the thread you uploaded it looks you had set (intentionally or not) the base_url for the OpenAI model provider being used to https://api.portkey.ai/v1. I suspect this was causing the 401s, as you signed in with ChatGPT but were hitting the Portkey responses endpoint instead of the OpenAI one.

The OPENAI_BASE_URL env variable is used for the base_url, so can you confirm whether you might have set OPENAI_BASE_URL to that Portkey url?

And to clarify, what did you do to get to the state shown in the screenshot? It seems from that screenshot that Codex is "working", but it seems to be a very old version (v0.1.*, when latest is v0.77.*). What do you get when you run codex --version? Is it possible you have an old alias/installation hanging around after your uninstall?

sulavtimsina · 6 months ago

@sayan-oai I had set that intentionally. I am running some terminal bench tasks, https://www.tbench.ai/ and I have a portkey api key for that.
I assumed that if I sign in my ChatGPT(and not API Key), the api keys on zshrc would be ignored.

But now I removed(commented) that api key from ~/.zshrc, and login with ChatGPT is working fine. Thank you.
My issue is resolved, but I am confused as to when I am using ChatGPT to sign why is the API key being checked on my zshrc file.

sayan-oai contributor · 6 months ago

When you sign in with ChatGPT, the OPENAI_API_KEY value _is_ ignored. You can confirm what auth method you are using (API key or ChatGPT) by running codex login status.

I don't think commenting out an API key would fix the problem if you are trying to sign in with ChatGPT while still pointing OPENAI_BASE_URL to Portkey. If it's working now, I suspect that either a) you are now correctly authing against the Portkey endpoint with an API key or b) OPENAI_BASE_URL is getting unset and "sign in with ChatGPT" is working as intended.

I think that the initial issue is resolved so I'm closing this, but happy to continue debugging with you in the comments if needed.