Automatically switch from Claude subscription to API billing when you hit usage limits
Project description
Claude Code Fallback
Automatically switch from your Claude subscription to API billing when you hit usage limits, keeping your coding session uninterrupted.
Why This Exists
Claude Code usage is shared with your web Claude.ai usage and resets every 5 hours. When you hit these limits during an active coding session, you're forced to either wait or manually switch to API billing. This tool automates that transition so you never lose momentum.
How It Works
The tool monitors your Claude Code session for usage limit errors and automatically:
- Detects when you've hit your subscription usage limits
- Prompts you to switch to API mode (or switches automatically if configured)
- Sets the
ANTHROPIC_API_KEYenvironment variable to enable API billing - Allows you to continue your session seamlessly
- Optionally switches back to subscription mode when limits reset
Features
- 🔄 Automatic Detection: Monitors for usage limit errors in real-time
- ⚡ Seamless Switching: Continues your session without losing context
- 💰 Cost Tracking: Logs API usage so you know what you're spending
- 🔔 Smart Notifications: Alerts you before switching or when approaching limits
- ⏰ Auto-Reset: Switches back to subscription mode when your limits reset
- 🛡️ Safe Mode: Optional prompt before spending API credits
Project Overview
This project is currently in early development. The planned architecture includes:
Source Files (src/claude_fallback/)
__init__.py- Package initialization and version infocli.py(planned) - Command-line interface using Typer for user interactionsmonitor.py(planned) - Background process monitor to detect Claude Code usage limit errorsconfig.py(planned) - Configuration management using Pydantic for settings validationapi_client.py(planned) - Anthropic API client wrapper for handling API-based requestsusage_tracker.py(planned) - Cost and usage logging functionalitynotifier.py(planned) - Notification system for alerts and status updatessession.py(planned) - Session state management and mode switching logic
Current Status
The project currently contains the basic package structure. Core functionality including monitoring, API switching, and usage tracking are planned for implementation. See the Roadmap section for upcoming features.
Installation
With mise + uv (Recommended)
This project uses mise for tool version management and uv for fast Python dependency management.
# Clone the repository
git clone https://github.com/yourusername/claude-code-api-fallback.git
cd claude-code-api-fallback
# Install mise (if not already installed)
# macOS/Linux:
curl https://mise.run | sh
# Install dependencies (mise will automatically set up Python 3.12 and create a venv with uv)
mise install
# Configure your settings
cp config.example.json config.json
vim config.json
Configuration
Edit config.json with your preferences:
{
"api_key": "sk-ant-api03-...",
"auto_switch": false,
"prompt_before_switch": true,
"cost_limit_per_session": 10.0,
"log_usage": true,
"notify_at_percentage": 80,
"auto_revert_on_reset": true
}
Configuration Options
api_key: Your Anthropic API key (get one from console.anthropic.com)auto_switch: Automatically switch without prompting (default: false)prompt_before_switch: Ask before switching to API mode (default: true)cost_limit_per_session: Maximum API spend per session in USDlog_usage: Track and log all API usage (default: true)notify_at_percentage: Alert when reaching X% of subscription limitsauto_revert_on_reset: Switch back to subscription when limits reset
Usage
As a Wrapper
# Run Claude Code through the fallback wrapper
python claude-fallback.py
As a Background Monitor
# Monitor an existing Claude Code session
python monitor.py --pid <claude-code-pid>
Manual Control
# Force switch to API mode
python claude-fallback.py --force-api
# Check current mode and usage
python claude-fallback.py --status
# Reset to subscription mode
python claude-fallback.py --use-subscription
Subscription Plans Reference
- Pro ($20/month): ~45 messages or 10-40 prompts every 5 hours
- Max 5x ($100/month): ~225 messages or 50-200 prompts every 5 hours
- Max 20x ($200/month): ~900 messages or 200-800 prompts every 5 hours
Usage varies based on codebase size, conversation length, and model choice (Opus uses ~5x more than Sonnet).
Cost Comparison
Example: Heavy coding session
- Subscription: $20/month Pro = ~40-80 hours of Sonnet 4
- API: ~$0.003/prompt input + ~$0.015/prompt output (varies by context)
For most users, subscription + occasional API usage is more cost-effective than pure API usage.
Requirements
- Python 3.8+ (Python 3.12 recommended with mise)
- Active Claude Pro or Max subscription
- Anthropic API key (for fallback)
- Claude Code installed
- macOS or Linux (uses PTY for process interaction - Windows support planned)
- Optional: mise for streamlined setup
Security Notes
- Store your API key securely (use environment variables or encrypted config)
- The tool only reads/writes the
ANTHROPIC_API_KEYenvironment variable - All usage logs are stored locally
- Your API key is never transmitted except to Anthropic's API
Roadmap
- Cost prediction based on codebase analysis
Contributing
Contributions welcome! This project addresses a real need expressed in Anthropic's GitHub Issue #2944.
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
Disclaimer
This is an unofficial tool not affiliated with Anthropic. Use at your own risk. Always monitor your API usage and costs. The tool respects Anthropic's terms of service by only switching between legitimate authentication methods.
License
APACHE 2.0 License - see LICENSE file for details
Acknowledgments
- Inspired by Issue #2944 on the claude-code repository
- Built for developers who need uninterrupted coding sessions
- Thanks to the Anthropic team for building Claude Code
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file claude_code_fallback-0.1.0.tar.gz.
File metadata
- Download URL: claude_code_fallback-0.1.0.tar.gz
- Upload date:
- Size: 99.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4da3160458295d68a20c8688c5858267cd98ced74fc8c518b135271596ad7f40
|
|
| MD5 |
d03b9ca22d706005fa243496eae87061
|
|
| BLAKE2b-256 |
c30bcf843a55df980ac5c0d59da882e71b6f1edce8c2b4e977f559bee643b1af
|
Provenance
The following attestation bundles were made for claude_code_fallback-0.1.0.tar.gz:
Publisher:
publish.yml on AveryClapp/Claude-Code-Fallback
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
claude_code_fallback-0.1.0.tar.gz -
Subject digest:
4da3160458295d68a20c8688c5858267cd98ced74fc8c518b135271596ad7f40 - Sigstore transparency entry: 660739737
- Sigstore integration time:
-
Permalink:
AveryClapp/Claude-Code-Fallback@f28c20381835340bf3c2b1d2369ccab5c58d5993 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/AveryClapp
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@f28c20381835340bf3c2b1d2369ccab5c58d5993 -
Trigger Event:
release
-
Statement type:
File details
Details for the file claude_code_fallback-0.1.0-py3-none-any.whl.
File metadata
- Download URL: claude_code_fallback-0.1.0-py3-none-any.whl
- Upload date:
- Size: 14.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
29758afd385d26aa34c2f29e7b1ddd2f0550d7c18ff39cbb662fb54a5ceba94c
|
|
| MD5 |
894daca33c467d8f8ea2495f54f67e01
|
|
| BLAKE2b-256 |
e03b2ae232c846ad69c1af83f2eee70516da04944d3f8b803441a1fb59829178
|
Provenance
The following attestation bundles were made for claude_code_fallback-0.1.0-py3-none-any.whl:
Publisher:
publish.yml on AveryClapp/Claude-Code-Fallback
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
claude_code_fallback-0.1.0-py3-none-any.whl -
Subject digest:
29758afd385d26aa34c2f29e7b1ddd2f0550d7c18ff39cbb662fb54a5ceba94c - Sigstore transparency entry: 660739738
- Sigstore integration time:
-
Permalink:
AveryClapp/Claude-Code-Fallback@f28c20381835340bf3c2b1d2369ccab5c58d5993 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/AveryClapp
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@f28c20381835340bf3c2b1d2369ccab5c58d5993 -
Trigger Event:
release
-
Statement type: