LLM chat UI primitives
May 8, 2026 · 14 min read
Building three LLM chat surfaces for the same company taught me which patterns reused and which did not. Here are the ten I would build again.
Why primitives matter
Each surface had a slightly different ask: an analyst chat, a customer-support assistant, an internal eval tool. The temptation is to fork the first one and customize. The cost shows up six months later when a bug in the SSE handler has been ported three times and fixed twice.
Primitives meant: build once, parametrize, share. The primitives were boring on purpose. The interesting code lives in the surfaces.
1. SSE event contract
Server-Sent Events with a typed event contract. Four event types: status, partial, complete, error. Status is for "thinking" and "searching" affordances. Partial is the text delta. Complete signals end-of-message. Error closes the stream with a code and message the client can branch on.
Why SSE and not WebSockets: SSE is one-way, browsers reconnect automatically, and the protocol maps cleanly to a token stream. WebSockets are bidirectional and pay a complexity cost we did not need.
Why a contract: without one, every surface invented its own JSON shape and the renderer code grew per-surface branches. With one, the renderer is the same everywhere.
2. Streaming renderer
The component that turns a stream of partials into rendered output. It is a state machine: idle, streaming, done, error. It handles auto-scroll on new content with a "pause auto-scroll when the user scrolled up" rule. It renders a soft blinking cursor at the end of the latest message while streaming.
It does not parse markdown. It hands off to a markdown renderer that runs on the accumulated text. Doing it incrementally is tempting and a trap: markdown is order-dependent, and you can render the wrong tree when a closing tag has not arrived.
3. Structured output blocks
LLMs are happy to mix prose and structured data. The renderer treats both as first-class. The model emits typed blocks (table, card, chart, code, text). Each block is a React component. The chat looks conversational; the data inside it stays typed.
The hard part is the contract between model and renderer. We used JSON Schema to describe block types and validated incoming blocks against it. Invalid blocks fall back to a plain text render with a warning, not a crash.
4. Embedded BI
For chart blocks, we embed Apache Superset via signed iframe. The model picks the chart, the renderer drops the iframe, and the user sees the same chart they would see in Superset directly. Compliance had already audited Superset; we did not have to re-fight that battle.
The trick is the signed URL. The frontend never sees the chart URL directly. The backend signs a one-time URL with the user's permissions baked in, and the iframe loads that.
5. Cancel
Every stream has a cancel button. The button calls abort on the fetch's AbortController, which closes the connection client-side. The server sees the close and stops generating. The UI shows the partial response as final, marked "stopped."
Cancel mid-stream is the most important LLM UI feature people skip. Without it, an analyst who realized the model is going down the wrong path has to wait for the full response. With it, they cut their losses and reframe.
6. Retry
A "retry" button on assistant messages that re-runs the request with the same input. Useful when the model produces a bad answer (and the user wants a different sample) or when the SSE stream errored partway through.
Retry preserves the conversation history up to the user message and re-runs from there. The bad assistant message is replaced. If the user wants the old answer back, undo brings it back.
7. Error recovery
Errors arrive over the wire as typed events. The renderer maps each error code to a recovery UI:
- network errors get a "retry" affordance
- rate-limit errors get a "you have hit the daily limit" message with a fallback contact
- model errors get a "the model failed to respond, here is what it sent so far" with a retry
- tool-use errors get an inline message and let the conversation continue
The point is that errors do not crash the conversation. They become ghost messages with their own action affordances.
8. Conversation memory
The chat carries the last N messages back to the server on every request. We capped N at 20 to keep the input size predictable. For longer conversations the server summarizes older turns and sends the summary plus the recent N.
The memory primitive is just an array of role/content pairs. The complexity is in deciding when to summarize, which model to use for the summary, and how to render the "summarized" affordance to the user (we used a collapsible "Earlier in this conversation" block).
9. Follow-up suggestions
After every assistant message, we render three suggested follow-ups. The model emits them as a structured block. They are not magic; they are the model's best guesses at what the user might ask next given the conversation so far.
The hit rate was higher than we expected. Analysts who used follow-ups asked more questions per session, which is the metric product cared about. We did not add this for that reason; we added it because it felt right. The metric is a happy side effect.
10. Export
A conversation has an Export action that downloads the conversation as Markdown. The model emits clean prose; the markdown export is just the rendered tree without the chat chrome.
Export matters because analysts wanted to paste findings into Slack and Notion. Without it they took screenshots. Screenshots are unsearchable. Markdown is.
The eleventh: telemetry
Bake telemetry into the SSE contract from day one. Time to first token, total stream duration, errors per session, cancels per session, retries per session. We added it later and lost a week reconstructing the missing month.
Telemetry is the difference between "I think the model is slow" and "the p95 time to first token rose 40% last Tuesday after the model swap." Build it in early.
What I would do differently
The structured-output renderer was a forking risk. The first surface emitted slightly different block shapes from the second one because we let each team customize the schema. Force a single schema and version it. Versioning meant a block consumer could declare which version it understood and the renderer could fall back gracefully on mismatches.
Build an internal eval harness before launch, not after compliance asks for one. Without an eval harness, every change to the prompt or model is gut-feel. With one, you can A/B prompts on a fixed test set and watch the win rate.
The thing I keep coming back to
LLM chat UI feels like a new field. It is mostly old fields wearing a hat. SSE has been around for a decade. Streaming UI patterns are older than that. The new part is the latency profile (long-running streams with cancellable mid-flight requests) and the structured-output rendering. Everything else is just careful state management with a model in the loop.
If you are building one of these, start with the primitives. The surface is the easy part. The primitives are what survive.