Blog5 min read

Frontend System Design: How I’d Design a ChatGPT-Style Chat App in an Interview

Frontend System Design: How I'd Design a ChatGPT-Style Chat App in an Interview

For years, the go-to frontend system design questions were “design a news feed”, “design an e-commerce site”, and “design a photo carousel”. Those are still around. But I’ve noticed “design ChatGPT” or “design an AI chat interface” quickly joining that list.

It makes sense. It’s a product everyone has used, it touches streaming, state management, performance, and accessibility, and there are a lot of ways to give a shallow answer.

Here’s how I’d walk through it in a 45-minute round. I’ll follow the same structure I recommend in my frontend system design interview guide.

How I’d spend the 45 minutes

Time Section Goal
~5 min Requirements Agree on scope before drawing anything
~10 min Architecture Components, state, data flow
~8 min Data model + API What the client stores, what it asks for
~15 min Deep dives Streaming, long lists, edge cases
~7 min Performance, accessibility, wrap-up Show breadth and trade-offs

Interviewers almost always want to go deep somewhere. Leave room for it. The worst thing you can do is spend 25 minutes on requirements and boxes.

1. Requirements

I’d start by saying what I think the core is and asking the interviewer to confirm:

Functional

  • Send a message and see the AI response stream in.
  • See a list of past conversations, open one, and continue it.
  • Stop a response mid-generation. Regenerate a response.
  • Render markdown and code blocks with syntax highlighting and copy buttons.

Non-functional

  • Time to first token should feel instant (show something within a few hundred milliseconds).
  • Smooth with very long conversations (hundreds of messages, long code blocks).
  • Works on mobile web, keyboard accessible, screen-reader friendly.
  • Resilient to flaky networks: no lost drafts, clear errors.

Out of scope unless asked: file uploads, voice, sharing, multiple models, real-time collaboration.

Explicitly calling out what’s out of scope is a senior move. It shows you know those features exist, and that you’re choosing to focus.

2. Architecture

Client architecture for a ChatGPT-style app: view, state, network and storage layers

I like splitting it into three layers and saying out loud that each layer only talks to its neighbor.

  • View: Sidebar, MessageList, Composer. Mostly presentational. They read from the store and dispatch actions.
  • State: a normalized conversation store, a stream controller that owns the active request, and a small UI state.
  • Network and storage: a typed API client, SSE over fetch for streaming, IndexedDB for drafts and a recent-conversations cache, and a BroadcastChannel so two open tabs don’t drift apart.

I’d also mention rendering strategy. The app shell can be server-rendered for a fast first paint, but the chat itself is highly interactive and user-specific, so it’s mostly client-rendered. There’s no SEO requirement for private conversations.

3. Data model

This is where I see a lot of candidates give a flat array of messages and move on. That works until the interviewer asks, “What happens when the user edits an earlier message or regenerates a response?”

The answer is that a conversation is a tree, not a list. Every edit or regenerate creates a sibling branch. The UI shows one path through the tree.

type Role = 'user' | 'assistant';

interface Message {
  id: string;
  conversationId: string;
  parentId: string | null; // forms the tree
  role: Role;
  content: string;
  status: 'complete' | 'streaming' | 'stopped' | 'error';
  createdAt: number;
}

interface Conversation {
  id: string;
  title: string;
  updatedAt: number;
  currentLeafId: string | null; // which branch is shown
}

interface ChatState {
  conversations: Record<string, Conversation>;
  messages: Record<string, Message>;
  childrenByParent: Record<string, string[]>; // for "< 2/3 >" branch switching
}

To render, you walk from currentLeafId up through parentId to the root and reverse it. That’s O(depth), which is fine.

4. API design

GET  /conversations?cursor=abc&limit=20      // sidebar, cursor pagination
GET  /conversations/:id/messages?before=xyz  // load older messages on scroll up
POST /conversations/:id/messages             // body: { parentId, content }
     -> 200 text/event-stream
        data: {"type":"meta","messageId":"m_42"}
        data: {"type":"token","value":"Sure"}
        data: {"type":"token","value":", here"}
        data: {"type":"done"}
POST /messages/:id/stop                      // optional, if abort isn't enough

Things worth saying:

  • Cursor pagination, not offsets. New conversations get added at the top constantly, which shifts offsets.
  • The stream starts with a meta event containing the server-assigned message ID, so the client can swap its temporary ID for the real one.
  • Why SSE over fetch instead of WebSockets? It’s request/response shaped, works with normal auth and HTTP infrastructure, and we don’t need server-initiated messages. I cover this trade-off in detail in how to stream LLM responses in the browser.

5. Deep dives

Streaming and state

When the user hits send:

  1. Optimistically insert the user message with a temporary ID, and an empty assistant message with status: 'streaming'.
  2. Open the stream with an AbortController. Keep one active stream per conversation.
  3. Batch tokens per animation frame and append them to the assistant message.
  4. On done, mark it complete. On error or stop, keep the partial content and set the status accordingly.

A good follow-up the interviewer might ask: “What if the user switches conversations mid-stream?” I’d let it keep streaming into the store in the background (it’s cheap) and show a small indicator in the sidebar, rather than aborting and losing the answer.

Very long conversations

Hundreds of messages with code blocks means thousands of DOM nodes. I’d virtualize the message list, but call out that it’s harder than a normal list:

  • Heights are variable and unknown until rendered, and the last message’s height changes on every token. You need measurement with a ResizeObserver. I wrote about this in virtualizing variable-height lists.
  • The list is anchored to the bottom, and loading older messages at the top must not make the content jump. You preserve scroll position by adjusting scrollTop by the height that was added.
  • Browser find-in-page (Cmd+F) breaks with virtualization. That’s a real trade-off. A middle ground is content-visibility: auto on each message, which skips rendering work for off-screen messages but keeps them in the DOM.

Markdown and code blocks

  • Parse completed blocks once and memoize them. Only re-parse the block that’s still streaming.
  • Syntax highlighting is expensive. Highlight after a code block closes, or move it to a Web Worker.
  • Treat model output as untrusted. Sanitize, don’t allow raw HTML, and be careful with images and links. See securing AI features on the frontend.

Network resilience

  • Save drafts to IndexedDB on change (debounced) so a refresh doesn’t eat a long prompt.
  • If the stream drops, show the partial answer with a “Response interrupted, retry?” action rather than silently failing.
  • Listen for online/offline events and disable send while offline, with a clear message.

6. Accessibility

This is the section most candidates skip, and it’s an easy way to stand out.

  • The message list is a role="log" region. But don’t announce every token to screen readers; that’s unbearable. Announce when a response completes, or announce in sentence-sized chunks.
  • The Stop button must be keyboard reachable while streaming, and focus should not jump around as content grows.
  • Enter sends, Shift+Enter adds a new line. Make sure IME composition (Japanese, Chinese input) doesn’t trigger a send. Check event.isComposing.
  • Code blocks need a labeled copy button and should be horizontally scrollable, not overflowing the page.

What separates a senior answer

Area Typical answer Senior answer
Data model Array of messages Tree with parentId for edits and regenerates
Transport “WebSockets” SSE over fetch, with a reason and trade-offs
Long chats “Virtualize it” Bottom-anchored, variable-height, scroll restoration, find-in-page trade-off
Failure Error toast Partial output kept, retry, drafts persisted
Accessibility Not mentioned Live region strategy, IME handling, focus management

If you want to practice, FrontendLead has a full write-up for Build ChatGPT (Frontend), a more general chat app design, and a hands-on Build ChatGPT coding question if you want to actually build it.

And that’s it, folks. If you can explain why a conversation is a tree, why you’d pick SSE, and how you’d keep a 500-message chat smooth, you’re already ahead of most candidates I’ve seen.