Skip to content

System design walkthrough · 11 min

An LLM gateway for every team

Every team in the company calls language models. Put one front door in front of them: budgets that count tokens, room kept for customers, fallbacks you can see, and a cache that knows its limits.

Seen in any company with several teams calling model providers: chat, search, internal tools, batch jobs.

The ask

What we are building

Three teams call a model provider: customer support chat, in-app help, and a reports job that summarises long documents in the background. They share one account and its quota. Design the layer between them and the provider so customers are never the ones who get errors, costs are attributed to teams, and a provider problem does not quietly become a quality problem.

  • Customer-facing chat keeps working at the Monday morning peak.
  • Background jobs use spare capacity and wait when there is none.
  • Every token is attributed to a team, for budgets and the bill.
  • A provider outage fails over without anyone being served a worse model unknowingly.
  • The gateway itself must not become the outage.
Account quota
2M tokens/min
Chat message
~2,500 tokens
Report request
~20,000 tokens
Monday 9am vs quiet
6×

Back of the envelope

How big is it?

Illustrative numbers. The point is that request counts and token counts tell opposite stories about who is using the quota.

WhatWorkingResult
Chat at the Monday peak8 messages/s × 2,500 tokens × 60~1.2M tokens/min
Reports at the Monday peak60 requests/min × 20,000 tokens~1.2M tokens/min
Total wanted vs quota1.2M + 1.2M vs 2M20% over
Requests per minute: chat vs reports480 vs 60reports look 8× smaller
Tokens per minute: chat vs reports1.2M vs 1.2Mthey are equal
Reserve kept for chat2M × 60%1.2M tokens/min: just fits

Build it, then break it

Your turn: design it first

Decide how the gateway shares the quota before reading on. The simulator runs two chat apps and the reports job through your gateway, from quiet hours to the Monday 9am peak, when the reports job alone wants most of the quota.

1How does the gateway limit each team?

Answer every question to run it.

Version 1

Every team calls the provider with one key

Breaks · at Monday 9am: the reports job crowds out chat

The fastest start: every team gets the company's API key and calls the provider directly. There is one quota for the whole account and nobody can tell which team spent what.

On Monday morning the reports job starts on a weekend's worth of documents. Each request is large, and the job sends them as fast as the provider accepts them. The quota fills; the provider starts answering 429; the job retries and fills it again. The customers in the support chat get the errors, and support cannot tell why.

Quotas
1, shared
Who spent what
unknown

Try each load. Numbers are illustrative: a small simulation, not a measurement.

Why not…

  • A separate key and account per team?

    Isolates teams but splits the discount and quota into small pieces, and every team rebuilds retries, logging and fallback. Most companies end up wanting one front door with per-team limits inside it.

  • Ask the reports team to run at night?

    Helps until the next job, or the next team. A rule that lives in a meeting, not in the system, is not enforced at 9am on a Monday.

Go deeper:BulkheadsNot every 429 is a rate limit

Version 2

A gateway with request limits per team

Breaks · at Monday 9am: few huge requests still fill the quota

Put a gateway in front of the provider: every team calls it with its own key, it calls the provider, and it applies a rate limit per team, like any API gateway, counted in requests per minute.

The limits look fair: chat sends 480 requests a minute, the reports job 60. But a chat message is about 2,500 tokens and a report request about 20,000, and the provider's quota is in tokens. By tokens the reports job is as big as all of chat; its request limit barely touches it, and chat still gets 429s at the peak.

Limits count
requests
Provider counts
tokens

Try each load. Numbers are illustrative: a small simulation, not a measurement.

Why not…

  • Lower the reports job's request limit further?

    Tuned for today's document sizes; next month's longer documents fill the quota again. The limit has to count the thing the provider counts.

Go deeper:Token bucket rate limit

Version 3

Token budgets, with room kept for chat

Holds · at Monday 9am: reports wait, chat is answered

The gateway counts tokens. Before a call it reserves the expected tokens (the prompt's size plus the requested max_tokens) against the team's budget, then corrects the count with the real usage the provider reports. Every call writes a usage record: team, model served, input, cached and output tokens, and cost.

Budgets are not equal shares. Chat, which customers wait on, has 60% of the quota kept for it; the reports job may use at most 40%, and waits in a queue when it has used it. At quiet times nothing waits. At the Monday peak the reports finish a little later, and customers notice nothing.

Limits count
tokens
Chat at peak
answered
Reports at peak
delayed

Try each load. Numbers are illustrative: a small simulation, not a measurement.

Why not…

  • Count only the prompt's tokens up front?

    The output is unknown until it is written; long answers then overshoot the budget. Reserve the maximum, then give back what was not used.

  • Let the reports job use all spare quota with no reserve?

    At the peak there is no spare quota: chat is busy all the time, so a job allowed to fill any gap fills them all and chat gets 429s anyway.

Go deeper:Headroom you reserved but never usedDenial of wallet

Harden it

Fallbacks you can see, a careful cache, and the gateway's own risks

Holds · with visible fallbacks and the gateway treated as critical

Fallbacks: when a deployment returns 429 or fails, the gateway retries on another deployment of the same model and puts the failing one in a short cooldown; only then does it fall back to a different model. It switches only before the first token has been streamed. Every response records the model that served it, the fallback model is tested on the same evaluation set, and an alert fires when it serves: green uptime with worse answers is still an incident.

Caching: an exact-match cache keyed on the model, parameters and whole prompt is safe and cheap for repeated requests, though two identical requests sent at once can both miss. A 'semantic' cache that matches similar prompts can hand one customer the answer to another's question; keep it off routes with personal or account-specific answers.

The gateway's own risks: it is now on every AI call's path, so run several instances and decide in advance whether it fails open or closed if its budget store is unreachable. It sees every prompt and response, so it is in scope for data protection; redact what you can and keep logs under the same rules as the data in them.

Go deeper:Fallback kept uptime, broke qualityLLM gatewayPinned until it retires

Putting it together

The final design

Every model call goes through the gateway. It counts tokens per team, keeps a reserve for customer-facing traffic and queues background work; it retries on another deployment before falling back to another model, and records which model served every response. It runs as several instances, sees every prompt, and is treated as critical.

  1. Gateway instances

    Authenticate the team, apply its budget, route the call, stream the response back.

    Watch out: On every AI call's path: run several, and decide in advance how it behaves if its budget store is down.

  2. Token budgets

    Reserve expected tokens before a call, correct them after, per team and window.

    Watch out: Counting requests instead lets a few huge calls take the whole quota.

  3. Priority and queue

    Keep a reserve for customer-facing teams; queue background jobs when theirs is used.

    Watch out: A queue with no limit just hides the overload until the job misses its deadline.

  4. Routes and fallbacks

    Other deployments first, with cooldowns; another model last, before the first token.

    Watch out: A fallback nobody can see keeps uptime green while answers get worse.

  5. Exact-match cache

    Return stored answers for identical requests.

    Watch out: Simultaneous identical requests both miss; a semantic cache can serve one user another's answer.

  6. Usage ledger

    One record per call: team, model served, tokens, cost.

    Watch out: Without it, the bill arrives with nobody able to say which team spent it.

  7. Redaction and logs

    Remove personal data before it leaves; keep prompts only as long as needed.

    Watch out: The gateway's logs hold every prompt: they are in scope for data protection.

From boxes to an API

API and data model

Teams call the gateway exactly as they would call the provider, so adopting it is a change of base URL and key. Behind it, budgets and a usage ledger decide who may spend what, and routes decide where a request goes and what it falls back to.

Endpoints

  • POST/v1/chat/completionsCalled by Any team's serviceProvider-compatible, streaming. The team is known from its key; the response says which model and deployment served it.
  • GET/v1/usageCalled by Team owners, financeTokens and cost by team, route, model and day, from the usage ledger.
  • PUT/v1/teams/{id}/budgetCalled by Platform teamSets a team's tokens per minute, its priority and whether it may wait in a queue.
  • PUT/v1/routes/{name}Called by Platform teamSets a route's primary deployment, fallbacks, cooldown and caching; changes are versioned so they can be rolled back.

Data

  • teams

    Key: team_id

    team_id, api_key_hash, priority, owner

    Keys identify the team on every call; priority decides who waits when the quota is short.

  • budgets

    Key: (team_id, window)

    team_id, window, token_limit, reserved_share, queue_allowed

    Limits in tokens, the unit the provider counts, with a reserve for customer-facing teams.

  • routes

    Key: route_name

    route_name, primary, fallbacks[], max_fallbacks, cooldown_s, cache_mode

    Where requests go and what they may fall back to, decided centrally instead of in every team's code.

  • usage_events

    Key: event_id

    event_id, team_id, route, model_served, input_tokens, cached_tokens, output_tokens, cost, fallback

    The ledger behind budgets and the bill; the model_served and fallback columns make a silent fallback visible.

From their own write-ups

How real companies did it

  • Uber's GenAI Gateway is a single service in front of external model providers and its own hosted models, mirroring the OpenAI API so teams use familiar clients. It redacts personal data before requests leave and restores it in responses, and its audit logs drive cost attribution and quality evaluation. In July 2024 it served about 30 teams and 16 million queries a month.

    Uber Engineering: Navigating the LLM Landscape: Uber's Innovation with GenAI Gateway (2024) ↗
  • Microsoft's architecture guide for putting a gateway in front of model deployments lists what it adds (load balancing with circuit breaking, rate limiting, token metrics) and what it costs: the gateway can become a single point of failure, it sees raw prompts and responses so it falls inside compliance scope, and it must hold long-lived streaming connections without timing out.

    Microsoft Learn: Access models through a gateway (Azure Architecture Center) ↗
  • Cloudflare's AI Gateway caches responses under a key hashed from the provider, endpoint, model, credentials and the full request body, so only identical requests match. Its documentation warns that the cache is volatile: two identical requests sent at the same moment can both miss.

    Cloudflare docs: AI Gateway caching ↗

In the interview

Follow-up questions

The budget store (say, Redis) behind the gateway is unreachable. Does the gateway fail open or closed?
Decide per team in advance. Customer-facing chat usually fails open with a conservative local limit per gateway instance, because refusing all customers is worse than a brief overspend. Background jobs fail closed and wait. Either way, alert at once: an overspend that nobody notices for a day can be large.
The primary model is down. A customer's answer is half streamed. What does the gateway do?
Nothing clever: it cannot switch models mid-answer without the customer seeing a jump in style or a contradiction. It fails the request cleanly and the client retries, which then goes to the fallback before the first token. Fallback is decided before streaming starts.
Finance wants to know why the bill doubled last month. How does the gateway help?
The usage ledger has a row per call with the team, route, model served and tokens, so the answer is a query: which team, which route, and whether it was more calls, longer prompts, longer answers or a pricier fallback model. Per-team budgets then stop it happening silently again.

Your turn

Check yourself

Why do per-team request limits fail to protect chat from the reports job?

Token budgets that add up to the whole quota still let chat get 429s at the peak. Why?

When should the gateway fall back to a different model?

Sources: Uber Engineering: Navigating the LLM Landscape: Uber's Innovation with GenAI Gateway (2024) ↗ · Microsoft Learn: Access models through a gateway (Azure Architecture Center) ↗ · Cloudflare docs: AI Gateway caching ↗ · LiteLLM docs: Routing, fallbacks and cooldowns ↗ · Report a problem

A typical design, informed by public write-ups, not any one company's exact system. Diagrams are our own. Numbers are illustrative. All wording is original and paraphrases the linked sources.