Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go

feat(runtimes): add Tokens metric toggle to Usage Daily chart (MUL-2148) (#2537)

Bohan Jiangc6ccc496507d
diff --git a/packages/views/locales/en/runtimes.json b/packages/views/locales/en/runtimes.json
index 246905b561..8dfcc7623c 100644
--- a/packages/views/locales/en/runtimes.json
+++ b/packages/views/locales/en/runtimes.json
@@ -185,9 +185,12 @@
"when_tab_daily": "Daily",
"when_tab_hourly": "Hourly",
"when_tab_heatmap": "Heatmap",
+ "daily_metric_cost": "Cost",
+ "daily_metric_tokens": "Tokens",
"heatmap_caption": "Last 26 weeks · daily $ intensity (period selector ignored here)",
"legend_input": "Input",
"legend_output": "Output",
+ "legend_cache_read": "Cache read",
"legend_cache_write": "Cache write",
"empty_no_usage": "No usage in this period.",
"empty_pricing_missing": "Tokens recorded but pricing missing for:",
diff --git a/packages/views/locales/zh-Hans/runtimes.json b/packages/views/locales/zh-Hans/runtimes.json
index 7355cfceae..9ecdc0f17d 100644
--- a/packages/views/locales/zh-Hans/runtimes.json
+++ b/packages/views/locales/zh-Hans/runtimes.json
@@ -180,9 +180,12 @@
"when_tab_daily": "按天",
"when_tab_hourly": "按小时",
"when_tab_heatmap": "热力图",
+ "daily_metric_cost": "费用",
+ "daily_metric_tokens": "Token",
"heatmap_caption": "最近 26 周 · 每日 $ 强度(此处忽略上方时间范围)",
"legend_input": "输入",
"legend_output": "输出",
+ "legend_cache_read": "缓存读取",
"legend_cache_write": "缓存写入",
"empty_no_usage": "此期间无使用记录。",
"empty_pricing_missing": "记录到 token,但缺少这些模型的价格:",
diff --git a/packages/views/runtimes/components/charts/daily-tokens-chart.tsx b/packages/views/runtimes/components/charts/daily-tokens-chart.tsx
new file mode 100644
index 0000000000..27da0408eb
--- /dev/null
+++ b/packages/views/runtimes/components/charts/daily-tokens-chart.tsx
@@ -0,0 +1,94 @@
+import {
+ BarChart,
+ Bar,
+ XAxis,
+ YAxis,
+ CartesianGrid,
+} from "recharts";
+import {
+ ChartContainer,
+ ChartTooltip,
+ ChartTooltipContent,
+ type ChartConfig,
+} from "@multica/ui/components/ui/chart";
+import { formatTokens, type DailyTokenData } from "../../utils";
+
+// Four-segment stack — input / output / cache read / cache write. Unlike the
+// cost chart, cache reads ARE visible here: a typical day on Claude shows
+// cache reads dominating raw token counts (often 10×+ input), so the user
+// only sees the real shape of usage when reads are stacked in. The cost
+// chart drops them for the opposite reason (their dollar contribution is
+// two orders of magnitude smaller and would be visually invisible).
+//
+// Series → CSS chart token: stack reads bottom-up as chart-1 (deepest brand
+// blue, "input") → chart-2 (mid) → chart-4 (cache read) → chart-3 (lightest,
+// "cache write"). Cache read gets chart-4 so the two cache series are
+// visually adjacent and tonally distinct from input/output.
+export const tokenStackConfig = {
+ input: { label: "Input", color: "var(--chart-1)" },
+ output: { label: "Output", color: "var(--chart-2)" },
+ cacheRead: { label: "Cache read", color: "var(--chart-4)" },
+ cacheWrite: { label: "Cache write", color: "var(--chart-3)" },
+} satisfies ChartConfig;
+
+export function DailyTokensChart({ data }: { data: DailyTokenData[] }) {
+ // No internal empty-state — same convention as DailyCostChart: the parent
+ // decides what to render when there's nothing to show.
+ return (
+ <ChartContainer config={tokenStackConfig} className="aspect-[3/1] w-full">
+ <BarChart data={data} margin={{ left: 0, right: 0, top: 4, bottom: 0 }}>
+ <CartesianGrid vertical={false} />
+ <XAxis
+ dataKey="label"
+ tickLine={false}
+ axisLine={false}
+ tickMargin={8}
+ interval="preserveStartEnd"
+ />
+ <YAxis
+ tickLine={false}
+ axisLine={false}
+ tickMargin={8}
+ tickFormatter={(v: number) => formatTokens(v)}
+ width={50}
+ />
+ <ChartTooltip
+ content={
+ <ChartTooltipContent
+ formatter={(value, name) =>
+ typeof value === "number"
+ ? `${formatTokens(value)} ${name}`
+ : `${value} ${name}`
+ }
+ />
+ }
+ />
+ {/* Legend is rendered by the parent in the chart card header. */}
+ <Bar
+ dataKey="input"
+ stackId="tokens"
+ fill="var(--color-input)"
+ radius={[0, 0, 0, 0]}
+ />
+ <Bar
+ dataKey="output"
+ stackId="tokens"
+ fill="var(--color-output)"
+ radius={[0, 0, 0, 0]}
+ />
+ <Bar
+ dataKey="cacheRead"
+ stackId="tokens"
+ fill="var(--color-cacheRead)"
+ radius={[0, 0, 0, 0]}
+ />
+ <Bar
+ dataKey="cacheWrite"
+ stackId="tokens"
+ fill="var(--color-cacheWrite)"
+ radius={[3, 3, 0, 0]}
+ />
+ </BarChart>
+ </ChartContainer>
+ );
+}
diff --git a/packages/views/runtimes/components/charts/index.ts b/packages/views/runtimes/components/charts/index.ts
index 5184df2941..509ac3d846 100644
--- a/packages/views/runtimes/components/charts/index.ts
+++ b/packages/views/runtimes/components/charts/index.ts
@@ -1,3 +1,4 @@
export { DailyCostChart, costStackConfig } from "./daily-cost-chart";
+export { DailyTokensChart, tokenStackConfig } from "./daily-tokens-chart";
export { ActivityHeatmap } from "./activity-heatmap";
export { HourlyActivityChart } from "./hourly-activity-chart";
diff --git a/packages/views/runtimes/components/usage-section.tsx b/packages/views/runtimes/components/usage-section.tsx
index b04a445250..9f13fe74cc 100644
--- a/packages/views/runtimes/components/usage-section.tsx
+++ b/packages/views/runtimes/components/usage-section.tsx
@@ -30,6 +30,7 @@ import { KpiCard } from "./shared";
import { ActorAvatar } from "../../common/actor-avatar";
import {
DailyCostChart,
+ DailyTokensChart,
HourlyActivityChart,
ActivityHeatmap,
} from "./charts";
@@ -245,6 +246,7 @@ export function UsageSection({ runtimeId }: { runtimeId: string }) {
// ---------------------------------------------------------------------------
type WhenTab = "daily" | "hourly" | "heatmap";
+type DailyMetric = "cost" | "tokens";
function WhenChart({
runtimeId,
@@ -259,6 +261,10 @@ function WhenChart({
}) {
const { t } = useT("runtimes");
const [tab, setTab] = useState<WhenTab>("daily");
+ // Daily-tab metric toggle (Cost vs Tokens). Stored here rather than in
+ // DailyTab so the legend in the card header can react to the active
+ // metric without prop-drilling.
+ const [dailyMetric, setDailyMetric] = useState<DailyMetric>("cost");
// Memo dep — the aggregates below run `estimateCost`, which now consults
// the user override store. Without listing pricings here the memos cache
// pre-override totals when query data hasn't changed.
@@ -271,7 +277,7 @@ function WhenChart({
enabled: tab === "hourly",
});
- const { dailyCostStack } = useMemo(
+ const { dailyCostStack, dailyTokens } = useMemo(
() => aggregateByDate(filtered),
[filtered, pricings],
);
@@ -300,8 +306,26 @@ function WhenChart({
] as const
}
/>
+ {/* Cost/Tokens toggle lives next to the main tab control so all
+ chart-axis switches are in one place. Only meaningful on the
+ Daily tab — Hourly always renders cost; Heatmap has its own
+ caption. */}
+ {tab === "daily" && (
+ <Segmented
+ value={dailyMetric}
+ onChange={setDailyMetric}
+ options={
+ [
+ { label: t(($) => $.usage.daily_metric_cost), value: "cost" },
+ { label: t(($) => $.usage.daily_metric_tokens), value: "tokens" },
+ ] as const
+ }
+ />
+ )}
</div>
- {tab !== "heatmap" && <ChartLegend />}
+ {tab !== "heatmap" && (
+ <ChartLegend includeCacheRead={tab === "daily" && dailyMetric === "tokens"} />
+ )}
</div>
{/* Heatmap intentionally ignores the page period selector and always
@@ -317,7 +341,14 @@ function WhenChart({
switching never collapses or stretches the card vertically (and
the right-rail / lower sections never reflow as a side effect). */}
<div className="min-h-[260px]">
- {tab === "daily" && <DailyTab data={dailyCostStack} usage={filtered} />}
+ {tab === "daily" && (
+ <DailyTab
+ metric={dailyMetric}
+ costData={dailyCostStack}
+ tokensData={dailyTokens}
+ usage={filtered}
+ />
+ )}
{tab === "hourly" && <HourlyTab data={hourlyCost} usage={filtered} />}
{tab === "heatmap" && <ActivityHeatmap usage={usage} />}
</div>
@@ -326,15 +357,30 @@ function WhenChart({
}
function DailyTab({
- data,
+ metric,
+ costData,
+ tokensData,
usage,
}: {
- data: { total: number }[];
+ metric: DailyMetric;
+ costData: Parameters<typeof DailyCostChart>[0]["data"];
+ tokensData: Parameters<typeof DailyTokensChart>[0]["data"];
usage: RuntimeUsage[];
}) {
- const totalCost = data.reduce((s, d) => s + d.total, 0);
+ if (metric === "tokens") {
+ // Token chart fires its own empty state: if no tokens were recorded the
+ // chart is genuinely empty (independent of pricing — unmapped models
+ // still contribute raw token counts).
+ const totalTokens = tokensData.reduce(
+ (s, d) => s + d.input + d.output + d.cacheRead + d.cacheWrite,
+ 0,
+ );
+ if (totalTokens === 0) return <EmptyChartState usage={usage} />;
+ return <DailyTokensChart data={tokensData} />;
+ }
+ const totalCost = costData.reduce((s, d) => s + d.total, 0);
if (totalCost === 0) return <EmptyChartState usage={usage} />;
- return <DailyCostChart data={data as Parameters<typeof DailyCostChart>[0]["data"]} />;
+ return <DailyCostChart data={costData} />;
}
function HourlyTab({
@@ -446,11 +492,17 @@ function UnmappedPricingNotice({ usage }: { usage: RuntimeUsage[] }) {
// header so the chart body keeps its full vertical real estate.
// ---------------------------------------------------------------------------
-function ChartLegend() {
+function ChartLegend({ includeCacheRead = false }: { includeCacheRead?: boolean }) {
const { t } = useT("runtimes");
+ // Token-stack mode adds a cache-read pip between output and cache-write to
+ // match the four-segment stack of DailyTokensChart. The cost chart drops
+ // cache-read because at typical pricing it'd be ~0 px tall in the stack.
const items = [
{ label: t(($) => $.usage.legend_input), color: "var(--color-chart-1)" },
{ label: t(($) => $.usage.legend_output), color: "var(--color-chart-2)" },
+ ...(includeCacheRead
+ ? [{ label: t(($) => $.usage.legend_cache_read), color: "var(--color-chart-4)" }]
+ : []),
{ label: t(($) => $.usage.legend_cache_write), color: "var(--color-chart-3)" },
];
return (