DEV Community

Ashwin Giridharan
Ashwin Giridharan

Posted on Edited on

Building an AI Model Gateway: Intelligent LLM Routing Across Multiple Providers

AWS re:Invent 2026 has put an important AI infrastructure problem in the spotlight.

Amazon Bedrock Gateway and smart routing introduce centralized model-request management, spending controls and model selection based on request complexity.

It raises an interesting engineering question:

Why should every application decide which LLM provider to use?

If you're building an AI platform with multiple model providers, this is an architecture worth exploring.

Let's design a simplified version.

1. The problem: provider logic everywhere

Imagine you have three backend services:

Customer Service
    └── OpenAI API

Document Service
    └── Anthropic API

Agent Service
    └── Gemini API
🏆 ยอดนิยม: ashwin giridharan/building an ai model gateway intelligent llm routing across multiple providers dpg - ประเด็นร้อน 🏆 ยอดนิยม: ashwin giridharan/building an ai model gateway intelligent llm routing across multiple providers dpg - ประเด็นร้อน

Each service maintains its own:

  • API credentials.
  • Provider configuration.
  • Retry behavior.
  • Model selection.
  • Usage tracking.
  • Error handling.

Now imagine switching providers or introducing a cheaper model.

You have multiple applications to modify.

This is the same coupling problem that API gateways helped solve in distributed systems.

2. Introduce a shared model gateway

Instead, applications communicate with one internal service.

       Java / Python / Node Applications
                     |
                     v
                Model Gateway
                     |
          +----------+----------+
          |          |          |
          v          v          v
       OpenAI    Anthropic    Gemini
🏆 ยอดนิยม: ashwin giridharan/building an ai model gateway intelligent llm routing across multiple providers dpg - ประเด็นร้อน 🏆 ยอดนิยม: ashwin giridharan/building an ai model gateway intelligent llm routing across multiple providers dpg - ประเด็นร้อน

The application sends a task request.

The gateway determines which provider and model should handle it.

The application doesn't need to know the provider-specific details.

3. Define a common request contract

Start with a provider-independent request structure.

from pydantic import BaseModel
from typing import Optional

class ModelRequest(BaseModel):
    task_type: str
    prompt: str
    tenant_id: str
    user_id: str
    max_budget: Optional[float] = None
    quality_requirement: str = "balanced"
🏆 ยอดนิยม: ashwin giridharan/building an ai model gateway intelligent llm routing across multiple providers dpg - ประเด็นร้อน 🏆 ยอดนิยม: ashwin giridharan/building an ai model gateway intelligent llm routing across multiple providers dpg - ประเด็นร้อน

The gateway receives a standardized request regardless of which provider eventually handles it.

4. Build a basic routing engine

Start with deterministic routing rules.

def select_model(request):

    if request.task_type == "classification":
        return "provider_a_small"

    if request.task_type == "summarization":
        return "provider_b_fast"

    if request.task_type == "complex_reasoning":
        return "provider_c_reasoning"

    return "provider_a_default"
🏆 ยอดนิยม: ashwin giridharan/building an ai model gateway intelligent llm routing across multiple providers dpg - ประเด็นร้อน 🏆 ยอดนิยม: ashwin giridharan/building an ai model gateway intelligent llm routing across multiple providers dpg - ประเด็นร้อน

This is not yet intelligent routing.

It is a useful baseline.

A production router can evolve to consider historical performance, estimated cost, latency, provider availability and task quality requirements.

5. Separate providers from routing logic

Avoid putting provider-specific API calls inside the routing engine.

Define a common interface:

from abc import ABC, abstractmethod

class LLMProvider(ABC):

    @abstractmethod
    async def generate(self, prompt: str):
        pass
🏆 ยอดนิยม: ashwin giridharan/building an ai model gateway intelligent llm routing across multiple providers dpg - ประเด็นร้อน 🏆 ยอดนิยม: ashwin giridharan/building an ai model gateway intelligent llm routing across multiple providers dpg - ประเด็นร้อน

Then implement provider adapters:

class OpenAIProvider(LLMProvider):

    async def generate(self, prompt: str):
        # Call OpenAI API
        pass


class AnthropicProvider(LLMProvider):

    async def generate(self, prompt: str):
        # Call Anthropic API
        pass
🏆 ยอดนิยม: ashwin giridharan/building an ai model gateway intelligent llm routing across multiple providers dpg - ประเด็นร้อน 🏆 ยอดนิยม: ashwin giridharan/building an ai model gateway intelligent llm routing across multiple providers dpg - ประเด็นร้อน

The gateway selects a provider adapter and invokes it through the common interface.

This is an application of the Strategy pattern.

It also makes provider integrations easier to test independently.

6. Add budget enforcement

Model routing without cost governance is incomplete.

For example, consider a tenant with a monthly inference budget.

Before sending a request to a provider, the gateway should:

  1. Identify the tenant and user.
  2. Retrieve the applicable budget policy.
  3. Estimate the request cost.
  4. Check current consumption.
  5. Reject or route the request according to policy.
  6. Record actual usage after completion.

A simplified interface:

class BudgetManager:

    async def authorize(
        self,
        tenant_id: str,
        estimated_cost: float
    ) -> bool:

        remaining = await self.get_remaining_budget(
            tenant_id
        )

        return estimated_cost <= remaining
🏆 ยอดนิยม: ashwin giridharan/building an ai model gateway intelligent llm routing across multiple providers dpg - ประเด็นร้อน 🏆 ยอดนิยม: ashwin giridharan/building an ai model gateway intelligent llm routing across multiple providers dpg - ประเด็นร้อน

This example omits concurrency control, atomic reservations, reconciliation and currency/pricing changes. Those are essential for production.

In a multi-tenant environment, budget checks must also be protected against concurrent requests overspending the same balance.

7. Add controlled fallback

What happens when a provider is unavailable?

A gateway should distinguish between:

  • Authentication failures.
  • Rate limits.
  • Provider outages.
  • Timeouts.
  • Invalid requests.
  • Budget exhaustion.

Not every failure should trigger a fallback.

For example, a timeout may justify a controlled retry or alternate provider. An authorization failure should not be bypassed by silently switching providers.

Fallback must preserve the original security, quality and data-handling requirements.

8. What production readiness actually requires

Before calling this a production model gateway, I would add:

Capability Why it matters
Authentication Identify calling applications and users
Tenant isolation Prevent cross-tenant access
Model registry Manage model capabilities and configurations
Routing policies Select models using measurable criteria
Budget reservations Prevent concurrent overspending
Provider adapters Avoid vendor coupling
Retry and circuit breakers Handle provider instability
Observability Measure cost, latency and quality
Audit records Explain routing and authorization decisions
Evaluation pipeline Continuously validate routing quality

One important design decision: never assume that a provider fallback is automatically safe. Data residency, retention, privacy and model capabilities must be checked before switching.

9. The next evolution: adaptive routing

Once the gateway collects enough evaluation data, routing can become more sophisticated.

For example:

Incoming Request
       |
       v
Task Classification
       |
       v
Routing Policy
       |
       v
Candidate Models
       |
       v
Cost / Latency / Quality Evaluation
       |
       v
Selected Provider
       |
       v
Execution
       |
       v
Outcome Evaluation
       |
       v
Routing Feedback
🏆 ยอดนิยม: ashwin giridharan/building an ai model gateway intelligent llm routing across multiple providers dpg - ประเด็นร้อน 🏆 ยอดนิยม: ashwin giridharan/building an ai model gateway intelligent llm routing across multiple providers dpg - ประเด็นร้อน

The gateway can learn which models perform well for specific task categories.

However, I would keep policy enforcement deterministic and make routing decisions explainable. An adaptive router should not be allowed to ignore budget or security constraints.

Final thoughts

I think AI infrastructure is moving toward a model where applications don't need to know which LLM provider is behind every request.

They need a reliable contract.

The infrastructure should manage model selection, provider integrations, cost controls, observability and resilience.

AWS's latest announcements are an interesting validation of this architectural direction.

The opportunity is no longer just building applications that call LLMs. It is building the infrastructure that allows thousands of applications to use LLMs intelligently.

What does your current architecture look like? Do you use a shared model gateway, or does each service integrate directly with its LLM provider?

About the Author

I'm Ashwin Giridharan, a Cloud & Enterprise Solutions Architect focused on building scalable systems and exploring the architecture of AI.

My interests include enterprise architecture, cloud platforms, distributed systems, Generative AI, LLMs, RAG, Agentic AI, and taking AI solutions from experimentation to production.

I share practical insights, technical explorations, and lessons learned while building and understanding modern technology systems.

Connect and explore my work:

Website: https://ashwingiridharan.com/

LinkedIn: https://www.linkedin.com/in/ashwingiridharan7/

GitHub: https://github.com/giridharan-ashwin

Top comments (0)