Case studies
Case study: LLM chat product
Designing a ChatGPT-style product, covering the request path, token streaming, conversation context, GPU capacity math, token-based rate limits, tools and untrusted input, and what to do when capacity runs short.
An LLM chat product is a real-time distributed system wrapped around a scarce accelerator pool. Users expect the first words within about a second, memory across turns, file uploads and tools. You pay per token, and at peak you run out of GPUs long before you run out of web servers.
The target for this design: time to first token (TTFT) under about 1 second, conversation history across sessions, file uploads and tools, roughly 10 million daily users, tiered quotas, and graceful degradation when capacity runs short.
A useful order for the design is query, then model, then response. Add the gateway, routing, retrieval and tools, guardrails, caching and orchestration after that. This case study covers the system design and the production skeleton, not how to train the model.
Request path
- API gateway: TLS, a web application firewall, request size limits and edge rate limits.
- Auth and quotas: the session or API key, and the remaining token budget for the current window.
- Orchestrator: loads the conversation, assembles the prompt, calls tools or retrieval, invokes the model, streams the answer back and saves the assistant message.
- Model router: picks a model by tier, latency target, capability and current load.
- Inference fleet: self-hosted GPUs, or vendor APIs behind one gateway.
Streaming needs the proxies to cooperate. Use server-sent events or chunked HTTP, and turn off response buffering in nginx or Envoy. A proxy that buffers the whole completion ruins time to first token.
Concepts in this section
| Step | Concept | What it answers |
|---|---|---|
| 1 | TTFT and token streaming | Why does the first word matter more than the total time? |
| 2 | LLM capacity math | How many GPUs does a daily token load need? |
| 3 | Conversation context assembly | What goes into each prompt, and what does it cost? |
| 4 | LLM rate limits and overload | What do you do when GPUs are full? |
| 5 | Tools and untrusted input | How do tools and uploads stay safe? |
| 6 | Streaming contract and cancellation | What happens when a user stops or a stream breaks? |
Common mistakes
- Measuring only total latency. Users judge the wait for the first token. Track TTFT separately.
- Limiting by requests, not tokens. One long request can cost more than a hundred short ones.
- Buffering streams in proxies. The user sees nothing until the whole answer is ready.
- Resending whole files every turn. Keep the file in storage and attach a reference.
- Letting a free tier starve paying users. Use priority and tier-aware admission.
- No record of which model and prompt produced a reply. Store the prompt version, model ID and router reason on every turn.
Key takeaways
- The path runs gateway, auth, orchestrator, router and fleet, with tokens streamed back.
- Size the fleet from tokens per day and peak tokens per second, then add headroom.
- Store the conversation as turns and context references, and assemble a bounded prompt each time.
- Limit by tokens, shed load by tier, and degrade gracefully instead of failing.
- Treat tool output and uploads as untrusted input, and make every tool call bounded.
Further learning
Chip Huyen's writing on building LLM platforms covers the gateway, routing and evaluation layers in more depth.
Chip Huyen: building LLM applicationsI work to bring AI into production. I write about AI system design.Chip HuyenAndrej Karpathy's talk on how large language models work gives the background for tokens, context and sampling.
For the serving side, see LLM APIs and the inference serving platform.
Related
Working with LLM APIs
Messages, roles, streaming, stop reasons, errors, rate limits and batch APIs. The plumbing of every LLM app.
Case study: designing an LLM serving platform
A 70B-class model and LoRA fleet at 50k RPM under TTFT and TPOT SLOs. Capacity math, pools, gateway and failure drills.
Rate limiting
Why systems cap how much work a client can send, which algorithm counts, where the limit lives and what the client is told. Start here for the rate-limiting map.