System design with LLMs
9 lessons10 to 15 min eachOpen source

System design with an LLM gateway

The architecture concepts that show up when a product runs LLMs in production, explained with the code and the numbers of a project that actually runs: the llm-gateway.

What an LLM gateway is

It is a server that sits between your apps and the model APIs (Anthropic, OpenAI and others). The apps call the gateway instead of the provider, and the gateway solves in one place what every app with an LLM ends up needing.

Without a gateway, each service calls the model API directly, with the real key:

in each service, without a gateway
const client = new Anthropic({ apiKey: process.env.ANTHROPIC_API_KEY });

With the gateway, the service only changes the address and the key. The rest of the code stays the same, because the gateway speaks the same API format:

in each service, with a gateway
const client = new Anthropic({ baseURL: 'http://localhost:3000', apiKey: 'gw_acme_123' });

The problems it solves

The provider goes down
The model API returns overload errors for 20 minutes, and your product goes down with it. The gateway switches providers on its own.
One customer uses up everyone's quota
A customer fires requests in a loop and burns the tokens that belonged to everyone else. The gateway limits each customer separately.
Nobody knows what it costs
Someone asks "how much did customer X cost this month?" and there is no way to answer. The gateway records the tokens and the cost of every request.

The path of a request

Every request goes through these stops, in this order. Each stop answers one question and can end the request right there.

  1. AuthenticationWho is the customer?Rejects with 401lesson 1
  2. CacheHas this question been answered already?Answers right away, without calling the providerlesson 8
  3. Rate limitDoes the customer still have tokens left?Rejects with 429lesson 3
  4. Load balancerWhich provider should get it?Picks among the healthy oneslesson 4
  5. Circuit breakerIs this provider failing?Skips the providerlesson 6
  6. QueueDoes the provider have a free slot now?Waits, or rejects with 503lesson 7
  7. Call and streamingPasses the response along as it arrivesIf it fails, tries another providerlesson 2

The 9 lessons

  1. 1Reverse proxy10 min
  2. 2Streaming15 min
  3. 3Rate limiting by tokens15 min
  4. 4Load balancing10 min
  5. 5Failover, retries and backoff15 min
  6. 6Circuit breaker10 min
  7. 7Queues and load shedding10 min
  8. 8Response caching10 min
  9. 9Metrics and load testing15 min
  10. ?Interview questions

Terms used in the guide

TermWhat it is
TenantA customer of the gateway: an app, a team or a company. Each tenant has its own key and its own quota.
ProviderThe API that runs the model. In the project, there are three fake providers that imitate the Anthropic API.
TokenThe piece of text the model reads or writes, and the unit it is billed in. A token is about 4 characters of English text; other languages usually get a bit fewer.
SSEServer-Sent Events: the format the API uses to send the response in pieces, over the same HTTP connection.
TTFTTime to first token: how long until the first piece of the response arrives. It is what the user feels as "it started answering".
p50 and p99The latency that 50% and 99% of requests stayed under. The p99 shows the worst case that still happens often.

How each lesson works

Every lesson has the same parts, always in the same order:

  1. In one sentence: what the concept is.
  2. When you need it: real situations where it is the right choice.
  3. Why it is the usual choice: what it gains over the alternatives.
  4. How it works: the flow, step by step, and the piece of the project's code that does it.
  5. What the experiment showed: the mechanism on and off, with measured numbers.
  6. Interview question: a question on the topic in the format of a system design interview, with a short answer.

A lesson counts as read when you scroll its page to the end. Each lesson read lights up a block in the top bar, and the progress is saved in this browser.

About the numbers

The numbers in the lessons come from the repository's pnpm bench command. It starts three fake providers, with a fixed latency (100 ms in most tests), and the gateway, all on the same machine. The numbers show the effect of each mechanism, not the performance against a real API. You can run it and check:

in the terminal
git clone https://github.com/nicomsz/llm-gateway
cd llm-gateway
pnpm install
pnpm test     # 94 tests
pnpm bench    # the experiments, about 3 minutes