Skip to content

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 #2478

Closed as duplicate

Description

@tompw89

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.

Activity

  1. added
    enhancement
    on Aug 20, 2025
  2. wbdb commented on Aug 23, 2025

    @wbdb

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

  3. breadtk commented on Sep 13, 2025

    @breadtk

    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.

  4. alexyangjie commented on Oct 7, 2025

    @alexyangjie

    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.

  5. paulpv commented on Oct 9, 2025

    @paulpv

    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.

  6. teamfighter commented on Oct 14, 2025

    @teamfighter

    +1 for that feature, would be really useful

  7. Occid3re commented on Oct 17, 2025

    @Occid3re

    +1 I would also love to see that feature!

  8. etraut-openai commented on Nov 14, 2025

    @etraut-openai
    Contributor

    Closing in favor of #2992, which has more upvotes

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

    enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions