Skip to content

Conversation

Copy link

Copilot AI commented Aug 31, 2025

This PR implements automatic switching between ChatGPT and API key authentication when usage limits are encountered, providing seamless fallback and recovery.

Problem

Previously, when ChatGPT usage limits were reached, users would see an error and need to manually switch to API key authentication. There was no automatic fallback mechanism or time-based retry logic to switch back to ChatGPT auth once limits reset.

Solution

Added automatic auth switching with the following behavior:

  1. Automatic Fallback: When a ChatGPT usage limit is reached and OPENAI_API_KEY is available in the environment, Codex automatically switches to API key mode and retries the request
  2. Limit Tracking: Records the timestamp when usage limits are hit in ~/.codex/limit
  3. Automatic Recovery: After 5 hours have passed since the limit was recorded, Codex automatically attempts to switch back to ChatGPT authentication

Implementation Details

New Components

  • LimitTracker: Manages the ~/.codex/limit file to track when usage limits were hit and determine when to retry ChatGPT auth
  • AuthManager Extensions: Added force_switch_to_api_key() and force_switch_to_chatgpt() methods for programmatic auth mode switching
  • Session Integration: Integrated auth switching logic into the turn execution flow in codex.rs

Key Features

  • Transparent Operation: Auth switching happens automatically with informative log messages
  • Robust Fallback: Only switches to API key if OPENAI_API_KEY environment variable is available
  • Time-based Recovery: Uses 5-hour cooldown period before attempting to switch back to ChatGPT
  • State Persistence: Limit timestamps persist across Codex sessions
  • Error Handling: Gracefully handles cases where credentials are unavailable

Example Flow

User starts with ChatGPT auth → Usage limit reached → Auto-switch to API key → Continue working
                                     ↓
5 hours later → Auto-switch back to ChatGPT → Clear limit record → Resume ChatGPT usage

The implementation is fully backward compatible and doesn't change existing behavior when no usage limits are encountered.

Fixes #1.


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copy link
Author

Copilot AI commented Aug 31, 2025

@iainlowe 👋 This repository doesn't have Copilot instructions. With Copilot instructions, I can understand the repository better, work faster and produce higher quality PRs.

I can generate a .github/copilot-instructions.md file for you automatically. Click here to open a pre-filled issue and assign it to me. I'll write the instructions, and then tag you for review.

Co-authored-by: iainlowe <3978+iainlowe@users.noreply.github.com>
Copilot AI changed the title [WIP] auto-switch to using api credentials when chatgpt usage is capped Implement automatic auth switching between ChatGPT and API key when usage limits are reached Aug 31, 2025
Copilot AI requested a review from iainlowe August 31, 2025 23:41
@iainlowe iainlowe marked this pull request as ready for review August 31, 2025 23:41
Copilot AI review requested due to automatic review settings August 31, 2025 23:41
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR implements automatic authentication switching between ChatGPT and API key modes when usage limits are encountered, providing seamless fallback and recovery without manual intervention.

Key changes:

  • Added LimitTracker component to manage usage limit timestamps and determine retry eligibility
  • Extended AuthManager with force switching methods for programmatic auth mode changes
  • Integrated automatic auth switching logic into the turn execution flow with 5-hour cooldown period

Reviewed Changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
codex-rs/core/src/limit_tracker.rs New component to track usage limit timestamps and manage 5-hour retry logic
codex-rs/login/src/auth_manager.rs Added force switching methods for programmatic auth mode changes
codex-rs/core/src/codex.rs Integrated auth switching logic into turn execution with fallback/recovery
codex-rs/login/src/lib.rs Added comprehensive tests for auth switching functionality
codex-rs/core/tests/auth_switching_integration.rs End-to-end integration tests for the complete auth switching workflow
codex-rs/core/src/lib.rs Added limit_tracker module export

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Comment on lines +425 to +427
unsafe {
env::set_var("OPENAI_API_KEY", "sk-test-key");
}
Copy link

Copilot AI Aug 31, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using unsafe to set environment variables in tests is unnecessary and potentially dangerous. Use std::env::set_var directly as it's safe, or consider using test-scoped environment variable management.

Copilot uses AI. Check for mistakes.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot check to see if this is still an issue; if it is, fix it

Comment on lines +442 to +444
unsafe {
env::remove_var("OPENAI_API_KEY");
}
Copy link

Copilot AI Aug 31, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using unsafe to remove environment variables in tests is unnecessary. Use std::env::remove_var directly as it's safe.

Copilot uses AI. Check for mistakes.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot check to see if this is still an issue; if it is, fix it

Comment on lines +12 to +14
unsafe {
env::set_var("OPENAI_API_KEY", "sk-test-fallback-key");
}
Copy link

Copilot AI Aug 31, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using unsafe to set environment variables in tests is unnecessary. Use std::env::set_var directly as it's safe.

Copilot uses AI. Check for mistakes.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot check to see if this is still an issue; if it is, fix it

iainlowe and others added 2 commits August 31, 2025 19:45
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@iainlowe iainlowe merged commit 1e39443 into main Aug 31, 2025
@iainlowe iainlowe deleted the copilot/fix-1 branch August 31, 2025 23:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

auto-switch to using api credentials when chatgpt usage is capped

2 participants