System Design: ChatGPT (Walnut AI Chat: Save-First Sends, Resumable Streams, Context and Memory, Model Routing, Usage Limits, Tools, Durable Agents)
We design a chat assistant like ChatGPT, which we call Walnut AI Chat. People type a question, and a language model writes the answer word by word.
A GPU is the chip that does a model's math, and running a model to get an answer is inference. A separate system, the Inference platform, runs the models on GPUs. Walnut AI Chat calls it like a model API.
This post designs the product around the model: it saves chats, streams answers, builds what the model reads and keeps each message cheap. It is written for a backend engineer who has never worked with language models. Every machine learning word is explained before it is used.
Every number and the design are our own planning assumptions. Facts about real companies are marked with their year. All times are Pacific time unless marked.
Which question is this? Several interview questions sound alike:
1. Requirements
1.1 What we are building
Lumen Labs is a made-up AI company in San Francisco. Its assistant, Walnut AI Chat, has about 1 billion weekly users on phones and the web.
A language model reads text and writes more text. Lumen Labs runs three of its own models:
- Swift, a small, fast model. It writes titles, answers easy questions and serves free plans when GPUs run short.
- Core, a mid-size model that most messages use. It fits on one B200-class GPU with 180 GB of memory: 70 GB of weights, and the rest for its streams.
- Deep, a large model that can think before it answers, for paid plans only. One copy runs on 32 GPUs.
GPUs are almost the whole cost. A conversation is one chat: a list of messages and answers.
The story runs over one week in November 2026:
- Monday 16 November at 07:42:00. Maya Chen is a nursing student on the free plan. In conversation c_4107 she sends her sixth message: "Explain how insulin lowers blood sugar, in 5 steps."
- Her first word appears at 07:42:00.6.
- At 07:42:04.3 her train enters a tunnel, and the phone loses its connection. The last part of the answer her phone received was event 37.
- At 07:42:09 the app reconnects and asks for everything after event 37. Events 38 to 85 arrive at once, and the rest follows live.
- The 480-token answer ends at 07:42:12.6, with no gap and no repeated words.
- Monday at 10:15. Daniel Okafor is a paralegal at a law firm with workspace ws_3310. In conversation c_5530 he uploads a 60-page lease, file f_2291. He asks about its early termination terms, and the answer cites pages 14 and 41.
- Tuesday 17 November at 09:00. Lucas Bennett owns three bakeries and pays for the Plus plan. He starts agent run ar_7720: "Compare four flour suppliers' bulk prices and email the two best quotes to my manager, Owen Price."
- At 09:18:31 the GPU serving step 23 dies. At 09:18:43 the call continues on another copy of the model, from the next word.
- At 09:33 Lucas approves the email. At 09:33:02, one second after the send, the agent's worker crashes.
- At 09:33:40 another worker finds the email in Lucas's sent mail and does not send it again.
- The run ends at 09:37 after 34 steps, at a cost of about $0.15.
- Tuesday at 20:10. Hannah Lee tells Walnut AI Chat, "Remember that I am vegetarian." It saves memory mem_551.
- Wednesday 18 November at 10:00. Model release deep-v9 launches to paid plans. Sofia Alvarez is the capacity on-call engineer.
- Demand for GPUs reaches 1.6 times a usual busy hour.
- From 10:04 to 12:30, free plans use Swift only, with a notice in the app. Paid messages are all served.
- Wednesday at 18:40. Hannah asks for a quick dinner recipe in a new chat. She gets a vegetarian chickpea curry, because her saved memory is in the model's input.
- Thursday 19 November from 01:00 to 11:20. A company that sells data runs 1,800 fake free accounts. They send about 1.1 million template questions and copy the answers to sell.
- At 11:20 the Abuse service links them by shared devices and timing. At 11:22 they are restricted.
- Thursday at 15:05. One of the three zones of the US West region fails.
- Answers that were running there pause and continue within about 20 s.
- For 4 minutes, free messages beyond the free plans' share of GPUs get "Walnut AI Chat is busy, try again in 20 seconds".
- Friday 20 November at 09:00. Daniel deletes conversation c_5530. It leaves his chat list at once, the live stores by Saturday 09:00, and the backups by Sunday 20 December.
- Friday at 21:05. Hannah deletes memory mem_551. At 21:10 she asks for a recipe again, and the answer does not assume she is vegetarian.
The hard part. A GPU writes each answer slowly and at great cost. Meanwhile phones lose their connection, GPUs die and users press send twice. Three promises must hold:
- Every message is saved once, even when the phone sends it three times.
- Every word reaches the user once and in order, even across a reconnect or a dead GPU.
- GPU time is spent only on work someone can use: it starts only after the limit check. The same words are never written twice.
1.2 Words we use
Words used in only one place are explained there.
The model
| Word | Meaning |
|---|---|
| Token | A piece of text, often a short word or part of a word. About 4.6 bytes of English text make one token. Maya's 9-word question is 12 tokens. |
| Weights | The learned numbers in a model. Core has about 70 billion at 1 byte each: 70 GB. |
| Layer, head | A model works in a stack of steps called layers; Core has 80. Each layer splits its work into parts called heads. |
| Prompt | Everything the model reads for one answer: our instructions, the user's memory, the chat so far and the new message. |
| Context window | The most tokens a model can read and write for one answer: 128,000 for Core. |
| Stateless | The model keeps nothing between answers, so each turn sends the whole chat again. |
| Prefill | The model reads the whole prompt in one pass, then writes one token at a time. So the first word takes longer than the rest. |
| Prompt cache | The platform keeps the numbers the model computed while reading a prompt's start, for a few minutes. These numbers are the KV cache. The next turn's prefill then skips that start. |
| Vector | A list of numbers for a text's meaning. Similar texts get close vectors, and a vector index finds the closest fast. |
| Thinking tokens | Tokens Deep writes to work through a problem before its answer. The user sees only a "Thinking" status. |
| Tool call | A request the model writes, such as "search the web for flour prices". We run it and put the result in the next prompt. |
Serving and speed
| Word | Meaning |
|---|---|
| Replica | One running copy of a model: one GPU for Core, 32 for Deep. |
| GPU-hour | One GPU busy for one hour. We plan at $8.17, as in our inference post. |
| Server-sent events (SSE) | One long HTTP response that streams the answer as numbered events, about 4 tokens every 100 ms. A reconnecting app sends the last event ID it got, such as 37. |
| TTFT | Time to first token: from send to the first word on screen. |
| TPOT | Time per output token: the gap between words after the first. 25 ms a token is 40 tokens a second. |
| p50, p99 | The value that 50% or 99% of measurements stay at or below. |
| Zone, region | A zone is a group of data centers with its own power. A region has 3 zones. We run four regions: US West, US East, Europe and Asia. |
The product
| Word | Meaning |
|---|---|
| Message, answer | The user sends a message; the model writes an answer. The two make a turn. |
| Branch | A second answer to the same message, made by regenerate or by editing the message. The old answer is kept. |
| Compaction | Replacing a long chat's older turns with a short summary in the prompt. The full chat stays stored. |
| Memory | Facts about the user that carry into new chats. A saved memory is one the user asked for. |
| Plan, usage limit | Free, Plus or Team. A plan sets which models a user may use and how many messages in a time window. The free plan allows 10 Core messages every 5 hours and 80 Swift messages an hour. |
| Classifier | A small model trained on messages labeled easy or hard. The router uses one to pick a model. |
| Reserve, settle | Before GPU work, the Usage service holds room in the limit. After the answer, it records the real use and frees the rest. |
| Client message ID | An ID the app makes once per message and sends with every retry, such as m_4107_06. |
| Attempt number | The number of the writer allowed to add events to an answer. When that writer dies, the number goes up. Then the old writer is refused. |
| Agent run | A task in many steps, each one model call plus the tool calls it asks for. |
| Side effect | A change outside Walnut AI Chat that cannot be taken back, such as a sent email. The user's yes before it is an approval. |
IDs in this post
| ID | What it names | Example |
|---|---|---|
| u_1042, u_3317, u_5520 | User IDs | Maya, Daniel, Hannah |
| c_4107, c_5530 | Conversation IDs | Maya's insulin chat, Daniel's lease chat |
| m_4107_06 | A client message ID | Maya's sixth message in c_4107 |
| a_88120 | An answer ID, shortened; real ones are random | The answer to Maya's message |
| f_2291 | A file ID | Daniel's 60-page lease |
| ws_3310 | A workspace ID: one company's Team plan | Daniel's law firm |
| ar_7720 | An agent run ID | Lucas's flour task |
| (ar_7720, 31, 0) | A tool journal key: run, step, call | The email to Owen |
| mem_551 | A saved memory ID | "Hannah is vegetarian" |
| deep-v9 | A model release | Wednesday's launch |
1.3 Every kind of request and change
Sizes are our estimates for the busy hour.
| Kind | What it is | How big or how often | Which part handles it | Who outside takes part |
|---|---|---|---|---|
| Send a message | Save the message, build the prompt, stream the answer | About 100,000 a second | Chat service, Context builder, Inference platform, Answer log | Lumen's inference team |
| Reconnect | Ask for events after the last one received | About 3,000 a second, 3% of answers | Stream gateway, from the Answer log | None |
| Follow on a second device | A laptop opens a running answer | About 1,000 a second | Stream gateway | None |
| Stop | The GPU work stops too | About 2,000 a second | Chat service, Inference platform | None |
| Regenerate or edit | A new answer on a new branch | About 4,000 a second | Chat service | None |
| Upload a file | Store the file, cut it into passages and index them | About 50 million a day, about 1 MB each | File service, Ingest job, Passage index | None |
| Tool call in a chat | Web search, file search or running code | About 5% of messages | Chat service, Search provider, Code sandbox | A web search company |
| Agent run | About 30 steps over about 20 minutes, on a cloud computer: a small virtual machine with a browser | About 5 million runs a day | Agent service, Agent workers, Run store | The user's email and other accounts |
| Approve or deny | The user answers an agent's question | A few per run | Agent service, Notifier | Apple's and Google's push services |
| Memory | Save, use, list or delete a memory | About 2% of messages save one | Memory service | None |
| History | List, search, rename, share, export, delete | About 30,000 a second | Chat service, Chat search index, Purge jobs | A content delivery network, for share pages |
| Feedback | Thumbs up or down on an answer | About 1% of answers | Event log, read by the evaluation team | None |
| Background work | Titles, and summaries of past chats for memory | Each new chat; nightly per active user | Title job, Chat summary job, on spare GPU time | None |
| Model release | A new model version, such as deep-v9 | A few a month | Router settings, Capacity manager | None |
| Abuse | Fake accounts, copying answers, breaking the checks | Always present | Abuse service, Guard service | None |
1.4 Functional requirements
- Chat with every answer streamed as it is written. Resume a dropped stream from the next event, and follow it on a second device.
- Stop, regenerate and edit. Regenerate and edit make a new branch, and the old answer is kept.
- Build each prompt inside the context window, and compact long chats.
- Memory the user controls: saved memories, past-chat summaries and an off switch. Temporary chats stay out of history and memory.
- Files, web search and a code tool, with citations.
- Pick the model by the user's choice, the plan and the question. Fall back to Swift when a limit is reached.
- Agent runs of up to 2 hours, not counting waits for the user's approval before every side effect. A side effect already made is never made again after a crash.
- Usage limits per plan, checked before GPU time is spent. A retried message is counted once.
- History: titles and search. Read-only share links, export and delete.
- Feedback and checks: thumbs up or down, and Guard service checks.
Out of scope: training the models, and the inside of the Inference platform and the Guard service. Company document connectors, voice and image making are out too. So are payments and ads.
1.5 Non-functional requirements
- Fast first word. TTFT p50 at most 0.7 s and p99 at most 2.5 s, with about 1,200 new prompt tokens. The platform takes p50 0.5 s and p99 2 s. Our own steps may use the rest of the time.
- Status. "Searching" or "Thinking" shows within 1 s.
- Steady words. TPOT p50 at most 30 ms, so at least 33 tokens a second. People read about 5 tokens a second.
- Saved once. An accepted message survives the loss of a zone, and a retry makes no second copy. A region loss can lose about the last 5 s of messages, and the app sends those again.
- Resume fast. A reconnect gets the next event within 2 s. An answer broken by a dead GPU continues within 15 s.
- Available. 99.9% of messages get an answer each month.
- Agents keep going. A run continues within 1 minute after its worker dies.
- Private. One user's text never enters another user's prompt or prompt cache. A deleted chat leaves the live stores within 24 hours and the backups within 30 days.
- Cost. At most $0.003 a message on average. That counts GPUs, agents, search and stores.
1.6 The numbers
These are rough whiteboard sums. The daily average spreads a day's total over 86,400 seconds. The busy hour is on US weekday mornings, and its rate is 2.5 times that.
Every message is one model call here, and stage 6 counts tool calls. Every token is priced as a Core token. Swift is cheaper and Deep writes a token for about the same GPU time, but Deep's prompt-reading GPUs are left out.
| Number | How we get it | Result | What it decides |
|---|---|---|---|
| Messages | 1 billion weekly users × 3.5 messages a day each = 3.5 billion a day. ÷ 86,400 = 40,500 a second. × 2.5 = about 100,000 | 100,000 messages a second at the busy hour | 3 writes each: message, answer and chat list row. 300,000 writes a second is too many for one database, so the Conversation store is split by user and conversation |
| Stored text | A 100-token message and a 500-token answer at 4.6 bytes a token, plus fields: about 3 KB. 3.5 billion × 3 KB = 10.5 TB a day. × 365 = 3.8 PB a year | 3.8 PB a year, 11.5 PB with 3 copies, growing every year | After 90 days with no activity a chat moves to object storage, compressed 2.6 times |
| Files | 50 million uploads a day × about 1 MB = 50 TB a day. × 365 | About 18 PB a year | Files go to object storage. The Passage index holds only their text |
| Open streams | 85% of answers: 500 tokens at 40 a second = 12.5 s. 15% think first on Deep, and these are all paid messages: 2,500 tokens = 62.5 s. Average 0.85 × 12.5 + 0.15 × 62.5 = 20 s. × 100,000 | About 2 million open streams | Any stream server can serve any answer, because it reads it from the Answer log |
| Answer log writes | 2 million streams × 10 events a second, one every 100 ms = 20 million appends a second. ÷ 200,000 appends a second per in-memory shard | 20 million appends a second, 100 shards | Words go to an in-memory log split by answer ID. The finished answer is saved to the Conversation store once |
| Prompt tokens | 6,000 tokens a prompt: 2,000 instructions and tool list, 600 memory, 3,300 history, 100 new. × 100,000 = 600 million a second. At an 80% cache hit, 1,200 new tokens a prompt | 600 million read a second, 120 million new | A fixed prompt order, so each turn starts like the last one. With no cache, prefill would need 30,000 GPUs instead of 6,000 |
| GPUs | Output: 100,000 × (500 shown + 15% × 2,000 thinking) = 80 million a second ÷ 2,500 a GPU = 32,000. Prefill: 120 million ÷ 20,000 a GPU = 6,000 | About 38,000 GPUs at the busy hour | Thinking uses 12,000, a third of the fleet, for 15% of messages. Paid plans send a quarter of messages, so 60% of paid messages think. So the router sends easy messages to Swift and caps thinking |
| Cost | Chat: 38,000 × 1.1 spares = 41,800 GPUs × $8.17 × 730 hours = $249M. Agents: 3,300 GPUs = $20M. Cloud computers $5M, web search $15.5M, stores and services $10M. $300M ÷ 106 billion messages a month. GPUs are $249M + $20M = $269M of it | About $0.0028 a message, 90% of it GPUs | Average use is 15,200 of the 41,800 GPUs held. So quiet hours run agents, memory summaries and titles |
Five inputs need a word:
- 2,500 output and 20,000 prefill tokens a second per GPU come from our inference post. There 62 streams × 40 tokens a second = 2,480. Our 2 million streams ÷ 62 = 32,000 GPUs, matching the sum above.
- The 80% cache hit. The 2,000 instruction tokens are the same for every user, so they are always cached. Memory and history are cached only when the user replies within a few minutes. That happens on about 7 turns in 10.
- Agents. 5 million runs a day × 30 steps × 0.71 GPU-seconds a step, on Core. A step writes 300 tokens and reads 3,000 new ones: 300 ÷ 540 + 3,000 ÷ 20,000 = 0.71. That gives about 3,000 GPUs at the busy hour and 3,300 with spares.
- The 540: a 30,000-token agent prompt holds 4.8 GB of KV cache. So a GPU fits only 20 streams at 27 tokens a second, 540 in all.
- Web search. Tool calls are 5% of messages: 175 million a day, all priced as web searches. Agents add 16 searches a run × 5 million runs = 80 million a day.
- 255 million a day × 30.4 days × $2 per 1,000 = $15.5M a month.
- $2 is a volume rate; list prices are about $3 to $5.
- Why hold GPUs all month. Renting only the daily average of 15,200 GPUs would cost $91M. But no provider rents out the other 26,600 GPUs and takes them back every day.
What these numbers tell us: writing tokens is most of the cost. The 2 million open streams are the part most likely to break. So Walnut AI Chat saves each message first, streams from a log, and never pays twice for the same words.