Codex CLI Switching to API key does not work as long as ChatGPT Teams subscription login is active

Resolved 💬 9 comments Opened Aug 26, 2025 by wbdb Closed Sep 7, 2025
💡 Likely answer: A maintainer (aibrahim-oai, contributor) responded on this thread — see the highlighted reply below.

What version of Codex is running?

0.24

Which model were you using?

gpt-5

What platform is your computer?

Linux 5.15.167.4-microsoft-standard-WSL2 x86_64 x86_64

What steps can reproduce the bug?

Currently, it is difficult in Codex CLI to automatically switch to the API when you reach your limits. Ideally, OpenAI will integrate a fallback here that kicks in automatically so that you don't have to stop in the middle of your work.

  1. I start codex with: codex --config preferred_auth_method="apikey"
  2. I select option 2: "Provide your own API key"
  3. When I hit enter here, I am back at step 2

To use Codex with the OpenAI API, set OPENAI_API_KEY in your environment

Press Enter to return

The OpenAI key is set as ENV: OPENAI_API_KEY.

Error exists in the following versions: 0.23 and 0.24

What is the expected behavior?

Use of the API key.

What do you see instead?

Endless loop, you keep coming back to step 2

You are currently using ChatGPT, while your preferred method is API key.

  1. Continue using ChatGPT

Included in Plus, Pro, and Team plans

2. Specify your own API key

Pay only for actual usage

Additional information

To solve the problem, you have to log out of Codex every time and log back in later. That's just no fun with such low 5-hour limits.

View original on GitHub ↗

9 Comments

gh640 · 10 months ago

This also happens in my macOS environment.

What version of Codex is running?

0.24

What platform is your computer?

Darwin 24.6.0 arm64 arm

Screen recording:

https://github.com/user-attachments/assets/76e64a59-1fb2-41d0-acb1-1cbcce2b8abe

---

For people down the road, a temporary workaround that works is to move or remove auth.json in the Codex CLI config directory.

aibrahim-oai contributor · 10 months ago

Do you have OPENAI_API_KEY set in your account?

gh640 · 10 months ago

@aibrahim-oai

Do you have OPENAI_API_KEY set in your account?

Yes. I use my API key with OPENAI_API_KEY for other scripts and the environment variable itself has no problem.

Here's the steps to reproduce in my case:

  1. Install Codex CLI 0.24.0 or 0.25.0
  2. Set a valid environment variable OPENAI_API_KEY
  3. Run codex --config preferred_auth_method='chatgpt'
  • Login with ChatGPT Plus, Pro or Team plan account and close the session.
  1. Run codex --config preferred_auth_method='apikey'
wbdb · 10 months ago

@aibrahim-oai yes:

/mnt/c/wamp64/www/...$ echo -n "$OPENAI_API_KEY" | wc -c
51
alexprotonotarios · 10 months ago
@aibrahim-oai > Do you have OPENAI_API_KEY set in your account? Yes. I use my API key with OPENAI_API_KEY for other scripts and the environment variable itself has no problem. Here's the steps to reproduce in my case: 1. Install Codex CLI 0.24.0 or 0.25.0 2. Set a valid environment variable OPENAI_API_KEY 3. Run codex --config preferred_auth_method='chatgpt' * Login with ChatGPT Plus, Pro or Team plan account and close the session. 4. Run codex --config preferred_auth_method='apikey'

That actually did work for me, thanks!

mblmarlon98 · 10 months ago

Same issue here. On Mac.

I have a valid API Key. -> it is working in my website, and charged up with money

echo $OPENAI_API_KEY returns the right key -> it is set in the environment

The i try to change codex --config preferred_auth_method='apikey' -> my usage of the payed plan ran out so i want to switch to pay per use

and i get stuck in the same screen as the recording above

IsaacWilsonMorse · 10 months ago

What worked for me was manually setting my OPENAI_API_KEY in ~/.codex/auth.json. Once it was set there, the preferred_auth_method flag worked as expected.

paulbettner · 10 months ago

Had the same issue, deleting auth.json fixed it. Thank goodness for this thread!

wbdb · 10 months ago

Hello everyone, I just tested it again with Codex 0.30.0 and it now works :). I am therefore closing this issue. No need to set the API key outside of the ENV.

When you also want automatic fallback to API upvote here: https://github.com/openai/codex/issues/2992