Case Study/Voice & Telephony/Design, engineering, maintenance
ElevenLabs Multi-Account Gateway
- Author
- Aryan
- Client
- Open source
- Published
- 2026
- Length
- 7 min · 1422 words
Abstract
Pools ElevenLabs accounts behind one endpoint with credit-aware routing and per-key metering.
Key outcomes
- Routing
- Credit-aware pool
- Failover
- Automatic
- Metering
- Per-key characters

The unit of access the upstream sells is a login
Text to speech bills by the character, and every ElevenLabs account is a closed pool: one quota, one rate limit, one expiration, one point of failure. For a single developer that is just a bill. For a team or an agency reselling voice work it becomes an operational problem, because the only thing the upstream can hand out is a session. The credential is an account refresh token, and a refresh token is full access to the account: credits, voices, subscription, everything. There is no way to hand a client some synthesis without also handing them the account.
That asymmetry is the reason the gateway exists. It changes the unit of distribution from a login to a key the operator controls. Upstream still sees one account among many. Clients see one base URL and one bearer token, and neither side sees the other’s internals.
The consequence is that this is not a transparent proxy. It terminates auth, it chooses an account, it meters the result, and it can revoke a client’s access without touching the upstream at all.
One endpoint over a pool of accounts
The API surface is deliberately small. POST /api/tts/:voiceId/stream is the streaming synthesis path; the rest is voices, history, usage and administration. A caller sends text, gets audio back, and can read what the request cost.
Every request carries either x-api-key for synthesis or x-admin-key for pool and key management. The account choice defaults to auto and can be pinned per request with x-account: <account-id> to inspect a single account without changing the pool. The response reports the decision rather than hiding it:
x-account-id: which account handled it
x-account-label: friendly name
x-character-cost: characters charged
x-credit-remaining: what's left after this call
Those headers matter more than they look. When a client reports that audio came back wrong or a request failed, the first question is which account served it, and the answer is already in the response the client kept.
Routing is a sort
The routing rule is four steps and no queue. Gather every active account, read each one’s remaining characters, discard the accounts that cannot afford the request, sort what is left by most credits first and oldest usage first, and send the job to the winner.
Most-credits-first is the opposite of round robin, and the reason is that unused credits are the thing being wasted. Round robin spreads load evenly and leaves several accounts partially spent at the end of a billing period. Front-loading the fullest account burns it down toward parity and keeps the emptier accounts as reserve capacity for the failover path. Oldest-first is the tiebreaker that makes equal-credit choices deterministic, so the same request does not bounce between two accounts on consecutive calls.
The trade-off is that affordability is an estimate. Character cost is derived from the request text before the upstream confirms it, and the credit figure each account reports is a snapshot that goes stale between refreshes. The router is therefore optimistic by design. It will occasionally pick an account that runs out mid-request, and the answer to that is failover rather than a more conservative filter that would leave usable credits untouched.
Failover, and what makes an account unhealthy
Failover is not a global retry loop. The routing pass produces an ordered list, and on an upstream error the next healthiest account in that list is tried, so a failed attempt cannot select the same account again. Transient conditions such as a rate limit or a 5xx move on to the next candidate. A rejected token is different, because retrying it elsewhere still leaves one broken account, and that account has to leave rotation.
Health is therefore a panel, not a boolean. It exposes token state, remaining credits and the last error for each account, so a session that expired in the last hour is visible before a client discovers it by receiving a failed synthesis. When every candidate has failed, the request returns an error rather than continuing to try. A gateway that hangs on a dead pool is worse than one that fails fast and says so.
Token expiry is the part that actually breaks
Accounts are authenticated by Firebase refresh tokens. A refresh token is durable but not directly usable upstream; it is exchanged for a short-lived access token, and that is what the synthesis calls carry. The exchange is the seam where a working gateway turns into a flaky one.
Exchanging on every request adds a round trip to the token endpoint in front of every synthesis and multiplies the load the gateway places on that endpoint. Caching the access token with no expiry awareness is worse in a subtler way. The cache serves a token that has already expired, and the failure surfaces as an upstream 401 that looks like an account problem rather than a caching problem. The implementation is a cache keyed by account with the token’s expiration attached, refreshed before use.
The failure mode this exposes is a burst. When an access token expires during a busy period, every in-flight request that was about to use that account wants a new token at once. Each one that refreshes independently is a redundant exchange, and depending on the upstream’s rate limiting that is a self-inflicted outage. The lesson is that refresh belongs to one path per account rather than to every request that happens to notice the token is stale.
Keys are the boundary the operator actually controls
Scoped keys are the reason this is infrastructure rather than a script. A key is minted as llk_…, stored only as a SHA-256 hash, and shown in full exactly once at creation. Comparison is timing-safe, so a wrong key cannot be narrowed down by measuring how long the rejection took. Keys can be paused or deleted, which revokes a client without touching the account pool, and each key accumulates its own spend.
Splitting x-admin-key from x-api-key keeps two audiences from sharing a credential. The admin key manages the pool, the keys and the reporting; the API key can only synthesize and read the endpoints a client needs. That distinction is not cosmetic, because the admin key is effectively the whole system and it should never be the thing embedded in a client’s integration.
Metering, audit and why they belong at the gateway
Upstream usage answers how many characters an account burned. It cannot answer which client owes what, because the upstream has no concept of the clients. The gateway does, because it is the only place that sees the request and the account at the same time, so metering is recorded there: characters used per key, per account, per day.
Alongside usage sits an audit log that records every request with who made it, what was requested, when, the status and the latency, plus a separate error log. The practical value is that a billing question and an incident become the same query. Voice and settings are cached upstream for ten minutes, which is a straight reduction in calls that do not need to happen, and the cache is small enough that a stale voice listing is a non-issue.
What I would do differently
State lives in plain JSON files under server/data/, gitignored so tokens never reach the repository. That is the right decision for a self-hosted tool with no database requirement, and it is also the weakest part of the system under concurrency. Two requests that finish at the same moment both read the usage file, both add their characters, and both write; one increment is gone. Under light use it never shows. Under a burst it silently undercounts, which is the worst possible failure for a metering layer.
The fix is not necessarily a database. Serializing writes through a single queue in the process would be enough for the load this runs at, and it preserves the no-database property. That is what I would reach for before adding a dependency.
The second change is earlier typing of the upstream error surface. Upstream failures were initially handled where they surfaced, which meant the difference between this account is spent and this request was malformed was encoded in several places by inspection. A single discriminated error type at the upstream boundary would have made the failover decision a match statement instead of a judgment call, and it is the kind of boundary that gets more expensive to introduce the longer the handlers live without it.
References
- [Source repository]github.com/Uo1428/11labs-api
- [Author]Aryan (Uo1428)
Built with