Sarghaby Araghatta
Home / Knowledge Base / Model routing, fallback & load balancing
Serving & infrastructure · AI Engineering

Model routing, fallback & load balancing

A decision layer that picks which model/provider/endpoint serves a request, retries or fails over on errors, and spreads traffic across replicas and API keys.

Route to Model AFallback to Model BIncoming RequestRouting Decision(Policy-based)Model A(Primary)Model B(Secondary)Load Balancing(Distribute Requests)
Model routing and fallback flow

How it works

Routing operates by evaluating the nature of the incoming request and selecting the most appropriate model based on predefined policies. For example, if a prompt is straightforward, the system might route it to a cheaper, less complex model. However, for more challenging prompts, it might escalate the request to a more advanced model, or even use a classifier to determine the right model based on confidence levels. This decision-making process ensures that the system optimally allocates resources based on the task at hand, improving efficiency and response times.

Fallback mechanisms are crucial for maintaining service continuity. When a request encounters issues like 429 (Too Many Requests) or 5xx errors, the system automatically retries the request using a secondary model or provider. For instance, if the primary model hosted on Azure OpenAI fails, the system can switch to OpenAI's direct API or a model from Bedrock Claude. This retry logic often incorporates exponential backoff strategies to prevent overwhelming the secondary service, ensuring that the system remains resilient under load.

Load balancing distributes incoming requests across multiple model instances or API keys to avoid hitting rate limits. It can use strategies like round-robin or least-busy algorithms to ensure even traffic distribution. For example, if one API key is nearing its request limit, the system can redirect traffic to another key that has more capacity. This not only enhances throughput but also optimizes resource usage, allowing for a smoother user experience.

  • Route based on prompt complexity.
  • Retry on errors with backoff.
  • Distribute requests across models.
  • Use fallback for high reliability.
  • Consider cost/quality in routing.

The software-engineering analogue

Think of this system as akin to an API gateway combined with a service mesh, which manages traffic and ensures reliability through circuit breakers and weighted load balancing. However, the key difference lies in the nature of the 'instances' being routed. In this context, the models are not identical; they vary significantly in terms of pricing, quality, context limits, and even the formats of prompts they accept. This means that routing decisions must consider both cost and quality, rather than relying solely on health checks of the services.

Why not the obvious alternative

A common initial approach is to use a single hardcoded model for all requests. This can lead to significant issues; if that model experiences an outage or hits a rate limit, the entire application can fail, leading to downtime and user dissatisfaction. This approach often results in overpaying because trivial prompts are sent to the most expensive model available, wasting resources.

By implementing a routing system, you gain resilience against provider outages and rate limits, ensuring that your application can continue functioning smoothly. This system allows for better cost management, as it enables you to direct less demanding requests to cheaper models, optimizing your overall expenditure while maintaining performance.

When to use which

For applications where reliability is critical, such as production environments, it is essential to implement a multi-provider fallback system to ensure uptime. In cases where cost is a significant concern, especially with high volumes of requests, tiered or cascade routing can help manage expenses effectively. Conversely, for simple internal tools where complexity is unnecessary, sticking with a single model may suffice and can prevent premature complications in the architecture.

Common Misconception Many assume a single model suffices, but this limits resilience and can lead to higher costs.

In production: when it breaks

The scenario Every request is routed to a 70B model, resulting in ~300ms average latency, while ~70% of the queries are simple and could be handled by a smaller model.
if simpleif complexSymptomHigh latency(300ms)Check querycomplexitySimple orcomplex query?Route toappropriate modelMonitoraccuracy/latency
Routing and fallback decisions

Why it happens. The routing logic does not differentiate between query complexities, leading to inefficient use of resources. Simple queries unnecessarily consume time and cost associated with the larger model, while the smaller model remains underutilized.

  • Check the query complexity distribution to categorize requests into simple, moderate, and complex.
  • Measure the latency and accuracy of responses from both the large and small models for each category of queries.
  • Log the escalation rate when simple queries are routed to the large model and compare against a threshold.
The call Implement a routing mechanism that directs simple queries to the smaller model, escalating to the larger model only on low confidence responses. The tradeoff is increased complexity in routing logic, but it will significantly reduce latency and cost. Monitor accuracy, latency, and cost per request to ensure the routing remains effective.

What it delivers

This routing, fallback, and load balancing strategy leads to higher effective uptime, reduced blended costs per request, and the flexibility to switch models smooth without requiring changes to the application code. It enhances the overall reliability and efficiency of the system, making it a crucial component for any scalable AI-driven application.