LLM Gateways and OpenAI-Compatible APIs: Routing, Fallbacks and Cost Control
Why teams put an LLM gateway in front of model providers: unified API, routing, fallbacks, caching, rate limits, spend tracking and observability.
Published December 1, 2026
Once an organisation uses several models, perhaps a frontier model for hard tasks, a cheap model for classification and a self-hosted model for sensitive data, calling each provider directly from every service becomes messy. An LLM gateway sits in the middle, offering one API to applications and handling routing, keys, fallbacks, budgets and logging centrally.
The concept
Many providers and self-hosted engines support an OpenAI-compatible chat completions API, which has become a common interface. Gateways build on that: applications send standard requests to the gateway, which translates them for the chosen provider, injects the right credentials, and returns a normalised response.
Gateways add features that are tedious to build in every service: routing by model name or rules, automatic fallback to another provider on errors or rate limits, retries with backoff, response caching for identical requests, per-team budgets and rate limits, PII redaction, and centralised logs of prompts, latency, tokens and cost for observability.
Gateway architecture
What a gateway provides
| Feature | Why it matters |
|---|---|
| Unified API | Swap providers without code changes |
| Fallbacks and retries | Survive provider outages and rate limits |
| Budgets and rate limits | Stop runaway costs per team or key |
| Caching | Avoid paying twice for identical requests |
| Logging and tracing | Debug quality issues, track cost per feature |
| Policy (PII redaction, model allow-lists) | Compliance and data protection |
Fallback in action
Practical examples
Route by alias
Applications ask for a capability alias, not a vendor model ID.
model_aliases:
smart: { primary: provider-a/large-model, fallback: provider-b/large-model }
fast: { primary: provider-a/small-model }
private:{ primary: vllm-internal/qwen2.5-7b }
budgets:
team-support: { monthly_usd: 500 }Calling the gateway from an app
Standard SDK, gateway URL and a virtual key.
from openai import OpenAI
client = OpenAI(base_url="https://llm-gateway.internal/v1", api_key=TEAM_VIRTUAL_KEY)
r = client.chat.completions.create(model="fast", messages=[{"role": "user", "content": "Classify: ..."}])Tag requests for cost attribution
Metadata headers let you report spend per feature.
client.chat.completions.create(model="smart", messages=msgs,
extra_headers={"x-feature": "ticket-summary", "x-user-tier": "pro"})Common mistakes
- Hard-coding vendor model IDs throughout the codebase.
- No budgets, discovering a runaway loop from the monthly bill.
- Logging full prompts containing personal data without redaction or retention policies.
- Assuming every provider behaves identically behind an OpenAI-compatible API (features and limits differ).
Key takeaways
- A gateway gives one API, central keys and consistent observability.
- Fallbacks, retries and caching improve reliability and cost.
- Budgets and rate limits prevent runaway spend.
- Use capability aliases so models can change without code changes.
FAQ
Build or buy a gateway?
Open-source and commercial gateways exist; small teams can start with a thin internal proxy and adopt a product when needs grow.
Does a gateway add latency?
Usually a few milliseconds, negligible compared to model inference time.
Can it handle streaming?
Yes, good gateways proxy streaming responses and still record token usage at the end.