{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "ai-usage-02",
  "description": "Model spend by product feature, with each feature's input, cached and output tokens and its cost per thousand requests.",
  "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/format.json",
    "https://www.dashboardblocks.com/r/radix/kpi.json",
    "https://www.dashboardblocks.com/r/radix/table.json"
  ],
  "files": [
    {
      "path": "registry/components/dashboardblocks/ai-usage/ai-usage-02.tsx",
      "content": "'use client'\n\nimport {\n  type TokenCounts,\n  TokenSplitBar,\n  formatTokens,\n  formatUsd,\n  tokenPartColors,\n} from '@/registry/components/dashboardblocks/ai-usage'\nimport {\n  BlockReady,\n  BlockStateProvider,\n  BlockStateRows,\n  BlockStateText,\n  type BlockStateProps,\n  getBlockState,\n} from '@/registry/components/dashboardblocks/block-state'\nimport { KPIValue } from '@/registry/components/dashboardblocks/kpi'\nimport {\n  DataTableListItem,\n  DataTableRowLink,\n} from '@/registry/components/dashboardblocks/table'\nimport { formatPercent, joinDetails } from '@/registry/lib/dashboardblocks/format'\n\nimport { Card, CardContent } from '@/components/ui/card'\n\ninterface FeatureUsage {\n  cost: number\n  /** The feature's calls or usage page: the whole row opens it. */\n  href?: string\n  label: string\n  requests: number\n  tokens: TokenCounts\n}\n\ninterface AiUsage2Props extends BlockStateProps {\n  /** Defaults to a sentence naming the feature that costs the most. */\n  description?: string\n  /** Leave out, or pass none, to show the empty state. */\n  features?: FeatureUsage[]\n  /** Ends the sentence under the total. @default 'this month' */\n  period?: string\n  title: string\n}\n\nconst exampleProps: AiUsage2Props = {\n  features: [\n    {\n      cost: 842,\n      label: 'Research agent',\n      requests: 18_400,\n      tokens: { cached: 21_000_000, input: 64_000_000, output: 9_800_000 },\n    },\n    {\n      cost: 391,\n      label: 'Chat assistant',\n      requests: 212_000,\n      tokens: { cached: 58_000_000, input: 41_000_000, output: 12_600_000 },\n    },\n    {\n      cost: 144,\n      label: 'Ticket summaries',\n      requests: 96_500,\n      tokens: { cached: 4_000_000, input: 38_000_000, output: 3_100_000 },\n    },\n    {\n      cost: 27,\n      label: 'Search embeddings',\n      requests: 1_480_000,\n      tokens: { input: 54_000_000, output: 0 },\n    },\n  ],\n  title: 'Cost by feature',\n}\n\nconst tokenParts = [\n  { color: tokenPartColors.input, label: 'Input' },\n  { color: tokenPartColors.cached, label: 'Cached input' },\n  { color: tokenPartColors.output, label: 'Output' },\n]\n\nconst describe = (sorted: FeatureUsage[], total: number) => {\n  const top = sorted[0]\n  if (!top || total <= 0) return 'No model spend yet.'\n  return `${top.label} takes ${formatPercent(top.cost / total, { maximumFractionDigits: 0 })} of the spend.`\n}\n\nconst AiUsage2 = (props: AiUsage2Props) => {\n  const { features = [], period = 'this month', errorMessage, status, title } = props\n  const state = getBlockState(status, features)\n  const total = features.reduce((sum, feature) => sum + feature.cost, 0)\n  const sorted = [...features].sort((a, b) => b.cost - a.cost)\n  const { description = describe(sorted, total) } = props\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              <BlockReady className='text-sm'>\n                <p className='text-muted-foreground text-sm'>{description}</p>\n              </BlockReady>\n            </header>\n            <div className='flex flex-col gap-1 border-t pt-4'>\n              <KPIValue value={total} format={formatUsd} />\n              {state === 'loading' ? (\n                <BlockStateText className='text-sm' />\n              ) : state ? null : (\n                <p className='text-muted-foreground text-sm'>\n                  Spent on models across {features.length}{' '}\n                  {features.length === 1 ? 'feature' : 'features'} {period}.\n                </p>\n              )}\n            </div>\n            {state !== 'empty' && state !== 'error' && (\n              <ul\n                aria-hidden\n                className='text-muted-foreground flex flex-wrap gap-x-4 gap-y-1 text-xs'\n              >\n                {tokenParts.map((part) => (\n                  <li key={part.label} className='flex items-center gap-1.5'>\n                    <span\n                      className='size-2.5 rounded-[3px]'\n                      style={{ backgroundColor: part.color }}\n                    />\n                    {part.label}\n                  </li>\n                ))}\n              </ul>\n            )}\n            {state === 'loading' && <BlockStateRows className='border-t' rows={4} />}\n            {!state && (\n              <ul className='flex flex-col border-t'>\n                {sorted.map((feature) => {\n                  const all =\n                    feature.tokens.input +\n                    (feature.tokens.cached ?? 0) +\n                    feature.tokens.output\n                  return (\n                    <DataTableListItem\n                      key={feature.label}\n                      className='flex flex-col gap-2 border-b py-3 last:border-b-0'\n                    >\n                      <div className='flex items-baseline justify-between gap-3'>\n                        {feature.href ? (\n                          <DataTableRowLink href={feature.href} className='text-sm'>\n                            {feature.label}\n                          </DataTableRowLink>\n                        ) : (\n                          <span className='text-sm font-medium'>{feature.label}</span>\n                        )}\n                        <span className='text-sm tabular-nums'>\n                          <span className='font-medium'>{formatUsd(feature.cost)}</span>\n                          <span className='text-muted-foreground ml-1.5 inline-block w-8 text-right text-xs'>\n                            {formatPercent(total > 0 ? feature.cost / total : 0, {\n                              maximumFractionDigits: 0,\n                            })}\n                          </span>\n                        </span>\n                      </div>\n                      <TokenSplitBar tokens={feature.tokens} />\n                      <div className='text-muted-foreground flex flex-wrap justify-between gap-x-3 gap-y-0.5 text-xs tabular-nums'>\n                        <span>\n                          {joinDetails(\n                            `${formatTokens(all)} tokens`,\n                            `${formatTokens(feature.requests)} requests`,\n                          )}\n                        </span>\n                        <span>\n                          {formatUsd((feature.cost / (feature.requests || 1)) * 1_000)}{' '}\n                          per 1K requests\n                        </span>\n                      </div>\n                      <span className='sr-only'>\n                        {`${formatTokens(feature.tokens.input)} input, ${formatTokens(feature.tokens.cached ?? 0)} cached input and ${formatTokens(feature.tokens.output)} output tokens.`}\n                      </span>\n                    </DataTableListItem>\n                  )\n                })}\n              </ul>\n            )}\n          </section>\n        </BlockStateProvider>\n      </CardContent>\n    </Card>\n  )\n}\n\nexport {\n  AiUsage2,\n  exampleProps as aiUsage2ExampleProps,\n  type AiUsage2Props,\n  type FeatureUsage,\n}\n",
      "type": "registry:component",
      "target": "@components/dashboardblocks/ai-usage/ai-usage-02.tsx"
    }
  ],
  "meta": {
    "shows": "Model spend by product feature, with each feature's input, cached and output tokens and its cost per thousand requests.",
    "size": "full",
    "usedWith": [
      "ai-usage-01",
      "ai-usage-04",
      "spend-01"
    ],
    "component": "AiUsage2",
    "props": "{ description?: string; features?: { cost: number; href?: string; label: string; requests: number; tokens: { cached?: number; input: number; output: number } }[]; period?: string; title: string; errorMessage?: string; status?: \"loading\" | \"error\" }"
  },
  "type": "registry:component"
}
