Skip to content

That's just a taste — their profile full of gems!

Visit profile
User avatar
Stephen Williams
June 29883

LLM Gateway Pattern

I’m a big believer in one boring rule for production AI systems: don’t let every service talk to an LLM provider directly.

It looks fast in the early prototype phase. Then the org grows, a few teams add prompts, someone ships an aggressive retry loop, and now you’ve got scattered logs, mystery token burn, and a provider migration that somehow touches half the backend.

That’s where the LLM gateway pattern stops being “nice architecture” and starts being basic operational hygiene.

What I like about this pattern is that it solves very real problems, not slide-deck problems:

  • one bad service can burn shared quota for everyone
  • cost attribution is basically nonexistent when calls are spread across microservices
  • debugging becomes miserable because logs are fragmented
  • provider switching turns into a distributed refactor instead of a config change

A centralized gateway gives you one place to handle the stuff that should never be reimplemented 12 times: auth, quotas, routing, caching, logging, fallback logic, and provider abstraction.

That last one matters more than people admit. If your app services know about specific providers, model names, auth schemes, and failure behavior, you’re already coupling product code to vendor decisions. That’s not strategy. That’s debt.

The four components I’d consider non-negotiable are pretty straightforward.

Authentication: identify the caller, enforce scopes, and redact sensitive data before anything leaves your network. In enterprise setups, PII handling can’t be an afterthought.

Rate limiting: and not the fake kind. For LLM systems, request-per-minute limits are often the wrong primitive. What matters is usually tokens, not requests. A tiny classification call and a giant context-heavy generation request are not the same thing, and treating them as equal is how quotas get gamed accidentally.

Observability: centralized logs, token counts, latency, error rates, prompt analytics, and cost dashboards by team or feature. If you can’t answer “who spent the money?” or “which workflow is causing latency spikes?” you’re not operating an AI platform, you’re hoping.

Resilience: circuit breakers, fallback chains, graceful degradation, and queueing where it makes sense. Providers fail. Networks flap. Latency jumps. Pretending otherwise is amateur hour.

The implementation detail I especially liked: using Redis + Lua for atomic sliding-window quota enforcement.

That’s the kind of thing that separates toy diagrams from production design. Under concurrency, “check then update” logic falls apart fast if it isn’t atomic. And the token-based point is exactly right: using request count instead of actual token consumption is a common mistake in LLM systems.

Another subtle but important design choice: check cache before quota.

That sounds small, but it’s the kind of systems thinking I appreciate. If you get a semantic cache hit, you avoid both provider cost and unnecessary quota-system work. Cleaner and cheaper.

The circuit breaker piece is also table stakes in real deployments. Closed, open, half-open. Fast-fail unhealthy providers, probe for recovery, route to healthier alternatives. Nothing glamorous, just good engineering. Which, frankly, is what a lot of AI infrastructure still needs more of.

I also think the quota-enforcement framing is a strong interview and design problem: how do you stop one team from consuming the company’s entire token budget? The answer is never just “add rate limits.” You need fairness, priority classes, burst rules, and a clear stance on fail-open vs fail-closed when your control plane has issues.

That tradeoff is where architecture gets real.

If Redis goes down, do you allow traffic and risk a cost explosion? Or block traffic and accept an outage? There isn’t a universal answer. It depends on whether you’re protecting margin, uptime, or a user-facing workflow with hard SLA expectations.

My main takeaway: an LLM gateway is not just an API gateway with “AI” in the name.

It’s the control plane for model usage across the company.

If you’re building anything beyond a single isolated feature, I’d strongly prefer this shape:

client/service → gateway → cache/quota/auth/observability/routing → provider(s)

That gives you centralized governance, safer provider migrations, better reliability, and much better cost visibility.

And honestly, this is the kind of AI architecture discussion I want more of. Less “AI transformation” fluff, more specifics about token accounting, failure modes, routing policy, and what actually breaks at scale.

#systemdesign#llm#circuitbreaker#aiarchitecture#mlops#enterpriseai#productionai#llmgateway#ratelimiting
Like
Dislike

98

24

Add a comment...