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
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
"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
- How do you make TTFT predictable?
- A user regenerates an answer. What changes?
- How would you run in two regions?
Model answers to each of these are in the book.