System design case study C23 · LLM · GPUs

Design an AI Chat Assistant

A chat app where the other side is a language model. Streaming tokens, GPU capacity, conversation memory, document uploads and safety: a design question that barely existed a few years ago and now comes up constantly.

What you'll be able to explain
  • Design a streaming chat API on top of LLM inference
  • Store conversations and build context windows efficiently
  • Plan GPU capacity, batching and queueing for inference
  • Add file uploads with retrieval, and tools with safe execution
  • Control cost and abuse with quotas, routing and caching

Scope

Functional

  • Users chat with an assistant; responses stream token by token.
  • Conversation history persists; users can list, rename and delete chats.
  • Users can upload documents (PDFs) and ask questions about them.
  • The assistant can use a few tools (web search, a calculator, the user's calendar) with consent.
  • Free and paid tiers with different limits and models.

Non-functional

  • Time to first token (TTFT) p95 < 1.5 s on paid tier; streaming ≥ 30 tokens/s.
  • High availability of chat; graceful degradation to a smaller model under load.
  • Strong privacy: per-user isolation of chats and documents; deletions are honoured.

Out of scope: model training, voice, image generation.

Size

  • 20M daily active users; on average 15 messages/day → 300M messages/day ≈ 3,500/s, peak ~10,000/s.
  • Average request: 3,000 input tokens (history + context) and 400 output tokens.
  • Output tokens at peak: 10,000/s × 400 ≈ 4M tokens/s being generated across the fleet.
  • If one GPU server sustains ~2,500 output tokens/s for our model with batching, we need ~1,600 GPU servers at peak for the main model, before headroom. GPUs are the dominant cost by far.
  • Storage: 300M messages/day × ~2 KB ≈ 600 GB/day of chat text; uploads add far more in object storage.

Decision: everything revolves around using GPUs efficiently: batching, routing easy requests to smaller models, caching shared prefixes and keeping prompts lean.

Architecture

CLIENTWeb / mobileNETWORKGatewayauth, quotasSERVICEChat serviceorchestrates a turnDATABASEConversation storemessagesSERVICEModel routerQUEUE / LOGInference queueper model, priorityWORKERGPU fleetbatched inferenceDATABASERetrievalvector + keywordSERVICETool runnersandboxedOKSafety filters
AI chat assistant: a streaming API in front of an inference fleet, with retrieval and tools on the side.

APIs

POST /v1/chats                                → {chat_id}
POST /v1/chats/{id}/messages  {content, attachments[]}
     → text/event-stream:  event: token {"t":"Hel"} … event: done {"usage":{…}}
GET  /v1/chats?cursor=                         → list
GET  /v1/chats/{id}/messages?before=           → history page
POST /v1/files  (multipart)                    → {file_id, status: processing}
DELETE /v1/chats/{id}

Streaming uses Server-Sent Events over HTTP: simple, proxy-friendly and one-directional, which is all a response stream needs (Chapter 7 of the book). A "stop generating" button sends a separate cancel request.

The 30-second summary

Say it in the interview

"A chat turn goes gateway (auth and quotas) → chat service, which stores the message, builds a context from the system prompt, a rolling summary, recent turns and any retrieved file chunks, and sends it through a model router into per-model priority queues in front of a continuously batched GPU fleet. Tokens stream back over SSE and are saved when the turn completes. GPUs are the cost centre, so we route easy requests to small models, cache shared prefixes, cap output length per tier, and degrade free-tier traffic first under pressure. Tools run sandboxed with the user's permissions, and anything that changes state needs confirmation."

Follow-up questions the interviewer may ask

  1. How do you make TTFT predictable?
  2. A user regenerates an answer. What changes?
  3. How would you run in two regions?

Model answers to each of these are in the book.