Running Several Aggregator Keys in Round-Robin From the Client: Where Sticky Sessions and Balance Checks Break
Explore the practical pitfalls of using multiple aggregator API keys in a client-side round-robin. Learn how to design your key pool, handle sticky sessions, and implement robust balance checks without breaking workflows.
Why Round-Robin Multiple Keys?
When you use an LLM API aggregator like this one, you might hold multiple keys—perhaps from different contributors or top-ups. Client-side round-robin (cycling through keys for each request) can help distribute load, isolate failures, and avoid single-key rate limits. However, naive round-robin often breaks in subtle ways, especially around session state and balance tracking.
The Sticky Session Problem
Many LLM interactions are not stateless: a conversation or workflow may require multiple requests to the same model instance, and some providers use session affinity (sticky sessions) to maintain context. If you round-robin across keys mid-conversation, you risk:
- Losing conversation state (if the key maps to a different backend session).
- Inconsistent model behavior or errors due to context mismatch.
- Cache misses that increase cost and latency.
Mitigation Strategies
- Pin conversations to a key: Use a consistent hashing scheme based on conversation ID or user ID, so all requests for a session go to the same key. This preserves stickiness while still distributing different conversations across keys.
- Check for explicit session tokens: Some APIs return a session token; store it and use the same key for subsequent calls.
- Design for statelessness where possible: Prefer API calls that include full context in each request (e.g., system prompts and message history), so key switching doesn’t break the session. But be aware that some providers still rely on server-side state.
Balance Checks Break Under Round-Robin
Each key has its own balance, and with aggregators, balance is tied to the key’s credit (topped up via USDC). Round-robin can cause:
- Rate limit or insufficient balance errors when a key runs dry mid-cycle.
- Skewed usage if some keys are used more due to failures or retries.
- Difficulty in tracking spend per key, especially when requests fail and are retried on other keys.
Building a Robust Balance-Aware Pool
Instead of simple round-robin, implement a dynamic pool:
- Fetch balances periodically (or on key addition) via the aggregator’s balance endpoint.
- Exclude keys with low balance from the rotation, or assign weights based on remaining balance.
- Handle errors gracefully: On an insufficient balance error, mark the key as depleted, retry the request with the next key, and trigger a notification to top up.
- Track usage per key by logging which key served each request, and reconcile with the aggregator’s usage reports.
Remember: Users pay official price × 1.3, and contributors are credited official price × 1.1 (or × 1.2 for premium models) in USDC. So each key’s balance reflects real credit, and mismanagement can lead to unexpected costs or blocked access.
Implementation Patterns
Weighted Round-Robin with Health Checks
- Maintain a list of keys with metadata: balance, last used timestamp, health status.
- Before each request, select a key using weighted random or least-recently-used, but only from healthy, sufficiently funded keys.
- After each request, update the key’s usage stats and check for errors that indicate balance or rate limit issues.
Circuit Breaker per Key
- If a key fails repeatedly (e.g., due to rate limiting or balance exhaustion), open a circuit breaker for a cooldown period.
- This prevents cascading failures and gives time for manual intervention (like topping up).
Centralized Balance Monitor
- Run a background process that periodically checks balances for all keys and updates a shared state (e.g., Redis).
- The request handler consults this state to decide key eligibility.
Testing and Observability
- Simulate key failures in staging to verify fallback behavior.
- Log key selection decisions with reasons (e.g., "selected key2: balance sufficient, not rate-limited").
- Monitor per-key error rates and latency to detect misbehaving keys early.
When to Avoid Client-Side Round-Robin
If your workload is highly session-dependent or you need strict cost control per key, consider:
- Using a single key and relying on the aggregator’s built-in rate limits and balance management.
- Server-side key management with a proxy that handles pooling centrally, reducing client complexity.
- Separate keys per project or environment rather than pooling for a single application.
Round-robin can be powerful, but only with careful handling of sessions and balances. By designing for stickiness and dynamic balance awareness, you can avoid the common pitfalls and build a resilient multi-key setup.
Key Takeaways
- Sticky sessions require pinning or full-context requests; simple round-robin breaks them.
- Balance checks must be dynamic and integrated into key selection, not an afterthought.
- Implement health checks, circuit breakers, and observability to maintain reliability.
- Consider whether client-side round-robin is necessary for your use case; sometimes a single key or a proxy is simpler and safer.