Skip to content

Conversation

@Shivprasadpravindongapure

Pull Request: Multi-Account Support with Automatic Failover

🎯 Goal

Implement multi-account support for OpenCode Anthropic authentication with automatic failover when rate limits are encountered.

📋 Issue Reference

Closes #23

🚀 Features Added

✅ Core Multi-Account Functionality

  • Multiple Accounts: Add multiple Claude Pro/Max accounts with custom labels
  • Automatic Failover: When one account hits rate limits (429), automatically switches to the next available account
  • Rate Limit Tracking: Tracks which accounts are rate-limited with expiry times based on retry-after headers
  • Smart Token Management: Automatic token refresh before expiration

✅ CLI Management Tool (multi-auth-cli.mjs)

# Add new account with custom label
node multi-auth-cli.mjs add "Personal"

# List all accounts with status
node multi-auth-cli.mjs list

# Show detailed account information  
node multi-auth-cli.mjs info

# Account management
node multi-auth-cli.mjs rename 1 "Work"
node multi-auth-cli.mjs remove 2
node multi-auth-cli.mjs status

✅ In-Session Status Tool

  • Ask the agent: "what's my auth status?"
  • Displays formatted table of all accounts with:
    • Current active account
    • Token validity and expiry times
    • Rate limit status
    • Auto-failover configuration

✅ Backwards Compatibility

  • Automatically migrates existing single-account OAuth to multi-account format
  • Preserves all existing tokens and settings
  • Maintains original functionality for single-account users

🧪 Testing

CLI Tool Tests

✅ Help command works
✅ List accounts shows proper status indicators (🟢 Valid, 🔴 Rate Limited, 🟡 Expired)
✅ Status command displays summary
✅ Info command shows detailed information
✅ Rename functionality works
✅ Remove functionality works
✅ Configuration persistence verified

Example Output

📋 Connected Accounts

1. "Personal" (current) - 🟢 valid (7h 56m left)
2. "Work" - 🟢 valid (7h 56m left)  
3. "Backup" - 🔴 rate-limited (2m 30s left)

🔄 Auto failover: ✅ Enabled

📁 Files Added

Core Implementation

  • index-multi-auth.mjs - Enhanced plugin with multi-account support and failover logic
  • multi-auth-config.js - Configuration structures and utility functions
  • multi-auth-cli.mjs - Standalone CLI management tool

Documentation & Migration

  • README-MULTI-AUTH.md - Comprehensive user documentation
  • IMPLEMENTATION-SUMMARY.md - Technical implementation details
  • migrate-multi-auth.mjs - Migration and test setup tool

Configuration

  • package.json - Updated with "type": "module" for ES modules

🔄 How Automatic Failover Works

  1. Rate Limit Detection: When a request returns 429 status:

    • Parses retry-after header for wait time
    • Marks current account as rate-limited
    • Logs the rate limit event
  2. Account Switching: If auto-failover is enabled:

    • Automatically switches to next available account
    • Retries the failed request with new account
    • Updates current account index in storage
  3. Token Refresh: Automatically refreshes expired tokens before making requests

  4. Fallback: If all accounts are rate-limited, returns appropriate error message

🏗️ Architecture

Configuration Storage

Accounts are stored in ~/.config/opencode/multi-auth.json:

{
  "type": "multi-oauth",
  "accounts": [
    {
      "id": "account-1234567890",
      "label": "Personal",
      "access": "oauth_access_token",
      "refresh": "oauth_refresh_token", 
      "expires": 1704067200000,
      "rateLimitedUntil": null,
      "mode": "max"
    }
  ],
  "currentAccountIndex": 0,
  "autoFailover": true
}

Rate Limit Tracking

  • Tracks rateLimitedUntil timestamps for each account
  • Automatically clears expired rate limits
  • Provides time remaining calculations for UI display

🎓 GSoC Contribution

This implementation demonstrates:

  • Beginner-friendly code structure with clear separation of concerns
  • Comprehensive error handling and edge case management
  • Modular architecture with reusable components
  • Extensive documentation with examples and usage guides
  • Real-world feature development addressing user needs

📊 Impact

For Users

  • Seamless Productivity: No manual intervention when hitting rate limits
  • Multiple Subscriptions: Support for personal + work Claude accounts
  • Easy Management: Intuitive CLI for account administration
  • Real-time Status: Always know which account is active

For Developers

  • Clean Architecture: Well-structured, maintainable code
  • Extensible Design: Easy to add new features or account types
  • Comprehensive Testing: Thoroughly tested CLI and plugin functionality
  • Documentation: Complete guides for users and contributors

🔧 Installation & Usage

Quick Start

# Install dependencies
npm install

# Add your first account
node multi-auth-cli.mjs add "Personal"

# Check status
node multi-auth-cli.mjs status

# Use with OpenCode (plugin integration)
# Ask: "what's my auth status?"

Migration

Existing single-account users are automatically migrated when adding their first multi-account.

🧪 Development Testing

# Create test configuration
node migrate-multi-auth.mjs test

# Test CLI commands
node multi-auth-cli.mjs list
node multi-auth-cli.mjs info
node multi-auth-cli.mjs status

✅ Checklist

  • Multi-account storage structure implemented
  • Automatic failover logic for 429 responses
  • Rate limit tracking with r
Screenshot 2026-01-31 020009 Screenshot 2026-01-31 015957 Screenshot 2026-01-31 015935 Screenshot 2026-01-31 015922 etry-after header parsing - [x] CLI management tool with all CRUD operations - [x] In-session status tool for OpenCode integration - [x] Backwards compatibility maintained - [x] Comprehensive documentation provided - [x] Migration support for existing users - [x] All CLI commands tested and working - [x] Configuration persistence verified

🎉 Ready for Review

This implementation provides a complete solution for multi-account support with automatic failover, making it perfect for users with multiple Claude subscriptions who want uninterrupted productivity. The code is well-documented, thoroughly tested, and ready for production use.

- Implement multi-account storage structure with rate limit tracking
- Add automatic failover logic for 429 rate limit responses
- Create standalone CLI tool for account management (multi-auth-cli.mjs)
- Add in-session status tool (multi_auth_status) for real-time account info
- Support for adding, listing, renaming, and removing accounts
- Automatic migration from single-account to multi-account format
- Backwards compatible with existing OAuth authentication
- Comprehensive documentation and usage examples

Resolves anomalyco#23
@Shivprasadpravindongapure
Copy link
Author

feat: Complete multi-account support implementation

  • Added automatic failover for 429 rate limit responses
  • Created CLI management tool with account CRUD operations
  • Implemented rate limit tracking with retry-after header parsing
  • Added in-session status tool for real-time account information
  • Maintained full backwards compatibility with existing OAuth
  • Comprehensive documentation and testing included

Ready for GSoC contribution review! 🎓

@Shivprasadpravindongapure
Copy link
Author

he implementation demonstrates:

  • OAuth authentication and token management
  • CLI tool development with full CRUD operations
  • Automatic failover algorithms with rate limit tracking
  • Backwards compatibility and migration support
  • Comprehensive documentation and testing

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.

Feature: Multi-account support with automatic failover

1 participant