|
| 1 | +# API Key Rotation |
| 2 | + |
| 3 | +AgentOS provides automatic round-robin API key rotation for every provider. Set any API key environment variable to a comma-separated list and rotation happens transparently. |
| 4 | + |
| 5 | +## Usage |
| 6 | + |
| 7 | +```bash |
| 8 | +# Single key (backward compatible): |
| 9 | +ELEVENLABS_API_KEY=sk_primary |
| 10 | + |
| 11 | +# Multiple keys with automatic rotation: |
| 12 | +ELEVENLABS_API_KEY=sk_primary,sk_backup,sk_overflow |
| 13 | + |
| 14 | +# Works for any provider: |
| 15 | +OPENAI_API_KEY=sk-key1,sk-key2 |
| 16 | +ANTHROPIC_API_KEY=sk-ant-key1,sk-ant-key2 |
| 17 | +``` |
| 18 | + |
| 19 | +## How It Works |
| 20 | + |
| 21 | +**Weighted round-robin:** The first key is selected ~2x more often than subsequent keys. In a 3-key pool, selection frequency is roughly 50% / 25% / 25%. |
| 22 | + |
| 23 | +**Quota detection:** When an API call returns a rate-limit or quota error, the key is automatically marked as exhausted and removed from rotation for 15 minutes. The next available key is tried immediately. |
| 24 | + |
| 25 | +Detected error signals: |
| 26 | +- HTTP 429 (rate limited) |
| 27 | +- HTTP 402 (payment required) |
| 28 | +- HTTP 456 (DeepL quota) |
| 29 | +- `quota_exceeded` (ElevenLabs) |
| 30 | +- `insufficient_quota` (OpenAI) |
| 31 | +- `overloaded_error` (Anthropic) |
| 32 | +- `RESOURCE_EXHAUSTED` (Google/Gemini) |
| 33 | + |
| 34 | +**Shared pools:** Providers that share an environment variable share a single key pool. If ElevenLabs TTS exhausts a key, ElevenLabs STT and SFX also skip it. |
| 35 | + |
| 36 | +| Env Var | Shared By | |
| 37 | +|---|---| |
| 38 | +| `OPENAI_API_KEY` | LLM, TTS, STT, Image, Realtime | |
| 39 | +| `ELEVENLABS_API_KEY` | TTS (batch + streaming), STT, SFX | |
| 40 | +| `FAL_API_KEY` | Image, Video, Audio | |
| 41 | +| `REPLICATE_API_TOKEN` | Image, Video, Audio | |
| 42 | +| `STABILITY_API_KEY` | Image, Audio | |
| 43 | +| `OPENROUTER_API_KEY` | LLM, Image | |
| 44 | + |
| 45 | +## Supported Providers |
| 46 | + |
| 47 | +Every AgentOS provider that accepts an API key supports rotation: |
| 48 | + |
| 49 | +- **LLM:** OpenAI, Anthropic, OpenRouter, Gemini, Groq, Mistral, Together, xAI |
| 50 | +- **Speech TTS:** ElevenLabs, OpenAI TTS, Azure Speech |
| 51 | +- **Speech STT:** OpenAI Whisper, Deepgram, AssemblyAI, ElevenLabs |
| 52 | +- **Voice Pipeline:** ElevenLabs Streaming TTS/STT, OpenAI Realtime, Deepgram Streaming |
| 53 | +- **Image:** OpenAI DALL-E, Stability, Flux, Fal, Replicate, OpenRouter |
| 54 | +- **Video:** Fal, Replicate, Runway |
| 55 | +- **Audio:** ElevenLabs SFX, Suno, Udio, Stable Audio, Fal, Replicate |
| 56 | +- **Web Search:** Serper, Tavily, Brave, Firecrawl |
| 57 | +- **NLP:** DeepL, OpenAI Translation |
| 58 | + |
| 59 | +## Implementation |
| 60 | + |
| 61 | +The key pool is implemented in `src/core/providers/`: |
| 62 | + |
| 63 | +- `ApiKeyPool.ts` — Weighted round-robin with exhaustion cooldown |
| 64 | +- `ApiKeyPoolRegistry.ts` — Singleton pools keyed by env var name |
| 65 | +- `quotaErrors.ts` — Cross-provider quota error detection |
0 commit comments