When logged in with ChatGPT Plus/Pro while also supplying an API key, automatically fall back to using API credits once ChatGPT credits run out

Resolved 💬 7 comments Opened Aug 20, 2025 by tompw89 Closed Nov 14, 2025
💡 Likely answer: A maintainer (etraut-openai, contributor) responded on this thread — see the highlighted reply below.

What feature would you like to see?

When logged in with ChatGPT Plus/Pro while also supplying an API key, automatically fall back to using API credits once ChatGPT credits run out.

This along with greater visibility of used/remaining credits in one's ChatGPT plan would be killer.

Are you interested in implementing this feature?

Not unless I can vibe code it...

Additional information

Just for fun...

user
Would it be difficult to implement the auto-fallback from ChatGPT plan credits to API credits?

codex (gpt-5-high)
Short answer: Moderate effort, low risk. It’s localized and straightforward to add.

Why it’s feasible

  • Single choke point: Requests go through codex-rs/core/src/client.rs::ModelClient::stream_responses, which already parses 429 errors and classifies usage_limit_reached / usage_not_included.
  • Both creds available: auth.json can contain both ChatGPT tokens and OPENAI_API_KEY; env var OPENAI_API_KEY works too. We can read either without changing formats.
  • URL selection handled: Switching auth automatically flips base URL (ChatGPT backend vs OpenAI API) via ModelProviderInfo::get_full_url.

Proposed approach

  • Hook: In stream_responses, on 429 with error.type == "usage_limit_reached" or "usage_not_included" and current auth is ChatGPT:
  • Try to resolve an API key:
  • Env: std::env::var("OPENAI_API_KEY").
  • File: codex_login::try_read_auth_json(get_auth_file(&self.config.codex_home))?.OPENAI_API_KEY.
  • If found, switch a mutable local auth to Some(CodexAuth::from_api_key(key)), then retry the request once.
  • Details to adjust:
  • Recompute store per attempt: let store = prompt.store && auth.as_ref().map(|a| a.mode) != Some(AuthMode::ChatGPT).
  • Only attempt fallback once (guard with a boolean).
  • Keep current retry/backoff behavior for other errors.
  • Continue sending chatgpt-account-id header only when auth.mode == ChatGPT (already conditional).

Edge cases

  • No API key available: Return current error unchanged.
  • Invalid/expired API key: Normal retry/UnexpectedStatus paths apply; no infinite loops.
  • Custom OPENAI_BASE_URL: Respected; URL comes from provider config regardless of auth.
  • Responses vs Chat wire API: Built‑in OpenAI uses Responses; fallback lives in stream_responses. If needed later, mirror logic in chat_completions path.

Tests to add

  • Fallback success: First response returns 429 usage_limit_reached with ChatGPT headers; second succeeds with Bearer sk-test-key and no chatgpt-account-id.
  • No key present: Still surfaces usage-limit error.
  • One-shot: Ensure only one fallback attempt occurs (two total requests).
  • Store flag: Assert store=false on first attempt (ChatGPT), store=true on fallback (API key).

Estimated changes

  • Files: core/src/client.rs (+ ~40–80 LoC), core/tests/client.rs (+ 1–2 wiremock tests).
  • Docs: Note auto-fallback behavior in README and optionally config.md (and add an opt-out flag later if desired).
  • Effort: ~1/2 day including tests and docs.

If you’d like, I can implement this behind a default-on behavior (or add auto_fallback_to_apikey config) and ship tests.

View original on GitHub ↗

7 Comments

wbdb · 11 months ago

Yes, that would make sense. After only about 15 GPT-5-Thinking requests, I reached my limit of 5 hours in the team subscription :(

breadtk · 10 months ago

This feature would solve a real workflow problem for me.

I often get deep into a session and need a bit more capacity before I finish. Manually switching auth mid-session is currently unreliable and disruptive (see related reports #1985, #2733, #3409). An automatic, well-signalled fallback would let me continue work while making billing explicit.

Proposed UX:

  1. Automatic fallback during an active session from ChatGPT plan auth to an available OPENAI_API_KEY when the ChatGPT plan reports usage limits.
  2. A warning on new-session launch if the account is already over the ChatGPT quota, with a clear option to continue using the ChatGPT plan or opt into pay-as-you-go for that session.
  3. Periodic in-session reminders that the session is using API-key billing rather than the ChatGPT plan, plus a one-time summary of expected cost impact when the fallback occurs.

This keeps the flow intact while making billing explicit so users understand they are moving to pay-as-you-go for that session.

alexyangjie · 9 months ago

I'd really like this to be implemented as well. This can be set in a config if the concern is that people unknowingly use API credits when plan usage reached limits.

When configured, the codex should fallback gracefully to the API key once the usage limit achieved.

paulpv · 9 months ago

Gracefully, yes. But it should not do it automatically. It should prompt to user to confirm before switching to pay-per-use.

Or make it an option to auto switch or not.

I imagine it could be complicated to pause in mid response, change the auth, and then resume the response.

teamfighter · 9 months ago

+1 for that feature, would be really useful

Occid3re · 9 months ago

+1 I would also love to see that feature!

etraut-openai contributor · 8 months ago

Closing in favor of #2992, which has more upvotes