-
Notifications
You must be signed in to change notification settings - Fork 0
Implement automatic auth switching between ChatGPT and API key when usage limits are reached #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
@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>
There was a problem hiding this 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
LimitTrackercomponent to manage usage limit timestamps and determine retry eligibility - Extended
AuthManagerwith 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.
| unsafe { | ||
| env::set_var("OPENAI_API_KEY", "sk-test-key"); | ||
| } |
Copilot
AI
Aug 31, 2025
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
| unsafe { | ||
| env::remove_var("OPENAI_API_KEY"); | ||
| } |
Copilot
AI
Aug 31, 2025
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
| unsafe { | ||
| env::set_var("OPENAI_API_KEY", "sk-test-fallback-key"); | ||
| } |
Copilot
AI
Aug 31, 2025
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
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:
OPENAI_API_KEYis available in the environment, Codex automatically switches to API key mode and retries the request~/.codex/limitImplementation Details
New Components
~/.codex/limitfile to track when usage limits were hit and determine when to retry ChatGPT authforce_switch_to_api_key()andforce_switch_to_chatgpt()methods for programmatic auth mode switchingcodex.rsKey Features
OPENAI_API_KEYenvironment variable is availableExample Flow
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.