
I’ve used a lot of AI features over the last year that felt broken. Most of the time, the model was fine. The UI around it wasn’t.
A button that does nothing for six seconds. A spinner that never ends because the stream silently died. A half-written answer that disappears when you click away. A “Regenerate” that wipes out the answer you actually liked.
None of these are AI problems. They’re frontend problems, and they’re very fixable. Here are the patterns I reach for every time I build an AI feature.
Start with a state machine
Normal API calls have three states: loading, success, error. AI requests have more, and if you model them as a pile of booleans (isLoading, isStreaming, hasError, wasStopped), you will end up in impossible combinations like “loading and errored at the same time”.

A discriminated union makes the impossible states unrepresentable:
type AiRequest =
| { status: 'idle' }
| { status: 'submitting'; startedAt: number }
| { status: 'streaming'; text: string }
| { status: 'complete'; text: string }
| { status: 'stopped'; text: string }
| { status: 'error'; text: string; message: string; retryable: boolean };
type Action =
| { type: 'submit' }
| { type: 'token'; value: string }
| { type: 'done' }
| { type: 'stop' }
| { type: 'fail'; message: string; retryable: boolean };
function reducer(state: AiRequest, action: Action): AiRequest {
switch (action.type) {
case 'submit':
return { status: 'submitting', startedAt: Date.now() };
case 'token':
return {
status: 'streaming',
text: (state.status === 'streaming' ? state.text : '') + action.value,
};
case 'done':
return state.status === 'streaming' ? { status: 'complete', text: state.text } : state;
case 'stop':
return { status: 'stopped', text: 'text' in state ? state.text : '' };
case 'fail':
return {
status: 'error',
text: 'text' in state ? state.text : '',
message: action.message,
retryable: action.retryable,
};
}
}
Now every state maps to exactly one UI. You can look at the union and ask “what does the user see here?” for each case. That’s the whole exercise.
Pattern 1: Show something in under 300ms
LLMs are slow compared to normal APIs. You can’t make the model faster, but you can make the wait feel shorter.
- Optimistically render the user’s message immediately. Don’t wait for the server to acknowledge it.
- Show a typing indicator right away, in the spot where the answer will appear. Not a full-page spinner.
- Stream the response. This is the biggest win by far. I covered the implementation in how to stream LLM responses in the browser.
- For long-running tasks (like “analyze this document”), show real progress steps: “Reading document… Finding key points… Writing summary”. Only if they’re real, though. Fake progress gets noticed.
Pattern 2: Always give users a Stop button
The moment generation starts, the Send button should turn into a Stop button. Users stop generations all the time: the answer is going in the wrong direction, or they already got what they needed from the first paragraph.
Two things to get right:
- Stop must actually cancel the request with an
AbortController, and the server should abort the upstream model call. Otherwise you’re paying for tokens nobody reads. - Keep what was generated. A stopped answer is not a failed answer.
Pattern 3: Never throw away partial output
This is my strongest opinion in this whole post. If a stream fails halfway through, show the partial text with a clear message and a Retry button. Don’t replace it with an error toast.
function AssistantMessage({ request, onRetry }) {
switch (request.status) {
case 'submitting':
return <TypingIndicator />;
case 'streaming':
case 'complete':
case 'stopped':
return (
<>
<Markdown text={request.text} />
{request.status === 'stopped' && <Note>Stopped. <button onClick={onRetry}>Regenerate</button></Note>}
</>
);
case 'error':
return (
<>
{request.text && <Markdown text={request.text} />}
<ErrorNote>
{request.message}
{request.retryable && <button onClick={onRetry}>Retry</button>}
</ErrorNote>
</>
);
default:
return null;
}
}
Also, write error messages a human can act on. “Something went wrong” is useless. “The response was cut off because the connection dropped” or “You’ve hit today’s limit, it resets at midnight” tells people what to do next.
Pattern 4: Regenerate shouldn’t destroy the old answer
Sometimes the first answer was better. If Regenerate overwrites it, users learn not to press Regenerate.
Keep previous versions and let users flip between them (the little “< 2 / 3 >” control). Under the hood, this means the conversation is a tree, not a list. Each regenerate creates a sibling. I explain the data model in how I’d design a ChatGPT-style app.
Pattern 5: Design the empty state
A blank text box with “Ask anything” is intimidating. People don’t know what the feature is good at.
- Show 3-4 suggested prompts that are specific to the context (“Summarize this PR”, not “Ask me anything!”).
- Make suggestions clickable and pre-fill the input instead of sending immediately, so users can tweak them.
- If the feature has limits (file size, languages, what it can’t do), say so here, before the user hits them.
Pattern 6: Make AI actions reversible
As AI features move from “write text” to “do things” (rename files, edit a document, send an email), the stakes go up.
| Action type | Example | UX pattern |
|---|---|---|
| Suggestion | Rewrite this paragraph | Show a diff, accept or reject |
| Reversible change | Reformat a document | Apply it, show a prominent Undo |
| Irreversible or external | Send an email, delete files, make a payment | Explicit confirmation showing exactly what will happen |
The confirmation step isn’t just UX. It’s a security boundary too, because models can be manipulated by content they read. More on that in securing AI features on the frontend.
Pattern 7: Show your sources and your uncertainty
- If the answer is based on documents, link to them inline. Users trust what they can check.
- If the model is unsure or the retrieval found nothing, say that instead of letting it guess confidently.
- Add lightweight feedback (thumbs up/down with an optional reason). It’s the cheapest eval data you’ll ever get.
Pattern 8: Don’t let the stream hurt the rest of the page
A streaming response re-rendering 40 times a second can make the input box lag. That’s the part users touch, so it’s the part that has to feel instant.
- Batch token updates to once per animation frame.
- Isolate the streaming message in its own component so the rest of the list doesn’t re-render.
- Only auto-scroll if the user is already at the bottom.
I go deep on measuring this in debugging INP.
A quick checklist
- Does every state in my union have a designed UI?
- Does the user see feedback within ~300ms of pressing send?
- Can they stop it? Does Stop actually cancel server-side?
- Is partial output kept on stop and on error?
- Does Regenerate preserve the previous answer?
- Are destructive AI actions confirmed or undoable?
- Is the input still responsive while streaming?
This also makes for a great interview answer. If you get “build an AI chat” in an app design round, walking through these states before you write a line of code shows the interviewer you’ve shipped real products. You can practice with the Build ChatGPT question on FrontendLead.
And that’s it, folks. The model is maybe half of the experience. The other half is the frontend, and that half is completely in your control.
