Topic 1 of 4 · 4 min read
What an LLM gateway does for you
Goal: Decide which concerns, such as keys, logging, budgets and fallbacks, a gateway should own in a given architecture.
A gateway is one service between your app and every model provider, and it should own the concerns that cut across all calls: keys, logging, budgets, caching and fallbacks. Your feature code keeps asking for "a completion"; the gateway decides who answers and records what happened.
Check yourself · 1 of 3
Five services each hold their own provider key. Finance cannot tell which feature caused a spike. Which gateway feature fixes that directly?
Why it matters
Without a gateway, every service holds its own provider keys, writes its own retry loop and logs prompts in its own format. When the finance team asks "which feature spent that money?" nobody can answer. When one provider has an outage, ten services fail ten different ways. Tools in this space include LiteLLM (a proxy you host yourself), OpenRouter (a hosted endpoint for many models) and Cloudflare AI Gateway (a hosted layer in front of provider APIs). Cloud providers sell similar products.
What a gateway can own
- Keys. Apps get a virtual key from the gateway. The real provider keys live in one place and can be rotated without redeploying anyone.
- Logging. One record per call: caller, model, token counts, latency, status. This is how you find the feature that costs the most.
- Budgets and rate limits. A per-team or per-key spend cap stops a runaway loop before the invoice does.
- Caching. An identical request can return a stored answer. It saves money only where repeated identical prompts really happen, such as a fixed FAQ.
- Retries and fallbacks. If the first model errors or times out, the gateway tries the next one in a list.
What it should not own
Prompt content, business rules and output parsing belong in your app. A gateway sees text and tokens; it does not know what a valid answer looks like for your feature.
Before and after
Before: the support bot, the search summariser and a nightly batch job each embed a provider key and a three-line retry. The batch job loops on a bug and burns the month's budget over a weekend.
After: all three call one gateway URL with their own virtual keys. The batch key has a daily cap, so the loop stops at the cap and an alert fires on Saturday morning. Provider keys exist only in the gateway's secret store.
# LiteLLM-style proxy config (sketch)
model_list:
- model_name: summarise
litellm_params:
model: anthropic/claude-sonnet-4-5
api_key: os.environ/ANTHROPIC_API_KEY
What goes wrong
- Gateway as a dumping ground. Teams push prompt templates and business logic into gateway config, where nobody tests it.
- Fallback without a quality check. A cheaper backup model may fail your format or safety needs, so failover quietly lowers quality.
- Logging everything. Full prompts in gateway logs can hold personal data. Decide what is stored and for how long.
- One gateway, no plan for its outage. See the trade-offs topic.
Examples
Capping a nightly batch job
A batch job that summarises tickets gets its own virtual key with a daily spend limit and an allow-list of one cheap model. A bug makes it retry forever. The key hits its cap, calls start returning a budget error, and the team is paged. Other services, with their own keys, are unaffected.
Choosing a tool
A two-person startup that wants to try many models quickly can use a hosted endpoint such as OpenRouter. A company with data rules that forbid third parties seeing prompts may run LiteLLM in its own cluster. A team already on Cloudflare may add AI Gateway for caching and logs without new servers.
Key points
- A gateway centralises cross-cutting concerns: provider keys, logging, budgets, caching and fallbacks.
- Apps hold virtual keys with caps, so one runaway loop cannot spend the whole budget.
- Per-call logs with caller, model, tokens and latency answer "which feature costs what".
- Caching only pays off where identical prompts repeat; fallbacks only help if the backup model meets your quality bar.
- Prompts, business rules and output validation stay in your application.
Common mistakes
- Putting prompt templates and business rules in gateway config instead of application code.
- Adding a fallback model without checking it meets the same format and quality needs.
- Logging full prompts and responses without deciding retention or who may read them.
- Giving every service the same shared key, so spend cannot be traced to a feature.
Check yourself · 2 of 3
Which of these should stay in your application instead of the gateway?
Check yourself · 3 of 3
A nightly job loops on a bug. You want it to stop spending without touching other services. What do you set?
Practice items, not counted towards your mastery.
Start here
- LiteLLM documentation (opens in a new tab)DocsFree
One interface and proxy server for many providers, with keys, budgets and logging.
- OpenRouter quickstart (opens in a new tab)DocsFree
Calling many models through one OpenAI-compatible endpoint with one key.
- AI SDK: providers and models (opens in a new tab)DocsFree
How a provider-neutral SDK swaps models behind one function call.
More for this topic
- LiteLLM virtual keys (opens in a new tab)DocsFree
Issuing per-team keys with budgets and model allow-lists in front of real provider keys.
- Cloudflare AI Gateway (opens in a new tab)DocsFree
A hosted gateway with caching, rate limits, logging and fallbacks in front of provider APIs.