Share subscription accounts
Serve several accounts of one provider as one LLMProvider, spending the quota that resets soonest first and failing over when an account runs out.
A subscription account (for example a Claude Max or ChatGPT plan) has usage windows: a five-hour
limit, a weekly limit, sometimes per-model limits. With several accounts of one provider,
@yolk-sdk/pool serves them as one LLMProvider
(ADR 0011):
- each request goes to the account whose longest usage window (usually the weekly one) resets soonest and still has quota, so quota that would reset unused is spent first (expiring-first);
- without fresh usage readings, a route stays on the account that served it last (sticky fill-first);
- when an account runs out of quota or its credentials are rejected before any output, it cools down and the same request moves to the next account.
Install
pnpm add @yolk-sdk/pool@canary @yolk-sdk/agent@canary effect@4.2.0Available from 0.1.0-canary.116. The package is runtime-neutral.
| Subpath | Purpose |
|---|---|
@yolk-sdk/pool | makePool, the pure ranking (rankPool, staleQuotaCooldowns), Schemas, and errors |
@yolk-sdk/pool/store | PoolStoreApi port, makeMemoryPoolStore, and the poolStoreContract semantics cases |
Build a pool
This server-side fragment assumes claudeWork and claudePersonal are LLMProvider services you
built, one per account with its credentials, and workToken is that account's fresh OAuth token:
import { Effect, Layer } from 'effect'
import { LLMProvider } from '@yolk-sdk/agent/loop'
import { fetchAnthropicClaudeSubscriptionUsage } from '@yolk-sdk/agent/providers/anthropic/usage'
import { makePool } from '@yolk-sdk/pool'
import { makeMemoryPoolStore } from '@yolk-sdk/pool/store'
const program = Effect.gen(function* () {
const pool = yield* makePool({
id: 'claude',
accounts: [
// LLMProvider services you build, one per account and its credentials.
{ id: 'claude-work', priority: 1, provider: claudeWork },
{ id: 'claude-personal', provider: claudePersonal }
],
// Serverless hosts implement PoolStoreApi on their database.
store: makeMemoryPoolStore()
})
// The loop's provider: innermost, under your retry or attachment wrappers.
const llm = Layer.succeed(LLMProvider, pool.provider)
// On your own schedule (cron, background job), never per request.
yield* pool.recordReading('claude-work', yield* fetchAnthropicClaudeSubscriptionUsage(workToken))
// For a settings page: pins, active cooldowns, readings.
const state = yield* pool.state
})Accounts must be interchangeable for the requests the pool serves (same provider, same models). The pool never sees tokens: each account's provider carries its own credentials.
makePool option | Meaning |
|---|---|
id | pool id; pins are keyed by pool id and route |
accounts[].id | unique within the store; cooldowns and readings are keyed by it |
accounts[].priority | breaks ranking ties and orders fill-first, higher first (default 0) |
accounts[].provider | the account's LLMProvider service |
store | a PoolStoreApi (makeMemoryPoolStore for one process) |
maxAgeMs | how old a reading may be and still rank accounts (default 30 minutes) |
reservePercent | quota left untouched on every window, from 0 to below 100 (default 0) |
Readings come from the usage helpers in
Read subscription allowance. A window with
models limits only those models, and a window without windowDurationMinutes (OpenCode Go's
month) can block a used-up account but never ranks. Readings older than maxAgeMs (default 30
minutes) are ignored.
Failover and retries
| Failure before the first event | Pool |
|---|---|
rate_limit with quotaResetsAtMs (Claude, Codex quota) | Cooldown until the reset, next account |
rate_limit with retryAfterMs of 30 minutes or more | Cooldown for that long, next account |
auth | 30-minute cooldown, next account |
| anything else, or any failure after an event | Unchanged to the caller; the loop retries it only when it is retryable and nothing was emitted |
| every account out on quota | rate_limit, retryable: false, earliest quota reset |
| every account out on rejected credentials only | auth (provider_error), retryable: false |
The loop's own retry calls the pool again, which picks again; the pool never retries transient
errors itself, so attempts never multiply. A request without a model id fails validation_error.
Store
PoolStoreApi holds pins (per pool and route), one cooldown per account, and the latest reading
per account. Implementations must be atomic per key (movePin is a compare-and-set).
putCooldown over an existing cooldown: one that ended before the other began gives way to it;
two that overlap merge into the later end and its kind (on a tie, the later start's; on a full tie,
quota), with the later startedAtMs of the two (the newest failure), so a reading taken before
that failure never clears it. A kept quota
cooldown records the end of an auth failure it absorbed as authUntilMs; readings do not clear it
before then (a reading cannot vouch for the rejected credential). clearCooldown removes only the
observed PoolQuotaCooldown (same startedAtMs and authUntilMs), never a PoolAuthCooldown.
Two requests failing on one account write concurrently, so merge in one atomic statement, not a
read followed by a write: for example an upsert that sets the greatest start, picks the end and
kind with a CASE on which ends later (then which started later, then quota), and sets
authUntilMs to the greatest auth end of the two (an auth cooldown's end, a quota cooldown's
authUntilMs) when the kept one is quota and that end is after the merged start, else null.
Run every case of poolStoreContract against a fresh store of your implementation in your tests.
Store failures never fail a request: the pool routes fill-first without state and logs a warning.
What you own
- Credentials: one refresher per OAuth grant (refresh tokens rotate). Separate logins of the same account are separate grants.
- Readings: poll each account's usage with the subscription usage fetchers on a schedule and call
recordReading. - Which accounts form a pool, labels, alerts, and UI.