{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "ai-usage-03",
  "description": "Requests, time to first token from the median to the 95th percentile, output speed and error rate for each model.",
  "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/table.json"
  ],
  "files": [
    {
      "path": "registry/components/dashboardblocks/ai-usage/ai-usage-03.tsx",
      "content": "'use client'\n\nimport { modelPalette } 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  DataTable,\n  DataTableBody,\n  DataTableCell,\n  DataTableHead,\n  DataTableHeader,\n  DataTableRow,\n  DataTableRowHead,\n  DataTableRowLink,\n  DataTableStateRows,\n} from '@/registry/components/dashboardblocks/table'\nimport { formatNumber, formatPercent } from '@/registry/lib/dashboardblocks/format'\n\nimport { Card, CardContent } from '@/components/ui/card'\n\nimport { cn } from '@/lib/utils'\n\ninterface ModelPerformance {\n  /** Share of requests that errored, 0–1. */\n  errorRate: number\n  /** The model's calls: the whole row opens them. */\n  href?: string\n  label: string\n  /** Output tokens per second while streaming. */\n  outputSpeed: number\n  requests: number\n  /** Time to first token, median, in ms. */\n  ttftP50: number\n  /** Time to first token, 95th percentile, in ms. */\n  ttftP95: number\n}\n\ninterface AiUsage3Props extends BlockStateProps {\n  /** Defaults to a sentence naming the busiest model and any over the error threshold. */\n  description?: string\n  /** Error rate above which a model is flagged, 0–1. */\n  errorThreshold?: number\n  /** Leave out, or pass none, to show the empty state. */\n  models?: ModelPerformance[]\n  title: string\n}\n\nconst exampleProps: AiUsage3Props = {\n  models: [\n    {\n      errorRate: 0.004,\n      label: 'Reasoning',\n      outputSpeed: 58,\n      requests: 41_200,\n      ttftP50: 1_240,\n      ttftP95: 3_880,\n    },\n    {\n      errorRate: 0.0012,\n      label: 'Fast',\n      outputSpeed: 164,\n      requests: 318_000,\n      ttftP50: 310,\n      ttftP95: 790,\n    },\n    {\n      errorRate: 0.021,\n      label: 'Vision',\n      outputSpeed: 91,\n      requests: 12_600,\n      ttftP50: 880,\n      ttftP95: 2_940,\n    },\n  ],\n  title: 'Model performance, last 24 hours',\n}\n\nconst ms = (value: number) =>\n  value >= 1_000\n    ? `${formatNumber(value / 1_000, { maximumFractionDigits: 2, minimumFractionDigits: 2 })} s`\n    : `${formatNumber(value, { maximumFractionDigits: 0 })} ms`\n\nconst percent = (value: number, digits = 0) =>\n  formatPercent(value, {\n    maximumFractionDigits: digits,\n    minimumFractionDigits: digits,\n  })\n\nconst describe = (models: ModelPerformance[], errorThreshold: number) => {\n  const requests = models.reduce((sum, model) => sum + model.requests, 0)\n  const busiest = models.reduce<ModelPerformance | undefined>(\n    (top, model) => (top && top.requests >= model.requests ? top : model),\n    undefined,\n  )\n  if (!busiest || requests === 0) return 'No requests in this period.'\n  const flagged = models.filter((model) => model.errorRate > errorThreshold)\n  const errors =\n    flagged.length === 0\n      ? `Every model is under the ${percent(errorThreshold)} error threshold.`\n      : flagged.length === 1\n        ? `${flagged[0].label} errors on ${percent(flagged[0].errorRate, 1)} of requests, over the ${percent(errorThreshold)} threshold.`\n        : `${flagged.length} models are over the ${percent(errorThreshold)} error threshold.`\n  return `${busiest.label} served ${percent(busiest.requests / requests)} of ${formatNumber(requests)} requests. ${errors}`\n}\n\nfunction LatencyRange({ max, p50, p95 }: { max: number; p50: number; p95: number }) {\n  const at = (value: number) => `${(Math.min(value, max) / (max || 1)) * 100}%`\n  return (\n    <span\n      aria-hidden\n      className='bg-muted relative block h-1.5 w-20 shrink-0 rounded-full'\n    >\n      <span\n        className='absolute inset-y-0 rounded-full bg-chart-2/35'\n        style={{ left: at(p50), right: `calc(100% - ${at(p95)})` }}\n      />\n      <span\n        className='ring-background absolute top-1/2 size-2.5 -translate-x-1/2 -translate-y-1/2 rounded-full bg-chart-2 ring-2'\n        style={{ left: at(p50) }}\n      />\n    </span>\n  )\n}\n\nconst AiUsage3 = (props: AiUsage3Props) => {\n  const { errorThreshold = 0.01, models = [], errorMessage, status, title } = props\n  const { description = describe(models, errorThreshold) } = props\n  const state = getBlockState(status, models)\n  const slowest = Math.max(0, ...models.map((model) => model.ttftP95))\n\n  return (\n    <Card>\n      <CardContent>\n        <BlockStateProvider errorMessage={errorMessage} state={state}>\n          <section className='@container flex flex-col gap-3'>\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            <DataTable caption={title} className='min-w-[34rem] border-t tabular-nums'>\n              <DataTableHeader>\n                <DataTableRow>\n                  <DataTableHead>Model</DataTableHead>\n                  <DataTableHead align='end'>Requests</DataTableHead>\n                  <DataTableHead align='end'>\n                    <abbr\n                      title='Time to first token, median and 95th percentile'\n                      className='no-underline'\n                    >\n                      TTFT, median to p95\n                    </abbr>\n                  </DataTableHead>\n                  <DataTableHead align='end'>Tokens/s</DataTableHead>\n                  <DataTableHead align='end'>Errors</DataTableHead>\n                </DataTableRow>\n              </DataTableHeader>\n              <DataTableBody>\n                <DataTableStateRows colSpan={5} rows={3} />\n                {!state &&\n                  models.map((model, index) => {\n                    const flagged = model.errorRate > errorThreshold\n                    return (\n                      <DataTableRow key={model.label}>\n                        <DataTableRowHead primary>\n                          <span className='flex items-center gap-2'>\n                            <span\n                              aria-hidden\n                              className='size-2.5 rounded-[3px]'\n                              style={{\n                                backgroundColor:\n                                  modelPalette[index % modelPalette.length],\n                              }}\n                            />\n                            {model.href ? (\n                              <DataTableRowLink href={model.href}>\n                                {model.label}\n                              </DataTableRowLink>\n                            ) : (\n                              model.label\n                            )}\n                          </span>\n                        </DataTableRowHead>\n                        <DataTableCell align='end' className='text-muted-foreground'>\n                          {formatNumber(model.requests)}\n                        </DataTableCell>\n                        <DataTableCell align='end'>\n                          <span className='flex items-center justify-end gap-2.5'>\n                            <span>{ms(model.ttftP50)}</span>\n                            <LatencyRange\n                              max={slowest}\n                              p50={model.ttftP50}\n                              p95={model.ttftP95}\n                            />\n                            <span className='text-muted-foreground w-14 text-end'>\n                              {ms(model.ttftP95)}\n                            </span>\n                          </span>\n                        </DataTableCell>\n                        <DataTableCell align='end'>\n                          {formatNumber(model.outputSpeed, {\n                            maximumFractionDigits: 0,\n                          })}\n                        </DataTableCell>\n                        <DataTableCell\n                          align='end'\n                          className={cn(\n                            flagged && 'font-medium text-red-700 dark:text-red-400',\n                          )}\n                        >\n                          {percent(model.errorRate, 2)}\n                          {flagged && (\n                            <span className='sr-only'>\n                              , above the {percent(errorThreshold)} threshold\n                            </span>\n                          )}\n                        </DataTableCell>\n                      </DataTableRow>\n                    )\n                  })}\n              </DataTableBody>\n            </DataTable>\n            <BlockReady className='text-xs'>\n              <p className='text-muted-foreground text-xs'>\n                TTFT is time to first token. The dot is the median and the bar runs to the\n                95th percentile. Error rates over {percent(errorThreshold)} are red.\n              </p>\n            </BlockReady>\n          </section>\n        </BlockStateProvider>\n      </CardContent>\n    </Card>\n  )\n}\n\nexport {\n  AiUsage3,\n  exampleProps as aiUsage3ExampleProps,\n  type AiUsage3Props,\n  type ModelPerformance,\n}\n",
      "type": "registry:component",
      "target": "@components/dashboardblocks/ai-usage/ai-usage-03.tsx"
    }
  ],
  "meta": {
    "shows": "Requests, time to first token from the median to the 95th percentile, output speed and error rate for each model.",
    "size": "full",
    "usedWith": [
      "ai-usage-01",
      "ai-usage-02"
    ],
    "component": "AiUsage3",
    "props": "{ description?: string; errorThreshold?: number; models?: { errorRate: number; href?: string; label: string; outputSpeed: number; requests: number; ttftP50: number; ttftP95: number }[]; title: string; errorMessage?: string; status?: \"loading\" | \"error\" }"
  },
  "type": "registry:component"
}
