Xzero UI
Pro library

Prompt cachePro

An animated explainer for prompt caching: the shared prefix of each request is cached once, later requests read it back, and the cost and time saved add up.

$ pnpm dlx shadcn@4.23.0 add @xzero-ui-pro/prompt-cache
Live · keyboard ready

When to use

When not to use

Installation

Register both Xzero UI namespaces in your project's components.json:

{
  "registries": {
    "@xzero-ui": "https://mcp.xzero.co.uk/r/{name}.json",
    "@xzero-ui-pro": "https://mcp.xzero.co.uk/r/pro/{name}.json"
  }
}
npx shadcn@4.23.0 add @xzero-ui-pro/prompt-cache

Install xzero-foundation once, and import @/components/xzero/foundation.css in your app root. No Xzero token is used by this snapshot. For sample portraits, photos, fonts, or logos, download https://mcp.xzero.co.uk/install-assets.mjs and run node xzero-install-assets.mjs <receiving-project>.

Usage

import { PromptCache } from "@/components/xzero/prompt-cache/prompt-cache";

export function CachingSection() {
  return <PromptCache
    segments={[
      { id: "tools", label: "Tools", tokens: 3_800 },
      { id: "system", label: "System prompt", tokens: 2_400 },
      { id: "documents", label: "Documents", tokens: 36_200 },
      { id: "conversation", label: "Conversation", tokens: 6_000 },
    ]}
    requests={[
      { message: "Which refunds are still pending this week?", tokens: 180 },
      { message: "Draft a reply to Northwind about their late refund", tokens: 240, gap: 42 },
      { message: "Summarize ticket 48213 and the attached invoice", tokens: 410, gap: 35 },
      { message: "Which customers asked about SSO in October?", tokens: 150, gap: 51 },
    ]}
    onRequest={result => console.log(result.kind, result.cost)}
  />;
}

API reference

PromptCache

An animated explainer for prompt caching. Four requests stack over time, each a bar of the same prefix segments (tools, system prompt, documents, conversation) plus its own new message, with token counts in the legend. The first request processes every segment in one steady sweep, then the prefix lifts out of the bar and travels into a cache panel, where a time to live ring fills. Later requests light their matching prefix as a cache hit with one quick sweep, only the new message is processed, and their processed tokens, input cost and time to first token roll down from the row above. A toggle edits one segment: the next request matches the cache only up to it, shows a break marker there, and rewrites the rest. When no requests arrive, the ring drains on an accelerated clock and the cache expires. Totals roll into a summary of input cost saved and first token speedup.

PropTypeDefaultDescription
segments{ id: string; label: string; tokens: number; color?: string }[]Tools 3.8K, System prompt 2.4K, Documents 36.2K, Conversation 6KThe stable prefix in prompt order. Prefix matching runs left to right, so put what changes least first. Colors default to the chart series tokens. Bar widths use a square root scale so a short message stays visible beside a long document; the legend shows the real counts.
requests{ message?: string; tokens: number; gap?: number }[]Four support questions of 150 to 410 tokens, 35 to 51 seconds apartOne to four requests per round. tokens is the size of the new message (the uncached suffix); gap is the simulated seconds since the previous request. message is used in the accessible description and the onRequest payload.
model{ name: string; mark?: ReactNode; inputPrice: number; cacheWritePrice: number; cacheReadPrice: number }Claude Sonnet 5.5: $2.00 input, $2.50 cache write, $0.20 cache read per million tokensThe model line in the header and the prices behind every cost. Prices are dollars per million input tokens. Pass your provider's current prices; the default uses Claude's pattern of reads at 0.1x and five minute writes at 1.25x the input price.
ttlnumber300Seconds the cache lives after its last use. Every hit refreshes it. Shown as the ring and the countdown, and in the copy as a duration.
changeSegmentstring | null"system"Id of the segment the change toggle edits. The toggle arms one edit for the next request and releases once that request is sent. Pass null to hide it.
autoPlaybooleantruePlays on its own while in view. With reduced motion it opens on a finished round instead and waits for play or step.
loopbooleantrueAfter a round the cache idles, expires and the story starts over. When false it stops after the cache expires.
speednumber1Multiplies the pace of the clock and every animation. 0 holds still.
firstToken(tokens: { processed: number; cached: number }) => number0.42 + processed / 40000 + cached / 500000Seconds to the first output token for a prompt. The default is an illustrative model: a fixed overhead, prefill of new tokens, and a much faster load of cached ones.
onRequest(result: PromptCacheResult) => void–Called when a request finishes, with index, simulated time, kind (write, hit or partial), breakAt, cachedTokens, writtenTokens, processedTokens, cost, uncachedCost, firstToken, uncachedFirstToken, changed, cold and message.
onExpire() => void–Called when the cache expires.
messageLabelstring"New message"Legend label for the suffix at the end of each prompt.
titlestring"Prompt cache"Heading of the card, also used in its accessible name.
classNamestring–Extra class on the root section.
styleCSSProperties–Inline styles on the root section.

Keyboard interactions

KeysAction
TabMoves through play or pause, send the next request, reset, and the change toggle.
Enter, SpacePlays or pauses, sends the next request, resets, or arms and disarms the edit.

Accessibility

Motion

Responsive behavior

Performance

Notes for AI

Also in illustrations

Guidance for AI tools

Prompt cache: An animated explainer for prompt caching: the shared prefix of each request is cached once, later requests read it back, and the cost and time saved add up. Follow the declared prop types and do not invent props. Keep keyboard access, reduced motion support, and both light and dark themes intact when adapting it.

Full library index: https://ui.xzero.co.uk/llms.txt

Browse all components