How to design a ChatGPT-style chat (frontend)
The first sections are free. The deep dives, level expectations and quiz are part of Pro.
On this page
- The problem
- Clarifying questions
- The requirements menu
- Non-functional requirements
- Estimates
- Core entities
- The API
- The high-level design
- What breaks first
- The deep dives
- Entity fields
- A reply that arrives word by word
- A reply that stops halfway
- A very long conversation
- Where conversations are kept
- Variants
- What each level is expected to show
- The 30-second answer
- Quiz
The problem
A chat screen where someone sends a message and the assistant's answer arrives word by word for about 20 seconds, with a stop button, a retry, and a list of past conversations. The answer turns on treating a reply as a long-running stream with a definite end state: text is appended as it arrives, formatting and sanitizing run again on what has arrived so far as text lands, and the message box keeps answering keystrokes the whole time.
Clarifying questions
| Ask | Why it matters | A typical answer |
|---|---|---|
Does the reply have to appear word by word, or can it show whole once it's ready? | Word by word is most of the question: a stream to read, a reply that's half there, formatting that's half finished. A whole reply is one request and one draw. | Word by word. First words within 1 to 2 seconds, and a typical reply of about 500 tokens takes around 20 seconds to arrive, so a spinner for the whole time isn't acceptable. |
Does the server already stream, and in what shape? Or do I pick how the reply is delivered? | If the server streams typed events over HTTP, the client's job is to read them well. If the transport is open, choosing it is part of the answer. | You pick. The server can answer a |
What's in a reply: plain text, or formatting like lists and code blocks? | Plain text can be appended as it comes. Formatting means parsing text that's still growing, and drawing markup from a source we don't trust. | Markdown-style formatting with lists and code blocks. The reply is model output, so it's untrusted until it has been sanitized. |
Do conversations need to follow the person across devices, or is one browser enough? | Across devices means the server is the home for conversations and the browser keeps little. One browser opens the door to storing everything locally, with its size limit. | Across devices: someone closes the tab and opens the chat the next day on another machine, expecting all their conversations in the list. Sign-in itself is out of scope. |
When someone presses stop, does the server need to stop too, or is it enough to stop showing text? | It decides whether stop is a local abort or a request to the server as well, and what the saved conversation holds for that reply. | Both. Stop should end generation on the server, and the partial text should remain in the conversation as a stopped reply that can be retried. |
Can someone send a second message before the current reply has finished? | Two replies streaming into one conversation at once complicates the state and the view. Blocking or canceling is simpler, and the interviewer may want you to choose. | A single in-flight reply per conversation. Say whether a second send gets blocked or stops the first one, and why. |
Who uses it, and with what? | Keyboard use and screen readers change the markup and how a finished reply is announced, and phones have less processing headroom for a growing page. | Keyboard and mouse, desktop and phone browsers. A screen reader user should learn that a reply has arrived without hearing each word as it lands. |
Non-functional requirements
| Requirement | What it forces |
|---|---|
The first words of a reply show within 1 to 2 seconds | The response must be read as it arrives, so the request is a stream, not a body that resolves at the end. The reply view appears in a waiting state the moment the message is sent, and the first text event replaces it. |
Typing and scrolling answer within 200 milliseconds while a reply streams | Work per arriving piece must stay small and flat as the reply grows: append rather than re-parse, and coalesce the paints. Interaction to Next Paint measures the time from the keystroke to the next frame, so anything long on the main thread shows up in that number. |
Model output is treated as untrusted text before it is drawn | Formatted replies pass through a sanitizer before they become markup, and the sanitizer runs on the accumulated text, since a harmful tag may be split across two pieces. Plain text pieces go in as text nodes, not as HTML. |
Usable with a keyboard, with new replies announced to screen readers | Stop and retry are real buttons reachable by Tab, the message box keeps focus during streaming, and a polite live region announces that a reply has finished rather than reading out each piece as it lands. |
Estimates
| Quantity | Working | Result | What it sizes |
|---|---|---|---|
Pieces of text per second during a reply | 500 tokens / 20 seconds = 25 tokens a second | About 25 a second | If each token became a separate screen update, the page would be redrawing about 25 times a second for 20 seconds. That is the rate the batching has to absorb. |
Screen updates one reply causes | Unbatched: one per token = 500 updates. Batched to at most one update per 200 ms (the keystroke budget): 20 seconds / 0.2 seconds = 100 updates. Batched per animation frame, the count depends on the display's frame rate and on how the pieces cluster, so it isn't worked out here. | 500 without batching, about 100 or fewer with it | The reply view's update count. The number itself matters less than what each update costs: an append is cheap, a full re-parse of the reply grows with its length. |
Messages that fit in the browser's key-value storage | 5 MB per site divided by 1 KB per message = 5,000 messages | About 5,000 messages | Why localStorage is for drafts and settings, not history. A heavy user with many code-filled replies can reach that limit, and the write that goes over it throws. |
Wait at a spinner without streaming | A reply takes about 20 seconds to generate, and the System Design Newsletter puts the unstreamed wait at 10 to 30 seconds | 10 to 30 seconds of nothing on screen | The gap between the first-words target of 1 to 2 seconds and what a whole-body response delivers. It is the reason the request is a stream. |
Core entities
| Entity | What it holds |
|---|---|
Conversation | One thread in the list, owned by the server and shown by title. |
Message | One turn in a conversation, from the person or the assistant. |
Reply in progress | Client-side state for the one assistant message currently streaming in a conversation. |
Conversation list on the client | What the sidebar knows: a page of conversations and where the next page starts. |
The API
| Endpoint | Notes |
|---|---|
| One request per sent message; the response body is an event stream rather than a single JSON body. The client reads it with |
| Called together with aborting the fetch in the browser, so the server stops generating and the partial text is saved as stopped. The client treats a non-OK answer as a stop that still happened locally, and lets the next load of the conversation show the server's view. |
| Asks for a fresh reply to the same user message. It replaces the stopped or failed message in the view, and its stream is read exactly like the first one. |
| Pages the sidebar newest first. The client keeps |
| Loads older messages when the person scrolls up, keyed by the oldest message already on screen rather than by page number, so a new message at the bottom doesn't shift the pages. |
The high-level design
Step 1 of 5
Message box, server, reply view
Start with the shape that works for any form: the message box sends a POST, the server answers with the whole reply, and the view draws it. It shows the data flow in one line, and it fails the first requirement at once: for a reply that takes about 20 seconds to generate, the person watches a spinner for the whole time, and the first words are nowhere near the 1 to 2 second target.
What breaks first
The deep dives
A reply that arrives word by word. Someone sends a message and the answer streams in over about 20 seconds, with a list and a code block in it. How does each piece of text get from the server onto the screen, and what in your view gets slow first?
A reply that stops halfway. Halfway through a reply one person presses stop, and in another chat the network drops and the text stops arriving. What does each of them see, what gets saved, and what happens when they press retry?
A very long conversation. One conversation has been running for weeks, full of long replies with code, and now typing in the message box lags while a new reply streams. What does the page do with all those messages, and what did you give up to keep it fast?
Where conversations are kept. Someone closes the tab in the middle of the week and opens the chat the next day on a laptop, expecting all their past conversations to be in the list. Where do the conversations live, which copy is the truth, and what happens to a reply that was still streaming at the moment the tab closed?
Concepts this guide uses
- Streaming responses in the UI
- Batching UI updates
- WebSockets, server-sent events and polling
- Partial failure in the UI
- Debounce and request cancellation
- Reconnect and resume
- List virtualization
- Pagination and infinite scroll
- Core Web Vitals
- Where state lives
- Client caching and normalized stores
- Offline-first storage
Sources
- Chrome for Developers: Best practices to render streamed LLM responses
- MDN: Using server-sent events
- MDN: EventSource
- MDN: Using readable streams
- MDN: AbortController
- MDN: Storage quotas and eviction criteria
- MDN: IndexedDB API
- MDN: ARIA live regions
- web.dev: Interaction to Next Paint (INP)
- web.dev: Web Vitals
- AI SDK: Chatbot resume streams
- The System Design Newsletter: ChatGPT system design
- sdeoffer: AI chat assistant system design