Model gateways and routing
A model gateway (also called an AI gateway or LLM gateway) is a service that sits between your applications and the AI models they use. Applications call the gateway with one API; the gateway decides which model and provider handles the request, enforces limits and policies, and records what happened.
It’s the AI equivalent of an API gateway. Once more than a couple of teams use models, it’s usually the first piece of shared AI infrastructure an organization needs.
Diagram
Section titled “Diagram”- Every application calls the gateway with one API and its own credentials.
- The gateway checks the caller's quota, removes sensitive data if policy requires, and passes the request to the router.
- The router picks a model by task, cost and availability, and falls back to another if the first fails.
- The call's model, tokens, cost, latency and outcome are recorded for chargeback, debugging and audit.
How it works
Section titled “How it works”One API for many models. Applications send requests in one format. The gateway translates them for each provider, so switching or adding a model is a configuration change, not a code change in every application.
Routing. The gateway chooses a model per request. Common rules:
- By task: a small, fast model for classification and extraction; a large model for complex reasoning.
- By cost tier: a cheaper model for internal tools, the best model for customer-facing answers.
- By data sensitivity: requests containing regulated data go only to models hosted in approved regions or in your own network.
- By difficulty (cascading): try a small model first and escalate to a larger one when its confidence or an automatic check is low.
Fallback. If a provider times out, rate-limits or is down, the gateway retries with backoff and then falls back to another model. Fallback models must be evaluated for the task too; a fallback that gives worse answers silently is a quality incident.
Limits and cost control. Per-team and per-application quotas on requests and tokens, budgets with alerts, and maximum output lengths. Without these, one runaway agent loop can spend a month’s budget in an afternoon.
Caching. Exact-match caching of identical requests is safe and cheap. Semantic caching (returning a stored answer for a similar question) saves more but can return a wrong answer for a question that only looks similar; use it only where that risk is acceptable.
Policy. Redacting or blocking personal data before it leaves your network, applying input and output filters, and enforcing which models each application may use.
Observability. Every call logged with caller, model, version, token counts, cost, latency and outcome. This is what makes evaluation and monitoring and governance possible.
- Swap or add models without changing applications.
- One place to control cost, quotas and data policy.
- Resilience to a single provider’s outages.
- Consistent logs for chargeback, debugging and audit.
- Another hop of latency, and another critical dependency: if the gateway is down, every AI feature is down.
- A lowest-common-denominator API can hide provider-specific features teams want.
- Centralizing prompts and logs concentrates sensitive data in one place, which must be secured.
When to use it
Section titled “When to use it”- More than one team or application uses models.
- You use, or expect to use, more than one model or provider.
- You need to control cost, data residency or which models are approved.
When to avoid it
Section titled “When to avoid it”- One application, one model and one team. Use the provider’s SDK directly and add a gateway when the second team arrives.
Common pitfalls
Section titled “Common pitfalls”- Routing without evaluation. Sending requests to a cheaper model saves money only if its answers are good enough for that task. Measure it.
- Logging full prompts and responses with no retention policy, creating a store of customer data nobody planned for.
- Building everything in-house. Open-source and commercial gateways exist; build only the policies specific to you.
- No per-caller identity, so cost can’t be attributed and a misbehaving client can’t be throttled alone.
Related patterns
Section titled “Related patterns”- API gateway and circuit breakers
- Retries, timeouts and backoff
- RAG and AI agents, the main callers of a gateway
- LLMOps
Further reading
Section titled “Further reading”- OWASP Top 10 for LLM applications (opens in a new tab), several of which (unbounded consumption, sensitive information disclosure) are best handled at the gateway
- AI system readiness checklist
Written and edited by Abhinav Taduka, enterprise software architect.
New pages are added regularly. Follow Abhinav on LinkedIn to see them first.