Charts and tiles
The parts of the usage page, drawn from a summary. Fold it first with deriveUsage(summary, metric).
import { deriveUsage, labelEveryFor } from '@adecore/agents-react/usage';import { windowFor, type UsageMetric } from '@adecore/agents-react/state';
import { useMoney, deriveUsage, labelEveryFor, UsageBreakdown, UsageChart, UsageSummary, UsageTiles } from '@adecore/agents-react/usage';
import { Segmented } from '@adecore/ui';
import { formatTokens } from '@adecore/ui/format';
import { useState } from 'react';
import { usageSummary } from '../shared/agents-data.ts';
import { AgentDemo } from '../shared/agents.tsx';
const summary = usageSummary(windowFor('30d'));
function Parts() {
const [metric, setMetric] = useState<UsageMetric>('cost');
const money = useMoney();
const derived = deriveUsage(summary, metric);
return (
<div className="flex w-full flex-col gap-6">
<Segmented<UsageMetric>
value={metric}
label="Metric"
options={[
{ id: 'cost', label: 'Cost' },
{ id: 'tokens', label: 'Tokens' }
]}
onValueChange={setMetric}
/>
<div className="grid gap-6 lg:grid-cols-[18rem_1fr]">
<UsageSummary metric={metric} costUsd={derived.costUsd} totals={derived.totals} sessions={summary.sessions} providers={derived.providers} />
<UsageChart
slots={derived.slots}
providers={derived.active}
format={metric === 'cost' ? money : formatTokens}
labelEvery={labelEveryFor(derived.slots.length)}
/>
</div>
<UsageTiles totals={derived.totals} cacheSavingsUsd={derived.cacheSavingsUsd} />
<UsageBreakdown summary={summary} metric={metric} providers={derived.active} />
</div>
);
}
export default function UsagePartsDemo() {
return (
<AgentDemo>
<Parts />
</AgentDemo>
);
}const derived = deriveUsage(summary, metric);
const money = useMoney();
<UsageSummary metric={metric} costUsd={derived.costUsd} totals={derived.totals} sessions={summary.sessions} providers={derived.providers} />
<UsageChart slots={derived.slots} providers={derived.active} format={metric === 'cost' ? money : formatTokens} labelEvery={labelEveryFor(derived.slots.length)} />
<UsageTiles totals={derived.totals} cacheSavingsUsd={derived.cacheSavingsUsd} />
<UsageBreakdown summary={summary} metric={metric} providers={derived.active} />UsageSummaryis the total of the period with its calls and sessions, and a legend row per CLI.UsageChartstacks a bar per slot by CLI, every slot of the period, an empty one included.formatwrites the values;labelEverysays which slots get a label.UsageTilesare the tokens processed, uncached and cached input, cache writes, output, and what the cache saved.UsageBreakdowncuts the period three ways behind tabs: by model, by folder and by day.
deriveUsage returns the chart's slots, the totals per CLI (providers), the CLIs with activity (active), and the period's totals, cost and cache savings. A slot is one bar with what each CLI put in it and the height of the stack. A total per CLI is a ProviderTotal, one row of the legend. deriveDays(summary) is the table by day, newest first. enumerateSlots and niceScale build the axis.
Money and numbers
Every price is in dollars. useMoney() returns the formatter of the chosen currency at the summary's rate; moneyFormat(currency, rate) works outside React. formatCount, formatTokens, formatDate, slotLabel, slotAxisLabel and shortPath write the rest. PROVIDER_LABELS and PROVIDER_COLORS name and color each CLI.