Skip to content

Commit 5cfd468

Browse files
committed
fix(usage): say when cost is unavailable instead of $0.00
When the LiteLLM rate table cannot be loaded, the usage page already receives pricing.status=unavailable but still rendered $0.00. Show "Cost unavailable" for dollar columns and explain that token counts remain valid.
1 parent d23b181 commit 5cfd468

8 files changed

Lines changed: 247 additions & 49 deletions

File tree

‎apps/mobile/src/features/usage/UsageRouteScreen.tsx‎

Lines changed: 45 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
formatHourShort,
99
formatPercent,
1010
formatTokens,
11+
formatUsageCost,
1112
formatUsd,
1213
makeWindow,
1314
} from "@t3tools/shared/usageFormat";
@@ -198,6 +199,7 @@ function ChartCard(props: {
198199
const { merged, metric } = props;
199200
const colors = useProviderColors();
200201
const hasActivity = props.daily.some((period) => period.totalTokens > 0);
202+
const costUnavailable = merged.pricingStatus === "unavailable";
201203

202204
return (
203205
<View className="gap-4 rounded-[24px] border-continuous bg-card p-4">
@@ -207,18 +209,28 @@ function ChartCard(props: {
207209
{metric === "cost" ? "Raw token cost" : "Processed tokens"}
208210
</Text>
209211
<Text className="text-4xl font-t3-bold tabular-nums text-foreground">
210-
{metric === "cost" ? `${formatUsd(merged.costUsd)}*` : formatTokens(merged.totalTokens)}
212+
{metric === "cost"
213+
? costUnavailable
214+
? formatUsageCost(merged.pricingStatus, merged.costUsd)
215+
: `${formatUsd(merged.costUsd)}*`
216+
: formatTokens(merged.totalTokens)}
211217
</Text>
212218
<Text className="text-sm text-foreground-muted">
213219
{metric === "cost"
214-
? "* if billed at full API rate"
220+
? costUnavailable
221+
? "Token counts are still valid."
222+
: "* if billed at full API rate"
215223
: `Across ${formatCount(merged.sessions)} sessions`}
216224
</Text>
217225
</View>
218226
<MetricToggle metric={metric} onChange={props.onMetricChange} />
219227
</View>
220228

221-
{hasActivity ? (
229+
{metric === "cost" && costUnavailable ? (
230+
<View style={{ height: CHART_HEIGHT }} className="items-center justify-center">
231+
<Text className="text-base text-foreground-muted">Cost unavailable</Text>
232+
</View>
233+
) : hasActivity ? (
222234
<UsageDailyChart
223235
days={props.days}
224236
daily={props.daily}
@@ -299,11 +311,12 @@ function ProviderSection(props: {
299311
const { merged, metric } = props;
300312
const colors = useProviderColors();
301313
if (merged.providers.length === 0) return null;
314+
const costUnavailable = merged.pricingStatus === "unavailable";
302315

303316
// Ranked by whatever the toggle is showing, so the rows always descend.
304317
// .sort() on a copy, not .toSorted(): Hermes doesn't ship the ES2023 method.
305318
const ordered = [...merged.providers].sort((a, b) =>
306-
metric === "cost" ? b.costUsd - a.costUsd : b.totalTokens - a.totalTokens,
319+
metric === "cost" && !costUnavailable ? b.costUsd - a.costUsd : b.totalTokens - a.totalTokens,
307320
);
308321

309322
return (
@@ -325,7 +338,7 @@ function ProviderSection(props: {
325338
</View>
326339
<Text className="text-lg tabular-nums text-foreground">
327340
{metric === "cost"
328-
? formatUsd(provider.costUsd)
341+
? formatUsageCost(merged.pricingStatus, provider.costUsd)
329342
: formatTokens(provider.totalTokens)}
330343
</Text>
331344
</View>
@@ -338,8 +351,12 @@ function ProviderSection(props: {
338351
</View>
339352
<Text className="text-sm text-foreground-muted">
340353
{metric === "cost"
341-
? `${formatPercent(share)} of cost · ${formatTokens(provider.totalTokens)} tokens`
342-
: `${formatPercent(share)} of tokens · ${formatUsd(provider.costUsd)}`}
354+
? costUnavailable
355+
? `${formatTokens(provider.totalTokens)} tokens`
356+
: `${formatPercent(share)} of cost · ${formatTokens(provider.totalTokens)} tokens`
357+
: costUnavailable
358+
? `${formatPercent(share)} of tokens`
359+
: `${formatPercent(share)} of tokens · ${formatUsd(provider.costUsd)}`}
343360
</Text>
344361
</View>
345362
);
@@ -367,11 +384,13 @@ function TotalsSection(props: { readonly merged: MergedUsage; readonly isPast24H
367384
/>
368385
<MetricCell
369386
label="Cache savings"
370-
value={formatUsd(merged.costQuality.cacheSavingsUsd)}
387+
value={formatUsageCost(merged.pricingStatus, merged.costQuality.cacheSavingsUsd)}
371388
detail={
372-
merged.costUsd > 0
373-
? `${(merged.costQuality.cacheSavingsUsd / merged.costUsd).toFixed(1)}x the raw cost`
374-
: "vs full input rates"
389+
merged.pricingStatus === "unavailable"
390+
? "requires the rate table"
391+
: merged.costUsd > 0
392+
? `${(merged.costQuality.cacheSavingsUsd / merged.costUsd).toFixed(1)}x the raw cost`
393+
: "vs full input rates"
375394
}
376395
/>
377396
<MetricCell
@@ -438,10 +457,14 @@ function ModelsSection(props: { readonly merged: MergedUsage }) {
438457
{model.model}
439458
</Text>
440459
<Text className="text-sm text-foreground-muted">
441-
{formatPercent(model.costShare)} of cost · {formatTokens(model.totalTokens)} tokens
460+
{merged.pricingStatus === "unavailable"
461+
? formatTokens(model.totalTokens)
462+
: `${formatPercent(model.costShare)} of cost · ${formatTokens(model.totalTokens)} tokens`}
442463
</Text>
443464
</View>
444-
<Text className="text-base tabular-nums text-foreground">{formatUsd(model.costUsd)}</Text>
465+
<Text className="text-base tabular-nums text-foreground">
466+
{formatUsageCost(merged.pricingStatus, model.costUsd)}
467+
</Text>
445468
</View>
446469
))}
447470
</SettingsSection>
@@ -463,17 +486,25 @@ function UsageCoverageNotice(props: {
463486
props.merged.staleEnvironments.includes(environment.environmentId),
464487
);
465488
const duplicateSources = props.merged.duplicateSources;
489+
const costUnavailable = props.merged.pricingStatus === "unavailable";
466490
if (
467491
failed.length === 0 &&
468492
stale.length === 0 &&
469493
duplicateSources.length === 0 &&
470-
!props.isPartial
494+
!props.isPartial &&
495+
!costUnavailable
471496
) {
472497
return null;
473498
}
474499

475500
return (
476501
<View className="gap-1 rounded-[16px] border-continuous bg-card px-4 py-3">
502+
{costUnavailable ? (
503+
<Text className="text-sm text-foreground-muted">
504+
The model rate table could not be loaded, so costs are omitted. Token counts are still
505+
valid.
506+
</Text>
507+
) : null}
477508
{props.isPartial ? (
478509
<Text className="text-sm text-foreground-muted">
479510
Some environments are still reporting. Totals are partial.

‎apps/web/src/components/usage/UsagePage.tsx‎

Lines changed: 68 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { UsageProviderKind } from "@t3tools/contracts";
1+
import type { UsagePricingStatus, UsageProviderKind } from "@t3tools/contracts";
22
import { CheckIcon, RefreshCwIcon, XIcon } from "lucide-react";
33
import { useMemo, useState } from "react";
44

@@ -16,6 +16,7 @@ import {
1616
formatHourShort,
1717
formatPercent,
1818
formatTokens,
19+
formatUsageCost,
1920
formatUsd,
2021
makeWindow,
2122
} from "@t3tools/shared/usageFormat";
@@ -66,13 +67,18 @@ export function UsagePage() {
6667
[isPast24Hours, merged.daily, merged.hourly],
6768
);
6869

70+
const costUnavailable = merged.pricingStatus === "unavailable";
71+
const formatCost = (value: number) => formatUsageCost(merged.pricingStatus, value);
72+
6973
// Ranked by whatever the toggle is showing, so the bars always descend.
7074
const orderedProviders = useMemo(
7175
() =>
7276
merged.providers.toSorted((a, b) =>
73-
metric === "cost" ? b.costUsd - a.costUsd : b.totalTokens - a.totalTokens,
77+
metric === "cost" && !costUnavailable
78+
? b.costUsd - a.costUsd
79+
: b.totalTokens - a.totalTokens,
7480
),
75-
[merged.providers, metric],
81+
[costUnavailable, merged.providers, metric],
7682
);
7783

7884
const activePeriods = (isPast24Hours ? merged.hourly : merged.daily).filter(
@@ -179,6 +185,7 @@ export function UsagePage() {
179185
environments={environments}
180186
duplicateSources={merged.duplicateSources}
181187
staleEnvironments={merged.staleEnvironments}
188+
pricingStatus={merged.pricingStatus}
182189
/>
183190

184191
{/* Cost first: the financial answer, then the provider split. */}
@@ -192,12 +199,16 @@ export function UsagePage() {
192199
</span>
193200
<span className="text-4xl font-semibold text-foreground tabular-nums">
194201
{metric === "cost"
195-
? `${formatUsd(merged.costUsd)}*`
202+
? costUnavailable
203+
? formatCost(merged.costUsd)
204+
: `${formatUsd(merged.costUsd)}*`
196205
: formatTokens(merged.totalTokens)}
197206
</span>
198207
<span className="text-xs text-muted-foreground">
199208
{metric === "cost"
200-
? "* if billed at full API rate"
209+
? costUnavailable
210+
? "Token counts are still valid."
211+
: "* if billed at full API rate"
201212
: `Input, cache reads and output across ${formatCount(merged.sessions)} sessions.`}
202213
</span>
203214
</div>
@@ -213,7 +224,7 @@ export function UsagePage() {
213224
</span>
214225
<span className="text-sm text-foreground tabular-nums">
215226
{metric === "cost"
216-
? formatUsd(provider.costUsd)
227+
? formatCost(provider.costUsd)
217228
: formatTokens(provider.totalTokens)}
218229
</span>
219230
</div>
@@ -228,8 +239,12 @@ export function UsagePage() {
228239
</div>
229240
<span className="text-xs text-muted-foreground">
230241
{metric === "cost"
231-
? `${formatPercent(share)} of cost · ${formatTokens(provider.totalTokens)} tokens`
232-
: `${formatPercent(share)} of tokens · ${formatUsd(provider.costUsd)}`}
242+
? costUnavailable
243+
? `${formatTokens(provider.totalTokens)} tokens`
244+
: `${formatPercent(share)} of cost · ${formatTokens(provider.totalTokens)} tokens`
245+
: costUnavailable
246+
? `${formatPercent(share)} of tokens`
247+
: `${formatPercent(share)} of tokens · ${formatUsd(provider.costUsd)}`}
233248
</span>
234249
</div>
235250
);
@@ -263,16 +278,22 @@ export function UsagePage() {
263278
<UsageChartLegend />
264279
</div>
265280
</div>
266-
<UsageProviderChart
267-
days={days}
268-
daily={merged.daily}
269-
hours={hours}
270-
hourly={merged.hourly}
271-
metric={metric}
272-
referenceTime={window.untilTime}
273-
resolution={isPast24Hours ? "hour" : "day"}
274-
timeZone={window.timeZone}
275-
/>
281+
{metric === "cost" && costUnavailable ? (
282+
<div className="flex h-56 items-center justify-center text-sm text-muted-foreground">
283+
Cost unavailable
284+
</div>
285+
) : (
286+
<UsageProviderChart
287+
days={days}
288+
daily={merged.daily}
289+
hours={hours}
290+
hourly={merged.hourly}
291+
metric={metric}
292+
referenceTime={window.untilTime}
293+
resolution={isPast24Hours ? "hour" : "day"}
294+
timeZone={window.timeZone}
295+
/>
296+
)}
276297
</div>
277298
</section>
278299

@@ -299,11 +320,13 @@ export function UsagePage() {
299320
/>
300321
<Metric
301322
label="Cache savings"
302-
value={formatUsd(merged.costQuality.cacheSavingsUsd)}
323+
value={formatCost(merged.costQuality.cacheSavingsUsd)}
303324
detail={
304-
merged.costUsd > 0
305-
? `${(merged.costQuality.cacheSavingsUsd / merged.costUsd).toFixed(1)}x the raw token cost`
306-
: "vs full input rates"
325+
costUnavailable
326+
? "requires the rate table"
327+
: merged.costUsd > 0
328+
? `${(merged.costQuality.cacheSavingsUsd / merged.costUsd).toFixed(1)}x the raw token cost`
329+
: "vs full input rates"
307330
}
308331
/>
309332
</section>
@@ -365,10 +388,10 @@ export function UsagePage() {
365388
</span>
366389
</td>
367390
<td className="py-2 text-right text-foreground tabular-nums">
368-
{formatUsd(model.costUsd)}
391+
{formatCost(model.costUsd)}
369392
</td>
370393
<td className="py-2 text-right text-muted-foreground tabular-nums">
371-
{formatPercent(model.costShare)}
394+
{costUnavailable ? "—" : formatPercent(model.costShare)}
372395
</td>
373396
<td className="py-2 text-right text-muted-foreground tabular-nums">
374397
{formatTokens(model.totalTokens)}
@@ -415,11 +438,11 @@ export function UsagePage() {
415438
key={provider}
416439
className="py-2 text-right text-muted-foreground tabular-nums"
417440
>
418-
{formatUsd(period.byProvider.get(provider)?.costUsd ?? 0)}
441+
{formatCost(period.byProvider.get(provider)?.costUsd ?? 0)}
419442
</td>
420443
))}
421444
<td className="py-2 text-right text-foreground tabular-nums">
422-
{formatUsd(period.costUsd)}
445+
{formatCost(period.costUsd)}
423446
</td>
424447
<td className="py-2 text-right text-muted-foreground tabular-nums">
425448
{formatTokens(period.totalTokens)}
@@ -471,30 +494,44 @@ function Metric({
471494
}
472495

473496
/**
474-
* Says plainly when the totals are incomplete: an environment that failed, or
475-
* one whose transcripts another environment already reported. Environments
476-
* that are still answering never reach this notice; the page shows the
477-
* loading skeleton until every one is terminal.
497+
* Says plainly when the totals are incomplete: an environment that failed,
498+
* one whose transcripts another environment already reported, or a missing
499+
* rate table. Environments that are still answering never reach this notice;
500+
* the page shows the loading skeleton until every one is terminal.
478501
*/
479502
function UsageCoverageNotice({
480503
environments,
481504
duplicateSources,
482505
staleEnvironments,
506+
pricingStatus,
483507
}: {
484508
readonly environments: readonly EnvironmentUsageStatus[];
485509
readonly duplicateSources: readonly string[];
486510
readonly staleEnvironments: readonly string[];
511+
readonly pricingStatus: UsagePricingStatus;
487512
}) {
488513
const failed = environments.filter((environment) => environment.error !== null);
489514
const stale = environments.filter((environment) =>
490515
staleEnvironments.includes(environment.environmentId),
491516
);
492-
if (failed.length === 0 && stale.length === 0 && duplicateSources.length === 0) {
517+
const costUnavailable = pricingStatus === "unavailable";
518+
if (
519+
failed.length === 0 &&
520+
stale.length === 0 &&
521+
duplicateSources.length === 0 &&
522+
!costUnavailable
523+
) {
493524
return null;
494525
}
495526

496527
return (
497528
<div className="flex flex-col gap-1 border border-border px-3 py-2 text-xs text-muted-foreground">
529+
{costUnavailable ? (
530+
<span>
531+
The model rate table could not be loaded, so costs are omitted. Token counts are still
532+
valid.
533+
</span>
534+
) : null}
498535
{failed.map((environment) => (
499536
<span key={environment.label}>{environment.label} could not report usage.</span>
500537
))}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { formatUsageCost } from "@t3tools/shared/usageFormat";
2+
import { describe, expect, it } from "vite-plus/test";
3+
4+
describe("formatUsageCost", () => {
5+
it("does not present a missing rate table as $0.00", () => {
6+
expect(formatUsageCost("unavailable", 0)).toBe("Cost unavailable");
7+
});
8+
9+
it("keeps a genuine zero when rates loaded", () => {
10+
expect(formatUsageCost("fresh", 0)).toBe("$0.00");
11+
});
12+
13+
it("formats a fresh dollar amount", () => {
14+
expect(formatUsageCost("fresh", 12.3)).toBe("$12.30");
15+
});
16+
17+
it("formats a cached dollar amount", () => {
18+
expect(formatUsageCost("cached", 4.5)).toBe("$4.50");
19+
});
20+
});

‎docs/user/usage.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
The Usage page combines Codex and Claude Code activity from your connected environments. It reads
44
the providers' local session history and shows API-equivalent token cost, processed tokens, cache
55
savings, provider shares, and model breakdowns. Subscription billing is separate from the raw token
6-
cost shown here.
6+
cost shown here. If the model rate table cannot be loaded, the page says cost is unavailable
7+
instead of $0.00. Token counts are still valid.
78

89
Use **Past 24h** for an hourly chart covering the exact rolling 24-hour period. The **7 days**,
910
**30 days**, and **90 days** ranges use daily resolution. Cost and token toggles update both the

0 commit comments

Comments
 (0)