Lesson 7 of 9 · 24m
Streaming UX That Users Trust
Use token streaming, progressive state, cancellation, and careful finalization to make latency honest without making the interface feel unstable.
Users do not experience model quality and latency separately. A correct answer that arrives after twelve silent seconds feels broken; a mediocre answer that streams immediately feels alive. Streaming is not merely a transport trick. It is a contract with the user about what is happening, what can be interrupted, and when output is authoritative.
Stream progress, not false confidence
Token streaming works because partial language is useful. Partial structured output often is not. Choose the rendering model based on the output type:
chat/prose stream tokens directly
extraction/classification show working state until valid JSON completes
tool workflow stream stage updates, not raw hidden reasoning
document generation stream sections after structural validationA form field that flickers through invalid partial JSON teaches users to distrust the system. Buffer until you can render a stable unit.
Implement cancellation as a first-class feature
Users close tabs, change their minds, and click stop. Abort the upstream request when possible and stop charging the interaction:
const controller = new AbortController()
const response = await fetch("/api/assistant/stream", {
method: "POST",
body: JSON.stringify({message}),
signal: controller.signal,
})
stopButton.onclick = () => controller.abort()Server-side, propagate cancellation to the provider client and worker task. Also decide what cancellation means for tool side effects. If a refund case was already created, do not pretend it vanished because the browser disconnected.
SSE is boring and often enough
Server-sent events are a good default for one-way model output:
event: delta
data: {"text":"Annual plans can "}
event: citation
data: {"chunk_id":"kb_1842:c3","label":"Refund policy"}
event: done
data: {"message_id":"msg_812","finish_reason":"stop"}WebSockets help when you already use them for presence or bidirectional collaboration. Do not add socket infrastructure solely because streaming sounds fancy. SSE traverses proxies well, reconnects with Last-Event-ID, and is easy to inspect.
Separate transport state from answer state
The UI needs at least five distinct states: queued, generating, waiting for tool, cancelable, and finalized. Error and degraded states deserve the same care:
type AssistantState =
| {kind: "queued"}
| {kind: "generating"; startedAt: number}
| {kind: "tool"; toolName: string}
| {kind: "final"; messageId: string}
| {kind: "failed"; retryable: boolean; reason: string}Do not render a partial answer as final after a network disconnect. Mark it incomplete and offer retry. If the backend persisted a completed answer after disconnect, reconnect should recover it rather than regenerate it and double cost.
Citations and actions need final validation
During streaming, citations may refer to retrieval results that are already known. Actions are different. Never enable a destructive button merely because partial text implies one. Enable it only after the final structured payload validates:
if (finalPayload.action?.type === "create_refund_case" && validated) {
renderConfirmation(finalPayload.action)
}The user experience should make the authority transition obvious: draft language first, validated action second.
Field notes
- Time-to-first-token is useful, but time-to-useful-answer is the metric users feel.
- Long silence during tool calls needs visible stage text, not a blinking cursor of doom.
- Reconnect logic must dedupe events; duplicated deltas are worse than a gap.
- Do not stream hidden chain-of-thought. Stream user-appropriate progress or concise rationales by design.
- Mobile networks interrupt streams constantly; test throttled connections, background tabs, and app switching.
Was this lesson helpful?