Routing Strategies
The gateway supports four routing modes configured through thestrategy.mode field:
Single Mode
Routes requests to a single provider. This is the simplest mode and is used when you want direct routing without any failover or distribution logic.Fallback Mode
Automatically falls back to alternative providers when the primary provider fails. The gateway tries each target in sequence until one succeeds.- Define specific status codes that trigger fallback with
on_status_codes - If no status codes are specified, falls back on any non-2xx response
- Stops at the first successful response
- Preserves the original request format
Gateway exceptions (internal errors) automatically skip to the next target regardless of
on_status_codes configuration.Load Balance Mode
Distributes requests across multiple providers based on assigned weights. This mode is ideal for distributing load, testing multiple providers, or optimizing costs.- Weights determine the probability of selecting each provider
- Default weight is
1if not specified - A provider with weight
0.7receives approximately 70% of requests - The gateway uses probabilistic selection based on total weight
Conditional Mode
Routes requests based on dynamic conditions evaluated at runtime. This allows for intelligent routing based on request parameters.- Evaluate request parameters using JSON path queries
- Route to specific named targets based on conditions
- Define a default target for unmatched conditions
- Combine with other routing strategies within targets
Request Flow
The routing system processes requests through the following pipeline:Advanced Routing
Nested Strategies
You can nest routing strategies to create sophisticated routing logic:Circuit Breaker Integration
The routing system integrates with circuit breakers to automatically exclude unhealthy targets. When a target is marked as “open” (unhealthy), it’s automatically filtered from the available targets. Source:src/handlers/handlerUtils.ts:646-658
Provider Selection
The gateway’s provider selection logic is implemented inselectProviderByWeight() which:
- Assigns default weight of 1 to providers without specified weights
- Calculates total weight across all providers
- Selects a random value between 0 and total weight
- Iterates through providers subtracting their weights until the random value is consumed
src/handlers/handlerUtils.ts:204-231
Custom Host Routing
For providers with custom deployments, you can specify a custom host:Best Practices
Use Fallbacks for Reliability
Configure fallback targets to prevent downtime when a provider experiences issues.
Load Balance for Scale
Distribute load across multiple API keys or providers to avoid rate limits.
Test Conditional Logic
Thoroughly test conditional routing rules before production use.
Monitor Target Health
Use circuit breakers to automatically exclude unhealthy targets.
Next Steps
Configs
Learn how to structure and manage gateway configurations.
Load Balancing
Deep dive into load balancing strategies and weight distribution.
Providers
Understand the provider system architecture.
Retries
Configure automatic retries for failed requests.