LLM Routing and AI Gateways Compared
Pick a gateway, set aliases and fallback chains, route cheap or frontier, and hedge slow safe reads.
- Concepts and terms
- Tasks and commands
- Annotated code
- Diagrams
- Comparisons
- Defaults and numbers
- Troubleshooting
- Questions and tips
AI gateways side by side#
| Factor | LiteLLM | Portkey | Kong AI Gateway | Cloudflare / Vercel |
|---|---|---|---|---|
| Code | MIT OSS, Python | Apache 2.0 OSS since March 2026 | Built on Kong Gateway | Managed, zero-ops |
| Overhead | 5-15 ms | 20-40 ms | 3-8 ms | 1-3 ms at the edge |
| Scale | Breaks at ~2000 RPS, 8 GB | Control plane | 228% faster than Portkey | Edge advantage |
| Rate limit | Token-bucket | Tiered | Sliding-window | Limited |
| Guardrails | Optional | Opinionated guardrails, PII redaction | Opinionated guardrails | Limited |
| Price | $0 for OSS | $49/mo production tier | $100/model/month, max 5 on Plus | Managed only |
| Best fit | Python, <500 RPS, dev | Regulated industries | Already on Kong, >1000 RPS | Edge JavaScript apps |
Run the labs#
| Command | What it does |
|---|---|
python code/ | Routing gateway: normal, outage, PII and all-down scenarios |
python3 scripts/ | Creates a work folder. The starter fails until you implement it |
python3 scripts/ | Grades one stage |
python3 scripts/ | Grades the reference solution |
go run . --samples 3 --delay-ms 10 --out / | In solution: baseline against hedged on loopback |
go run . --primary <url> --secondary <url> | Hedges your own two safe read endpoints |
python3 scripts/ | Writes the completion evidence |
Defaults and limits#
| Setting | Default | Note |
|---|---|---|
hardSetThreshold | 0.88 | At or above it, the router pins frontier |
maxAttempts | 2 | Demo retry budget per provider |
| Free tier bucket | capacity: 2 | refillPerSec: 0.5, third call gets 429 |
--delay-ms | 10 | Hedge delay, allowed 0 to 1s |
--samples | 3 | Samples per policy, 1 to 100 |
--out | / | Receipt file |
| GET attempts | 2 | Body 64KiB, two-second ceiling, 100ms grace |
Troubleshooting#
| Symptom | Cause | Fix |
|---|---|---|
| Cascading failures near 2000 RPS | LiteLLM at its ceiling | Migrate to Kong first |
| TTFT P99 over a 100 ms SLA | Portkey adds 20-40 ms | Use Kong or Cloudflare |
| Escalation rate over 30% | Cheap model is over-routed | Route less traffic to the cheap model |
| Quality drops with no alarm | Cheap-model drift | Gate routes by online quality metrics |
| Outage takes the alias down | Chain has one provider | Add a second failure domain |
| Fallback cascades raise cost | Retries without a budget | Count retries against a budget |
| A fast error wins the hedge | Winner not validated | Require HTTP 200 and nonempty value |
| Cancel shows no saved work | Server keeps working | Read fixtureWork counts |
Use X when#
- LiteLLM
- you have an SRE team, want data sovereignty and stay below 500 RPS.
- OpenRouter
- managed SaaS, 300+ providers, credits. You want no infra.
- Portkey
- you need guardrails and compliance out of the box.
- Kong AI Gateway
- you already run Kong and need more than 1000 RPS.
- Cloudflare / Vercel
- you serve JavaScript at the edge and need zero ops.
- Self-host
- data residency forces it. Healthcare and finance default to self-host.
- No gateway
- a single-model prototype. Direct API calls are simpler.
Routing patterns#
| Factor | Pre-route | Cascade | Ensemble |
|---|---|---|---|
| How | Classifier up front | Cheap first, escalate | Cheap and frontier in parallel |
| Latency | ~5-10ms added, fastest | ~1.2x median, ~2x escalated | Parallel runs |
| Quality | Depends on classifier | Best quality floor | Highest |
| Cost | Cheap for simple only | Cheap plus frontier on escalation | Highest cost |
| Use when | Classifier is accurate | Quality floor matters | Critical A/B only |
Gateway request path#
One request: route()#
Alias routing gateway in code#
ROUTES = {"smart": [...], "fast": [...]}alias to fallback chainPRICES = {"openai/gpt-4o": (5.0, 15.0)}per 1M tokens, in and outinv = route("smart", [{"role": "user", "content": "<prompt>"}])inv.chosen_modelfirst model that answeredinv.attemptsevery model tried, in orderinv.input_tokens, inv.output_tokensfrom usageinv.cost_usdtokens times PRICESinv.redactedTrue when PII matchedinv.error"all providers failed"OUTAGE.add("openai/gpt-4o")simulate a primary outageTasks in code#
| Task | Code |
|---|---|
| Route an alias | route(alias, |
| Scrub PII before dispatch | redact_pii(text) |
| Compare routing patterns | simulate("CASCADE", |
| Pick a model by policy | router.pick(c.signals, |
| Gateway fallback simulator | simulate_fallback(gateway, |
| Retry one provider | callWithRetry(provider, |
| Try providers in order | callWithFallback(chain, |
| Hedge a safe read | Hedged(ctx, |
Numbers and questions#
- What does fallback buy?
- One provider at 3% error gives 97%. Two give 99.94%. Three give 99.997%.
- What does routing save?
- 20-60% at iso-quality in case studies.
- What does a 70/30 split do?
- The bill drops ~65% at the same product quality.
- What does semantic caching save?
- 30 to 60 percent on repeated agent loops.
- Which online gates catch drift?
- Thumbs per route, LLM-judge on a 5% sample, escalation and refusal rate.
- Which metric to alert on first?
- Fallback-use rate, then gateway latency P99.