FullStack AI Hub
Studio
lesson depth
Mastery
not started · 0%

Skeletons and Spinners for AI Response Latency

Choose loading feedback from the result's shape and measured wait, while reserving layout space and exposing meaningful status during generation.

Freshness: current•12 min read•AI User Experience and Human-AI Interaction
Full Technical Reference Document

Mental model

Loading feedback is a rendering decision, not a way to make inference finish sooner. Use a skeleton when the result's shape is predictable and you can reserve its final geometry; use an indeterminate spinner when the result shape is unknown or the wait is too short to justify a placeholder. Replace either as soon as meaningful output can render.

An AI response has distinct phases: the request is accepted, the first useful content arrives, more parts stream, and the result completes or fails. A single decorative loader cannot explain all of these states. Choose feedback from the shape of the result and measured response timing, then keep the transition to streamed content stable. A skeleton is useful when it occupies the same layout as the result; a generic gray block that later expands can create the shift it was meant to prevent.

Request enters pending state
Reserve known result geometry or show spinner
First useful stream part arrives
Replace placeholder with real content
Mark completion or failure accessibly
Conceptual teaching model synthesized from:web-vitals-cls-guide,vercel-ai-sdk-ui-stream-protocol,w3c-wcag22

Theory

The browser's layout shift score for one shift is the impact fraction multiplied by the distance fraction:

S=I×DS = I \times D

Both II and DD are dimensionless fractions. The impact fraction is the viewport area covered by the union of the affected elements' old and new positions. The distance fraction is the largest movement distance divided by the viewport's largest dimension. This score says how much content moved on screen; it does not measure inference time or whether the wait felt acceptable.

Suppose an 800 px wide, 200 px tall answer card moves down by 80 px in a 1000 px by 800 px viewport. Its old and new rectangles overlap by 120 px, so their union covers approximately 800 × 280 = 224,000 square pixels. The viewport covers 800,000 square pixels, giving I=0.28I=0.28. The movement is 80 / 800 = D=0.10D=0.10. The single shift contributes S=0.028S=0.028. If a skeleton reserves the final card geometry, this displacement can be avoided; a skeleton that is 80 px shorter cannot.

The loading presentation should follow the UI state machine rather than arbitrary animation timing:

StateVisual treatmentAccessibility and measurement
Waiting, known result shapeSkeleton with the final container dimensionsMark the result region busy and announce a concise status once
Waiting, unknown result shapeIndeterminate spinner beside plain-language statusDo not imply a percentage or completion time without data
Stream has useful contentShow the actual content and remove the placeholderKeep the busy state active until the stream completes
Complete or failedRemove the loader and show the result or recovery actionClear the busy state and expose the final status

Time to first useful content and time to completion are separate measurements. A fast first token can still precede a long generation, and a skeleton must not be treated as output. Compare p50 and p95 for first useful content, completion time, and layout shift across the same request class. When the endpoint is fast, adding a transient skeleton may flicker; when it is slow and the result has known structure, a stable placeholder can make the wait easier to parse. Measure rather than hard-code a single universal delay.

Trade-offs

A skeleton communicates the shape of a result, so it works well for a known card, chart frame, or answer panel whose dimensions are already constrained. It costs extra markup and design maintenance: if the result changes shape, the placeholder must change too. A spinner communicates indeterminate work without pretending to know the output. It is cheaper when the output could be a short refusal, a long explanation, or a table, but it provides no layout reservation.

Neither pattern changes server inference latency. Do not claim that skeletons are faster from a perceived-speed intuition alone; observe time to useful content, completion, abandonment, and layout shift together. Keep the status message distinct from decorative shapes: a screen reader should hear “Generating answer” or “Answer complete,” not a stream of “loading” announcements for every token. Use aria-busy on the result region while it is incomplete and a separate status region for the state change. Color or shimmer should not be the only signal.

Implementation pattern

The example chooses a skeleton only when the caller knows the final geometry, appends stream chunks as text, records first-content and total timing, and clears busy state on both success and failure. chunks is an async iterable adapted from the application's streaming transport.

typescript
type LoadingState = "idle" | "skeleton" | "spinner" | "streaming" | "ready" | "error";
​
const result = document.querySelector<HTMLElement>("#answer")!;
const skeleton = document.querySelector<HTMLElement>("#answer-skeleton")!;
const spinner = document.querySelector<HTMLElement>("#answer-spinner")!;
const status = document.querySelector<HTMLElement>("#answer-status")!;
​
function showState(state: LoadingState) {
const busy = ["skeleton", "spinner", "streaming"].includes(state);
result.setAttribute("aria-busy", String(busy));
skeleton.hidden = state !== "skeleton";
spinner.hidden = state !== "spinner";
status.textContent = {
idle: "",
skeleton: "Generating answer",
spinner: "Generating answer",
streaming: "Answer is arriving",
ready: "Answer complete",
error: "Answer could not be completed",
}[state];
}
​
export async function renderAnswer(
chunks: AsyncIterable<string>,
hasStableGeometry: boolean,
) {
result.replaceChildren();
const startedAt = performance.now();
performance.mark("answer-start");
showState(hasStableGeometry ? "skeleton" : "spinner");
let firstPart = true;
try {
for await (const chunk of chunks) {
if (firstPart) {
firstPart = false;
performance.mark("answer-first-part");
performance.measure("answer-time-to-first-part", "answer-start", "answer-first-part");
showState("streaming");
}
result.append(document.createTextNode(chunk));
}
showState("ready");
performance.measure("answer-total-time", "answer-start");
} catch (error) {
showState("error");
throw error;
}
return { firstPartReceived: !firstPart, elapsedMs: performance.now() - startedAt };
}

The skeleton and spinner elements should be hidden from assistive technology when they are only decoration, while the separate status region remains available to announce state transitions. Give skeleton rows explicit block sizes or use an aspect ratio for media so their geometry matches the completed result. If the response begins with a non-renderable metadata part, wait until the first meaningful text or UI part before switching from “waiting” to “streaming.”

Failure modes and misconceptions

  • The skeleton is shorter than the content. It still causes a shift when the real answer arrives. Reserve actual dimensions or use a spinner when the layout cannot be predicted.
  • A spinner is shown for a fast response. Very brief feedback can flash and distract. Use measured latency distributions and avoid rendering a transitional state that is likely to disappear before a frame is useful.
  • The placeholder remains beneath partial output. Users may see duplicated content or think the model has not started. Remove the placeholder on the first useful part, not only after the final event.
  • The busy state clears after the first token. That announces completion while more content is still arriving. Keep the result region busy until the stream finishes, is cancelled, or errors.
  • Every streamed token updates a live region. Assistive technology can receive an overwhelming number of announcements. Announce phase changes in a separate status region, and let the response itself remain readable.
  • Perceived speed is reported as server speed. A loading treatment may improve continuity without reducing the actual model latency. Track user-visible timing and backend timing separately.

Decision scenario

A response panel always renders as a 640 px wide answer card with a title, paragraph, and citation area. Its p95 time to first useful content is 1.8 seconds, while the full response often takes six seconds. Use a skeleton that reserves those measured regions, announce generation once, and replace it with content at the first renderable part. If another route can return either a short answer or a large table with no stable geometry, use an indeterminate spinner and a clear status instead.

Learning outcomes

  • Select a skeleton only when its geometry can reserve the eventual content layout.
  • Calculate how a displacement contributes to an individual layout shift score.
  • Distinguish time to first useful content from time to completion.
  • Expose pending, streaming, complete, and error states without flooding a live region.
Reflect before revealing the guide

Prerequisites & Related Concepts (2)

Private notes

0 words
Recommended Free Track
Free · Verifiable Credential

FS-501AI Evaluation and Reliability

Turn product intent and risk into representative evaluations release gates traces service targets and continuously improving regression evidence.

17 concepts~22h self-paced
Start Free Course
Weekly Engineering Dispatch

Get the weekly systems teardown

One technical note per week covering inference bottlenecks, retrieval harnesses, and commit-pinned teardowns.

Next