Drills D08

LLM gateway with routing, caching and fallbacks

Twenty internal teams call three LLM providers directly, each with its own keys and retry code. Design a shared LLM gateway that handles routing, caching, fallbacks, rate limits and cost attribution.

45 minutes · revise 11.08 · 11.09 · 11.10 · 31.03 · 31.04 · 32.08 · 32.09 · 30.06

Clarify and scope

A gateway is a reverse proxy with opinions; the design follows from traffic and rules. Ask:

Out of scope: training and model hosting (the self-hosted model is just a backend).

Baseline architecture

 clients (house SDK or OpenAI-compatible HTTP)
        |
 [auth]          virtual key -> team, feature tag, budget, allowed aliases
        |
 [policy]        size limits, PII redaction (per team), provider/region allowlist
        |
 [rate limiter]  per-team RPM + TPM token buckets in a shared store (Redis)
        |
 [exact cache]   opt-in per alias, temperature 0 only  --hit--> response
        |
 [router]        alias "fast" / "smart" / "vision" -> ordered candidate list
        |        filtered by health, region policy, context length, capability
        |
 [adapters]      provider A (primary)   provider B (fallback)   self-hosted
        |        retries honour Retry-After, jitter, retry budget, circuit breaker
        |
 [stream proxy]  SSE pass-through, usage captured from the final event
        |
 [usage ledger]  async: trace id, tokens (in / cached / out), cost, latency,
                 attempts, fallback_from, cache status -> warehouse + dashboards

The gateway is stateless apart from Redis (rate limits, exact cache) and an async log pipeline. Run at least three replicas across zones: every AI feature now depends on it.

Key choices and trade-offs

Aliases, not model ids. Teams ask for smart-v3, not a dated model string. The platform team maps aliases to models. A model upgrade becomes a config change that has to pass that alias’s eval suite before it ships. Teams that need a pinned model can ask for one, and pinned requests never fall back silently.

How much to normalise. Messages, tool calls, structured output and streaming events all differ between providers. A lowest-common-denominator API is portable but hides useful features (prompt-cache hints, reasoning effort, server-side tools). The usual compromise is a normalised core plus a provider_options pass-through, with adapters that fail loudly when an option is not supported.

Routing, in increasing order of risk:

  1. Static: alias to one model.
  2. Health-aware: skip a provider whose circuit breaker is open.
  3. Rule-based: route long contexts to a long-context model, a premium tier to a bigger model, background jobs to a batch lane.
  4. Cascade: try a cheap model first and escalate when a validator fails or confidence is low.
  5. Learned router: a classifier predicts which model is good enough.

Start with 1 to 3. Add a cascade only for an alias whose eval shows the cheap model passes on most traffic.

Caching has three layers, and they behave very differently.

Fallbacks. First classify the error:

For retryable errors: honour the provider’s Retry-After, otherwise use exponential backoff with jitter. Cap total retries with a retry budget (for example, retries may add at most 10% to traffic). Open a circuit breaker per provider, model and region after repeated failures. Fall back to a different provider only if the alias allows it, because a fallback model can change output quality, cost and data residency. With streaming, transparent fallback is possible only before the first token reaches the client.

Rate limiting. Use token buckets per team for both requests per minute and tokens per minute. Estimate tokens before the call, then reconcile with real usage afterwards. Add priority classes so interactive traffic preempts batch traffic when the shared provider quota is tight.

Build or buy. Open-source and hosted gateways (LiteLLM, Portkey, Kong and others) already do the adapter and logging work. Aliases, fallback policy, residency and attribution stay your decisions.

Evals

A gateway should not change what a model says. That means it needs:

Guardrails and fallbacks

Observability and cost

Every request logs:

Follow the OpenTelemetry GenAI semantic conventions so traces join with the callers’ traces. Dashboards show cost per team and per feature, error and fallback rate per provider, cache hit rate per alias, and p95 TTFT. Reconcile with invoices monthly.

The numbers

Token throughput at peak. Assume 2,000 input and 400 output tokens per request. 200 req/s × 60 s × 2,400 tokens = 28.8M tokens per minute. Compare that with your provider rate-limit tiers. You will likely need several keys or projects, more than one region, or more than one provider just for capacity.

Concurrent streams. Assume a streamed response lasts 10 s on average. By Little’s law, concurrency = arrival rate × duration = 200/s × 10 s = 2,000 open streams. That calls for an async server, not one thread per request.

Gateway overhead budget (design targets). Auth from an in-memory cache, about 0. One Redis round trip for rate limiting and one for the cache lookup, typically around a millisecond each in the same zone (measure yours). Logging off the request path. That leaves plenty of room under the 20 ms p99 target. A synchronous call to a remote database on every request would use it up.

Cost per 1K requests (P in USD per million tokens; check current prices). Assume 1,500 of the 2,000 input tokens are a cached prefix:

C_1K = 1000 x [(T_in - T_cached) x P_in + T_cached x P_cached + T_out x P_out] / 1e6
     = 0.5 x P_in + 1.5 x P_cached + 0.4 x P_out   dollars

Retry amplification. Suppose a provider has a 30% error rate during a brownout and clients make up to 3 attempts. Expected attempts per request = 1 + 0.3 + 0.09 = 1.39, so the struggling provider gets 39% more load. That is why you need retry budgets and circuit breakers.

Availability. Two providers that are each 99.5% available and fail independently are both down 0.005² = 0.0025% of the time, which is 99.9975% combined. They are not fully independent (shared clouds, your own gateway), so the gateway’s replicas and zones now limit the real number.

Exact cache memory. 1M entries × (32-byte key + about 2 KB response) ≈ 2 GB of Redis, before per-key overhead.

Follow-ups they will ask

  1. A stream fails halfway. Can you fall back? Not transparently. Before the first token you can. After it, return an error event with the partial text and let the client retry, or re-request with the partial output as a prefix where the provider allows it.
  2. How do you attribute cost to features? Virtual keys per team plus a required feature tag. The ledger keeps both, and a request without a tag is rejected or billed to the team’s “untagged” line.
  3. Would you add a semantic cache? Only for narrow, non-personalised, high-repeat aliases, scoped per tenant, with the threshold calibrated on labelled pairs. Prompt caching comes first.
  4. How do you roll out a new model behind an alias? Run the alias eval suite, then shadow traffic, then a canary percentage with per-alias dashboards. Roll back by changing config.
  5. How do you stop one team starving the others? Per-team TPM buckets, priority classes, and a separate batch lane that uses provider batch APIs.
  6. What if the fallback provider is not allowed for a team’s data? The residency policy filters fallbacks too. That team gets a clean 503, never a silent reroute.

How to bring in your own work

You have built the in-process version of this gateway more than once. You have not run a shared, multi-tenant one.

esc

Narrow with repo:jm layer:eval q:naive mine · ↑↓ to move, enter to open