How to design an LLM gateway
The first sections are free. The deep dives, level expectations and quiz are part of Pro.
On this page
- The problem
- Clarifying questions
- The requirements menu
- Non-functional requirements
- Estimates
- Core entities
- The API
- The high-level design
- What breaks first
- The deep dives
- Entity fields
- Routing and fallback
- Limits and budgets per team
- Streaming through the gateway
- Cost tracking and audit
- Variants
- What each level is expected to show
- The 30-second answer
- Quiz
- GitHub
- Moveworks
- Decagon
The problem
An LLM gateway is the one service a company's teams call to reach language models: it takes a request in a single shape, picks a provider deployment for it, holds the team to its limits, streams the answer back and records what the call cost. The answer turns on one fact: the company holds a single set of limits at each provider, and all teams draw on it. So admission has to be counted in one shared place rather than per instance, and the router has to read an overloaded provider as a reason to go elsewhere for a while, not as a reason to retry harder.
Clarifying questions
| Ask | Why it matters | A typical answer |
|---|---|---|
How much traffic goes through the gateway, and how does that compare with the limits the providers give us? | It decides whether the gateway's own capacity or the provider's quota is the scarce thing, and that decides where the design effort goes. | About 16 million calls a month, around 25 a second at peak, against a provider limit of 10,000 requests and 10 million input tokens a minute for one model. The gateway instances are lightly loaded; the shared quota is what runs out. |
Do teams name a provider's model directly, or an alias that we map to deployments? | An alias gives the gateway room to route, weight and fall back without changing callers. A hard-coded provider model leaves it almost nothing to decide. | Teams call an alias. The deployments behind it, their weights and the fallback order are set per alias. |
When a provider is down, may we answer with a similar model from another provider, or must the call fail? | A different model answers differently, which some teams can live with and some can't. It fixes how deep the fallback chain may go. | Per team. Some accept a similar model, some would rather see an error. Make it a setting on the route, preferring a deployment of the identical model elsewhere. |
Which limits do we hold a team to: requests, tokens, money, or all three? | Requests are easy to count before the call; tokens and money depend on an answer length you don't know yet, which shapes the admission step. | All three: requests a minute, tokens a minute, and a monthly spend budget. |
Do callers need streamed answers? | Streaming turns a short request into a long open connection, which changes how instances are sized and what a mid-call failure looks like. | Yes. Answers stream token by token over server-sent events. |
May the gateway store prompts and answers, or just the metadata of each call? | Content is the useful part for debugging and audit, and the sensitive part for privacy. The answer decides the shape of the usage pipeline. | Metadata for all calls. Prompts and answers as each team's logging policy allows, redacted where the policy says so. |
Are the models all external providers, or do we also front models served on our own GPUs? | Self-hosted serving brings batching and scheduling with it, which is a different design. It belongs behind the gateway, not in it. | External providers in this round. An internal deployment could sit behind the same adapter interface later. |
Non-functional requirements
| Requirement | What it forces |
|---|---|
Adds little latency: the first token arrives about as fast as when calling the provider directly | Admission is a handful of fast reads and increments against an in-memory store, with no synchronous database write on the request path. The stream is relayed as events arrive instead of being buffered, and the usage record is written after the answer, not before it. |
Keeps the company inside each provider’s own limits, which every team shares | A second layer of counters, one per provider deployment, sized from the provider's published requests-per-minute and tokens-per-minute limits and checked before the per-team ones let a call through. When the provider still answers with an HTTP 429, the router backs off for the time the provider asks for. |
One provider’s outage does not take the gateway down | The router keeps a health record per deployment, stops sending to one that keeps failing, and moves to the next deployment in the alias's chain with a cap on attempts and on total time. Gateway instances stay stateless so an instance tied up by a sick provider's hanging connections can be replaced without losing any state, though its open calls fail and are retried. |
Limits hold across every gateway instance, not per instance | The counters sit in one store that all instances update with atomic increments, and no instance keeps an authoritative count of its own. You also have to say what the gateway does when that store is unreachable: fail open and risk the provider's rate-limit errors, or fail closed and refuse calls. |
Prompts and answers are logged only as each team’s policy allows | The usage event carries metadata for all calls and no content. Prompts and answers go to a separate store behind a per-team flag, redacted before they are written, and the audit log records which key called which alias at what time, without the text. |
Estimates
| Quantity | Working | Result | What it sizes |
|---|---|---|---|
Average calls a second, and the peak against it | 16,000,000 calls a month ÷ (30 days × 86,400 seconds = 2,592,000 seconds) ≈ 6.2 a second; peak 25 ÷ 6.2 ≈ 4 | About 6 calls a second on average, with a peak about 4 times the average | The gateway fleet: a few instances cover this, so the design effort goes to the shared quota and the open streams, not to request throughput. |
Share of the provider's request limit used at peak | 10,000 requests a minute ÷ 60 ≈ 167 a second; 25 ÷ 167 ≈ 15% | Peak traffic uses about 15% of one model's request limit | The headroom that one team's burst can take from everyone else before the provider starts refusing. |
Tokens per request at which the token limits bind before the request limit | 10,000,000 input tokens a minute ÷ 10,000 requests a minute = 1,000 input tokens a request; 2,000,000 output tokens a minute ÷ 10,000 = 200 output tokens a request | Above about 1,000 input tokens or 200 output tokens a request, tokens run out before requests do | A rough sign of which bucket a team's traffic presses on first: input tokens when prompts run long, requests when they are short. Output length, cache reads and the other teams' traffic can shift it either way. |
One team's hundredfold burst against the provider limit | 25 a second ÷ close to 30 teams ≈ 0.8 a second ≈ 50 a minute for an average team; × 100 = 5,000 a minute; at 1,000 input tokens each, 5,000,000 input tokens a minute | One average team at a hundred times its traffic takes about half the request limit and half the input-token limit | The per-team cap: well under half the provider limit, so a single runaway job can't take the rest of the company past it. |
An even split of the provider's limit across teams | 10,000 requests a minute ÷ 30 teams ≈ 333 a minute each; 10,000,000 input tokens a minute ÷ 30 ≈ 333,000 input tokens a minute each | About 333 requests and 333,000 input tokens a minute per team if split evenly | The floor each team keeps for itself. Whenever a team sits below its floor, that share is idle, which is why borrowing matters. |
Retry amplification when half of calls fail | 10,000 calls a minute with half failing = 5,000 failures; one retry each on the same provider = 15,000 attempts a minute; two retries each = 20,000 | One blind retry turns a full provider into one and a half times full; two retries double the load | Why retries need a cap and a cooldown: at a limit of 10,000 a minute the retries themselves come back as rate-limit errors. |
Open streams the fleet holds at once | 10,000 calls a minute ≈ 167 a second; if each stream stays open for an assumed 10 seconds, 167 × 10 ≈ 1,670 open at once; at an assumed 30 seconds, about 5,000 | Between about 1,700 and 5,000 open connections, depending on how long answers take | Gateway instances: by concurrent connections and the memory each holds, not by requests a second. |
Usage log growth | 16,000,000 calls a month × an assumed 500 bytes of metadata per record ≈ 8 GB a month; with prompts and answers at an assumed 4 KB per call, 16,000,000 × 4 KB ≈ 64 GB a month more | About 8 GB a month of metadata; about 64 GB a month more if full content is kept | The usage store: metadata is cheap enough to keep for years; content is what the per-team logging policy decides. |
Core entities
| Entity | What it holds |
|---|---|
Team | Who is calling, what they may call, and how much of it they may use. |
Route | One model alias and the ordered list of deployments that can serve it. |
Provider deployment | One model at one provider in one region, with its own health and its own limits. |
Usage record | One event per finished or cut call, the row a bill and an audit both read. |
Price | What a token costs at one provider for one model, with the date the price took effect. |
The API
| Endpoint | Notes |
|---|---|
| The one shape all teams use; |
| Reads the priced usage records, not the fast counters, so it matches the bill. The latest minutes may be missing until the cost job has run; say so in the response. |
| Writes the team row and pushes the new values to the gateway instances' policy cache. The next request is admitted against the new limits; running calls are not cut. |
| Changes where an alias goes without any team redeploying. Validate that each fallback can take the alias's request shape, and roll a change out by weight first so a bad deployment gets a small share before it gets the whole alias. |
The high-level design
Step 1 of 4
One shape in, one adapter per provider out
Start with the translation job. A team service sends POST /v1/chat with an alias in the model field. The gateway checks the team's key, loads its policy, and hands the request to the adapter for the provider behind that alias. The adapter rewrites the common request into the provider's own API, opens the call, and relays the provider's stream back as server-sent events in the gateway's common event shape, so a caller sees the same events whichever provider answered. Uber built its gateway this way, as a Go service wrapping each vendor's client behind an OpenAI-style interface. Instances are stateless, so any of them can take any request.
What breaks first
The deep dives
Routing and fallback. At peak the main provider begins refusing half its calls with overload errors. What does the gateway do with the next request, how does it decide a deployment is unhealthy, when does it try that deployment again, and what does a caller see when a retried call ran twice?
A concept that helps here: Circuit breakers and cooldowns
A concept that helps here: Retries and dead-letter queuesA concept that helps here: Idempotency keysLimits and budgets per team. One team's batch job sends a hundred times its usual traffic, and the company holds one limit at the provider for all teams. How does the gateway keep the other teams working, and how does it count a team's tokens when the answer's length isn't known until the call ends?
Streaming through the gateway. A user watches an answer appear word by word and the provider's connection drops halfway. Walk the stream from the provider through the gateway to the caller, name what limits how many streams one instance holds, and say what the usage log records for the cut stream and who pays for it.
Cost tracking and audit. Finance wants each team's bill to the cent and security wants each call traceable. How does one call's tokens become a cost on the right team's bill without slowing the call down, what happens to the records when the usage store is down for an hour at peak, and how do your bills get matched against the provider's invoice?
Concepts this guide uses
- Circuit breakers and cooldowns
- Retries and dead-letter queues
- Idempotency keys
- Rate limiting algorithms
- Multi-tenant isolation
- Failing open or failing closed
- Streaming LLM tokens
- WebSockets, server-sent events and polling
- Backpressure and load shedding
- Event sourcing and ledgers
- Reconciliation against an external system
- Queues and logs
Sources
- Uber engineering: Navigating the LLM landscape, Uber's GenAI Gateway
- Claude Platform docs: Rate limits
- Claude Platform docs: API errors
- Claude Platform docs: Streaming messages
- Claude Platform docs: Pricing (prompt caching multipliers)
- LiteLLM docs: Router, load balancing and cooldowns
- LiteLLM docs: Retries, fallbacks and cooldowns
- LiteLLM docs: Fallbacks (provider failover)
- LiteLLM docs: Budgets and rate limits
- Cloudflare docs: AI Gateway features
- Redis documentation: INCR (counter and rate limiter patterns)
- OpenAI developer docs: Rate limits