- Distributing traffic across multiple provider accounts to stay within per-key rate limits.
- A/B testing providers by routing a configurable percentage of traffic to each.
- Spreading traffic across multiple providers to reduce blast radius from a single provider outage.
- Maximizing throughput when one provider’s capacity is a bottleneck.
- Minimizing response time by routing to the fastest available model.
Quick Start
Distribute requests across multiple providers using weighted routing.Configuration
Weight Calculation:
- Weights are normalized:
[0.4, 0.8]→[33%, 67%]. - Higher weight = more traffic.
- Minimum weight:
0.001. - Default weight:
0.5.
load_balancer selects one model per request. It does not retry a failed call against another model in the pool; it only records the failure so future selections avoid it. The top-level model field is used as-is only if load_balancer fails to produce a selection (for example an empty models list). To fail over to a different model when a call errors, pair load_balancer with Fallbacks.Weight-based routing
Split traffic across models by percentage weights instead of latency or a fixed rotation.Patterns
Use cases
Latency-based routing
Route each request to the model with the lowest recently observed latency, instead of a fixed traffic split.- Each AI Gateway replica tracks its own in-memory latency history per
provider/modelas an exponentially weighted moving average (EWMA): a running average where each new sample blends in at 30% weight against 70% for the existing average, so recent calls shift the average faster than older ones without discarding history entirely. This history is not shared between replicas and is lost when a replica restarts. - With fresh data (samples from the last 5 minutes) for every configured model, the lowest-latency model is selected. A model with no data, or stale data, is probed again instead of being written off.
- Ten percent of selections explore the full pool by configured weight regardless of current latency, keeping measurements fresh. A failed call is penalized (the greater of twice its duration or 1,000 ms) so a fast failure doesn’t look like a fast success.
- Configured weights decide exploration, probing, and near-ties (latencies within 0.5 ms of each other). With fresh, non-tied data, the lowest latency always wins regardless of weight.
Full selection flow
Full selection flow
See also: Organization-level load balancing
To apply load balancing across your organization without changing request code, use Routing Rules to configure Fallback, Latency, Weighted, and Round Robin strategies at the workspace level.
Code examples
Monitoring
Track these metrics for optimal load balancing:- Traffic distribution: Actual vs expected percentages.
- Cost per model: Monitor spending across providers.
- Response times: Compare latency by model.
- Error rates: Track failures by provider.
latency_based, response time is already the selection signal. Manual weight tuning for performance is not needed; adjust weights only to influence exploration, cold-start probing, and near-tie behavior.
Troubleshooting
Uneven distribution- Check if weights are normalized correctly.
- Verify sufficient request volume (min 100 requests for accuracy).
- Monitor over longer time periods. Unexpected costs
- Track actual vs expected cost distribution.
- Monitor for expensive model overuse.
- Set up cost alerts per provider. Performance issues
- Check latency differences between models.
- Monitor for provider-specific slowdowns.
-
Adjust weights based on performance data.
All traffic going to one model with
latency_based - Expected once one model is consistently fastest. Ten percent of requests still explore the rest of the pool to keep their latency data fresh.
-
Confirm
load_balancer.typeis set to the intended strategy if an even split was expected instead.
- Expected. Latency history is in-memory and local to each replica. A restarted replica treats every model as unknown until it collects fresh samples.
Limitations
- Probabilistic routing: Short-term traffic may not match exact weights.
- Minimum volume needed: Requires sufficient requests for statistical accuracy.
- Response variations: Different models may return varying output quality.
- Cost complexity: Managing billing across multiple providers.
- Provider dependencies: Requires API access to all models.
- Per-replica latency state: With
latency_based, latency history is in-memory and local to each AI Gateway replica. It is not shared across replicas and does not persist across restarts.