Skip to main content

Run Claude CLI subprocesses with automatic multi-account rotation and rate limit handling

Project description

claude-rotator

Run Claude CLI (claude -p) subprocesses with automatic multi-account rotation and rate limit handling. Zero dependencies.

Install

pip install claude-rotator

Requires claude CLI installed and authenticated on at least one account.

Quick Start

from claude_rotator import ClaudeRunner

runner = ClaudeRunner(
    accounts=[
        None,                      # default (~/.claude/)
        "/home/user/.claude_alt",  # fallback account
    ],
)

result = runner.run(prompt="Explain this code", model="sonnet")
print(result.output)
print(f"Cost: ${result.cost_usd:.4f}")
print(f"Duration: {result.duration_seconds:.1f}s")

When the first account hits a rate limit, claude-rotator detects it, caches the cooldown, and retries with the next account.

Async

result = await runner.run_async(prompt="Summarize", model="opus")

API

ClaudeRunner

ClaudeRunner(accounts=[None])
  • accounts: List of HOME directory paths. None means the default HOME. Each directory should contain a .claude/ folder with separate credentials.

runner.run() / runner.run_async()

runner.run(
    prompt="...",
    model="sonnet",           # "sonnet", "opus", or full model ID
    tools="Read,Write",       # allowed tools (None to omit)
    cwd=Path("."),            # working directory
    timeout=600,              # seconds
)

Returns a ClaudeResult:

  • output: str - the response text
  • cost_usd: float - total cost
  • duration_seconds: float - wall clock time
  • model: str - model used

ClaudeError

Raised on subprocess failure, timeout, or when all accounts are rate-limited.

  • stderr: str - error output
  • returncode: int - exit code (-1 for timeout)

How Account Rotation Works

  1. claude-rotator tries the first account in the list.
  2. If the output contains rate limit phrases ("usage limit", "rate limit", "out of extra usage"), it caches the cooldown time.
  3. It moves to the next account and retries.
  4. Cached accounts are skipped on subsequent calls until their cooldown expires.
  5. If all accounts are exhausted, it raises ClaudeError.

How Accounts Work

Each account is a directory containing a .claude/ folder with its own OAuth credentials. Set up multiple accounts by logging in with different HOME directories:

# Account 1 (default)
claude login

# Account 2
HOME=/home/user/.claude_alt claude login

Then pass the directories to ClaudeRunner:

runner = ClaudeRunner(accounts=[None, "/home/user/.claude_alt"])

License

MIT

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

claude_rotator-1.0.1.tar.gz (34.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

claude_rotator-1.0.1-py3-none-any.whl (7.6 kB view details)

Uploaded Python 3

File details

Details for the file claude_rotator-1.0.1.tar.gz.

File metadata

  • Download URL: claude_rotator-1.0.1.tar.gz
  • Upload date:
  • Size: 34.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for claude_rotator-1.0.1.tar.gz
Algorithm Hash digest
SHA256 b9b267e63bddfb1579211d4976976bbd2068e6c52399eff56e50a8ed4e1ea256
MD5 d73591299f7a121c877c2235435c2328
BLAKE2b-256 ef6f403abee9d56f934657c4fc5bc486ec65d56b08647d76bbb2fd52cc82f6c5

See more details on using hashes here.

File details

Details for the file claude_rotator-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: claude_rotator-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 7.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for claude_rotator-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 1b0ab3397b8aea0304b7b6fdafc4c33340b62c0c705537e3bba54812954c27c2
MD5 7f1ecaeead2a93f4bdea56349c35538e
BLAKE2b-256 a23d865c2656847a293eac142c9f8328d848cdf4654be5bbc81cf68e681c5db8

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page