{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "ai-usage-04",
  "description": "The share of input tokens served from the cache, what it saved and what the tokens would have cost without it.",
  "registryDependencies": [
    "card",
    "https://www.dashboardblocks.com/r/radix/ai-usage.json",
    "https://www.dashboardblocks.com/r/radix/block-state.json",
    "https://www.dashboardblocks.com/r/radix/kpi.json"
  ],
  "files": [
    {
      "path": "registry/components/dashboardblocks/ai-usage/ai-usage-04.tsx",
      "content": "'use client'\n\nimport {\n  type TokenCounts,\n  type TokenPrices,\n  TokenSplitBar,\n  formatTokens,\n  formatUsd,\n  getCacheHitRate,\n  getCacheSavings,\n  getTokenCost,\n  tokenPartColors,\n} from '@/registry/components/dashboardblocks/ai-usage'\nimport {\n  BlockReady,\n  BlockStateProvider,\n  type BlockStateProps,\n  getBlockState,\n} from '@/registry/components/dashboardblocks/block-state'\nimport {\n  KPI,\n  KPIChange,\n  KPITitle,\n  KPIValue,\n} from '@/registry/components/dashboardblocks/kpi'\n\nimport { Card, CardContent } from '@/components/ui/card'\n\ninterface AiUsage4Props extends BlockStateProps {\n  /** Follows the previous hit rate in the sentence. @default 'last month' */\n  comparison?: string\n  description: string\n  /** Hit rate for the period before, 0–1, to show the change. */\n  previousHitRate?: number\n  prices: TokenPrices\n  title: string\n  /** Leave out to show the empty state. */\n  tokens?: TokenCounts\n}\n\nconst exampleProps: AiUsage4Props = {\n  description: 'All requests this month. Cached input costs a tenth of the input price.',\n  previousHitRate: 0.24,\n  prices: { cached: 0.3, input: 3, output: 15 },\n  title: 'Prompt caching',\n  tokens: { cached: 83_000_000, input: 197_000_000, output: 25_500_000 },\n}\n\nconst tokenParts = [\n  { key: 'input', label: 'Input' },\n  { key: 'cached', label: 'Cached' },\n  { key: 'output', label: 'Output' },\n] as const\n\nconst AiUsage4 = (props: AiUsage4Props) => {\n  const {\n    comparison = 'last month',\n    description,\n    previousHitRate,\n    prices,\n    errorMessage,\n    status,\n    title,\n    tokens = { input: 0, output: 0 },\n  } = props\n  const state = getBlockState(status, props.tokens)\n  const hitRate = getCacheHitRate(tokens)\n  const saved = getCacheSavings(tokens.cached ?? 0, prices)\n  const cost = getTokenCost(tokens, prices)\n  const asPercent = (rate: number) => Math.round(rate * 1_000) / 10\n\n  return (\n    <Card>\n      <CardContent>\n        <BlockStateProvider errorMessage={errorMessage} state={state}>\n          <section className='@container flex flex-col gap-4'>\n            <header className='flex flex-col gap-0.5'>\n              <h2 className='font-semibold tracking-tight'>{title}</h2>\n              <p className='text-muted-foreground text-sm'>{description}</p>\n            </header>\n            <KPI className='gap-1 pt-4'>\n              <KPITitle>Input tokens served from the cache</KPITitle>\n              <KPIValue value={asPercent(hitRate)} format='percent' animated />\n              {previousHitRate !== undefined && (\n                <KPIChange\n                  changeType='points'\n                  comparison={comparison}\n                  previous={asPercent(previousHitRate)}\n                  value={asPercent(hitRate)}\n                />\n              )}\n            </KPI>\n            {state !== 'empty' && state !== 'error' && (\n              <>\n                <dl className='grid grid-cols-2 gap-4 border-t pt-4'>\n                  <div className='flex flex-col gap-1'>\n                    <dt className='text-muted-foreground text-sm'>Saved by caching</dt>\n                    <dd>\n                      <KPIValue className='text-2xl' value={saved} format={formatUsd} />\n                    </dd>\n                  </div>\n                  <div className='flex flex-col gap-1'>\n                    <dt className='text-muted-foreground text-sm'>Spent on tokens</dt>\n                    <dd>\n                      <KPIValue className='text-2xl' value={cost} format={formatUsd} />\n                    </dd>\n                    <dd className='text-muted-foreground text-xs'>\n                      <BlockReady className='text-xs'>\n                        {formatUsd(cost + saved)} without the cache\n                      </BlockReady>\n                    </dd>\n                  </div>\n                </dl>\n                <div className='flex flex-col gap-2'>\n                  <TokenSplitBar tokens={tokens} />\n                  <BlockReady className='text-xs'>\n                    <ul className='text-muted-foreground flex flex-wrap gap-x-4 gap-y-1 text-xs tabular-nums'>\n                      {tokenParts.map((part) => (\n                        <li key={part.key} className='flex items-center gap-1.5'>\n                          <span\n                            aria-hidden\n                            className='size-2.5 rounded-[3px]'\n                            style={{\n                              backgroundColor: tokenPartColors[part.key],\n                            }}\n                          />\n                          {part.label}\n                          <span className='text-foreground font-medium'>\n                            {formatTokens(tokens[part.key] ?? 0)}\n                          </span>\n                        </li>\n                      ))}\n                    </ul>\n                  </BlockReady>\n                </div>\n              </>\n            )}\n          </section>\n        </BlockStateProvider>\n      </CardContent>\n    </Card>\n  )\n}\n\nexport { AiUsage4, exampleProps as aiUsage4ExampleProps, type AiUsage4Props }\n",
      "type": "registry:component",
      "target": "@components/dashboardblocks/ai-usage/ai-usage-04.tsx"
    }
  ],
  "meta": {
    "shows": "The share of input tokens served from the cache, what it saved and what the tokens would have cost without it.",
    "size": "half",
    "usedWith": [
      "ai-usage-01",
      "ai-usage-02"
    ],
    "component": "AiUsage4",
    "props": "{ comparison?: string; description: string; previousHitRate?: number; prices: { cached?: number; input: number; output: number }; title: string; tokens?: { cached?: number; input: number; output: number }; errorMessage?: string; status?: \"loading\" | \"error\" }"
  },
  "type": "registry:component"
}
