What is an LLM gateway?
Definition
An LLM gateway is a service between your applications and model providers that every model call passes through. It exposes one API for many providers and handles keys, per-team budgets, rate limits, fallbacks, caching and logging, so applications can switch or combine models without changing their own code.
What does an LLM gateway do?
Without a gateway, every application holds its own provider keys, its own retry code and its own idea of what a request costs. A gateway moves those concerns into one service that all model traffic passes through. The jobs it takes on, as three open-source and managed gateways document them:
| Job | What it means in practice | Where it is documented |
|---|---|---|
| One API for many providers | Apps call one OpenAI-compatible endpoint; the gateway translates to each provider | LiteLLM: one endpoint "for 100+ LLM providers" |
| Credentials and access | Provider keys live in the gateway; each app or team gets its own key with allowed models | LiteLLM virtual keys; Cloudflare "Bring Your Own Keys" |
| Spend and rate limits | Budgets and request limits per key, team, model or provider | LiteLLM budgets; Cloudflare spend limits with daily, weekly or monthly windows |
| Failover | Retries, load balancing and fallbacks when a provider errors or rate-limits | LiteLLM fallbacks; Agent Router automatic failover |
| Routing | Stable model aliases, conditional routes, percentage traffic splits | Cloudflare routing |
| Caching | Identical requests served from cache instead of the provider | Cloudflare caching |
| Policy | Content filters, PII masking or data loss prevention on prompts and responses | LiteLLM guardrails; Cloudflare DLP |
| Records | Cost, tokens, latency and errors per request, sent to your logging tools | LiteLLM logging to OpenTelemetry, Prometheus and others; Cloudflare analytics |
The sources: the LiteLLM AI Gateway overview, Cloudflare's AI Gateway features page, and the Agent Router overview (the project formerly called Envoy AI Gateway, now under the Agentic AI Foundation). Several gateways now front MCP tools as well: LiteLLM says the same gateway gives access to MCP tools and A2A agents, with the same keys and spend records.
How is an LLM gateway different from an API gateway, a router or a framework?
| Component | What it decides | What it does not do |
|---|---|---|
| API gateway | Who may call which HTTP route, at what request rate | Usually not: count tokens, price a call or translate between model providers |
| LLM gateway | Which provider and key serve a model call, within which budget, logged how | Decide what the application asks the model |
| LLM router | Which model should answer this request | Hold keys, budgets or logs (Cloudflare ships one, Auto Router, as a gateway feature) |
| Agent framework | The agent's loop, tools, memory and handoffs | Govern traffic shared by many apps |
The Agent Router docs draw the last line themselves: it "is not an agent framework and does not tell an agent how to reason." Its value, the same page says, is clearest when AI traffic crosses provider, team, security, cost or operational boundaries.
When do you need an LLM gateway?
The LiteLLM docs give a short test. Use the gateway if more than one application or person sends requests to LLMs, or if you need virtual keys, spend tracking, logs in one location or guardrails. Use their Python SDK instead if one Python application sends the requests and you need none of those controls.
The Agent Router overview adds signals from the platform side: provider credentials should not live inside application workloads, teams should not integrate with every provider separately, and you need consistent permissions, quotas, usage attribution or failover.
A single product with one provider and one team may not need one yet. A thin model layer in your own code, with retries and cost logging, covers that case until a second team or provider arrives.
Example: a provider fails, and the app never notices
We ran the LiteLLM proxy (version 1.110.1, Python 3.11) with two model groups. LiteLLM's mock_response setting stands in for both providers, so no API keys are needed: the primary always returns a rate-limit error, and the backup returns a fixed answer.
# config.yaml: two model groups behind one endpoint.
# mock_response stands in for the providers: the primary always returns a rate-limit error.
model_list:
- model_name: support-chat
litellm_params:
model: openai/primary-model
api_key: fake-key
mock_response: litellm.RateLimitError
- model_name: support-chat-backup
litellm_params:
model: anthropic/backup-model
api_key: fake-key
mock_response: "Your order ships on Monday."
router_settings:
num_retries: 0
fallbacks: [{"support-chat": ["support-chat-backup"]}]
general_settings:
master_key: sk-local-test
We started it with litellm --config config.yaml --port 4011 and called it from a client that knows only one model name:
# call.py: the app calls one OpenAI-compatible endpoint; the gateway picks the provider.
import json, urllib.request
req = urllib.request.Request(
"http://localhost:4011/v1/chat/completions",
data=json.dumps({"model": "support-chat",
"messages": [{"role": "user", "content": "When does order A-42 ship?"}]}).encode(),
headers={"Authorization": "Bearer sk-local-test", "Content-Type": "application/json"},
)
with urllib.request.urlopen(req) as r:
body = json.load(r)
print("asked for: support-chat")
print("answered by:", r.headers.get("x-litellm-model-group"), "/", r.headers.get("x-litellm-model-name"))
print("attempted fallbacks:", r.headers.get("x-litellm-attempted-fallbacks"))
print("reply:", body["choices"][0]["message"]["content"])
Output of python call.py:
asked for: support-chat
answered by: support-chat-backup / anthropic/backup-model
attempted fallbacks: 1
reply: Your order ships on Monday.
The application got an answer, and from its own code it could not tell that a different provider wrote it. That is the point of a gateway, and also its main risk. The only trace is in the response headers and the gateway logs, so record which model answered every request, and run your evals against each fallback model, not only the primary.
What can go wrong with fallbacks?
The LiteLLM fallback docs list three fallback types: content_policy_fallbacks for content policy errors, context_window_fallbacks for requests that exceed a model's context window, and fallbacks for all other errors, such as rate limits. Fallbacks run in the order listed.
Two details from the same page matter in production:
- Reasoning state does not travel across providers. A fallback to another provider or another API key cannot decrypt the encrypted reasoning the failed deployment produced on earlier turns. LiteLLM drops what the target cannot decrypt, so the fallback model reasons fresh on that turn.
- Proxy fallbacks need a real failure to test. From LiteLLM Proxy v1.85.0,
mock_testing_fallbacksand the related mock flags are stripped from incoming proxy requests. The docs say to trigger an actual provider error in a non-production environment instead. (Our example above usesmock_responsein the model config, which is a different setting.)
What should you check before an LLM gateway goes to production?
- It is not a new single point of failure. Run more than one instance, with health checks, since every model call now depends on it.
- Its added latency is measured on your real traffic, at peak load.
- The answering model is logged for every request, next to tokens and cost.
- Budgets sit at the right level: per team or app key, with alerts before the hard limit.
- Cache rules are safe. Never serve one user's cached answer to another, and keep personal data out of shared caches.
- Prompt logging follows your data rules. A gateway that records every prompt is also a store of everything users typed.
- Evals cover every model the gateway can route to. A fallback that has never been scored is an untested release.
For the cost side of routing, see inference cost; for content filters at the gateway, see AI guardrails. In our generative AI development work, we keep a thin model layer so you can switch or combine providers, and re-run evals to compare them.
Sources
- LiteLLM AI Gateway, LiteLLM docs (BerriAI), 2026-10-13
- Fallbacks (Provider Failover), LiteLLM docs (BerriAI), 2026-11-17
- Agent Router Overview, Agent Router (formerly Envoy AI Gateway) docs, 2026-10-06
- Features, Cloudflare AI Gateway docs, 2026-11-04
Frequently asked questions
Is an LLM gateway the same as an AI gateway?
The names overlap. Cloudflare calls its product AI Gateway and LiteLLM calls its server the LiteLLM AI Gateway, and both route model calls. The broader name fits gateways that also front MCP tools and agents, as LiteLLM's does.
Does an LLM gateway add latency?
Every request makes one extra hop and some processing, so measure it under peak load. A cache hit at the gateway skips the provider call, which can make repeated requests faster than going direct.
Should we build our own LLM gateway?
Only if your access, logging or data rules cannot be met by an existing gateway. Building one means owning provider translation, streaming, retries and cost tables as providers change their APIs.
Can an LLM gateway stop prompt injection?
It can run input and output filters on every call, which helps. It cannot see what an agent does with a tool result inside its own loop, so keep checks in the application too.
Mikhail BodnarchukFounder of Codeception and CodeceptJSBoth are open-source testing frameworks: Codeception for PHP, CodeceptJS for JavaScript.LinkedInGitHub