Multi-key failover
When you configure multiple API keys for a provider, the gateway automatically handles failover:- Keys are tried in the order they are listed
- If a key fails, the next key is used automatically
- Failover triggers include:
- Rate limit errors (HTTP 429)
- Quota exceeded responses from the provider
- Timeout or server errors (HTTP 5xx)
Advanced key selection
For fine-grained control over which API key is used, configureapi_key_selection with CEL expressions that select keys based on runtime metrics like quota usage and error rates.
Basic configuration
How strategies work
Strategies execute in order until one returns at least one key:- First strategy filters keys with >100 remaining requests
- If no keys match, falls back to all keys
- Selected keys are then tried in order for failover
Each strategy is a CEL expression that returns a list of keys. The first strategy that returns a non-empty list is used.
Quota-based selection
Prioritize keys with remaining capacity:Error-rate-based selection
Avoid keys experiencing issues:Load distribution
Randomize key selection to distribute load:Available key variables
| Variable | Description |
|---|---|
k.quota.remaining_requests | Requests remaining before rate limit |
k.quota.remaining_tokens | Tokens remaining before rate limit |
k.error_rate.total | Fraction of all errors (0.0 to 1.0) |
k.error_rate.rate_limit | Fraction of rate limit (429) errors |
k.error_rate.timeout | Fraction of timeout errors |
Next steps
- CEL Functions Reference: complete list of CEL functions for key selection
- Managing Provider Keys: storing, rotating, and securing your provider API keys
- Bring Your Own Keys: overview of using your own provider keys