Skip to content

Commit c989e51

Browse files
wezzcoetzeeclaude
andcommitted
fix(web): usage page says when model rates are unavailable
The usage page renders $0.00 across every model when the LiteLLM rate table fails to load, indistinguishable from having spent nothing. The server already reports why - UsageSummary.pricing.status and per-bucket unpricedRecords - but no client reads either field, so a total pricing failure looked like a real answer. mergeUsage now derives pricingStatus from the environments that contributed buckets, and UsageCoverageNotice states when rates could not be loaded and what share of responses that excluded from cost. The share comes from unpricedShare rather than an assumption, so the message is also correct for the partial case where a single new model is missing from the table. No contract change. cached is intentionally silent. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
1 parent 3d74474 commit c989e51

3 files changed

Lines changed: 114 additions & 8 deletions

File tree

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

Lines changed: 37 additions & 6 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

@@ -137,6 +137,9 @@ export function UsagePage() {
137137
environments={environments}
138138
duplicateSources={merged.duplicateSources}
139139
staleEnvironments={merged.staleEnvironments}
140+
pricingStatus={merged.pricingStatus}
141+
unpricedShare={merged.costQuality.unpricedShare}
142+
records={merged.records}
140143
/>
141144

142145
{/* Cost first: the financial answer, then the provider split. */}
@@ -407,25 +410,52 @@ function Metric({
407410
}
408411

409412
/**
410-
* Says plainly when the totals are incomplete: an environment that failed, or
411-
* one whose transcripts another environment already reported. Environments
412-
* that are still answering never reach this notice; the page shows the
413-
* loading skeleton until every one is terminal.
413+
* Says plainly when the totals are incomplete: an environment that failed, one
414+
* whose transcripts another environment already reported, or a rate table that
415+
* never loaded. Environments that are still answering never reach this notice;
416+
* the page shows the loading skeleton until every one is terminal.
414417
*/
415418
function UsageCoverageNotice({
416419
environments,
417420
duplicateSources,
418421
staleEnvironments,
422+
pricingStatus,
423+
unpricedShare,
424+
records,
419425
}: {
420426
readonly environments: readonly EnvironmentUsageStatus[];
421427
readonly duplicateSources: readonly string[];
422428
readonly staleEnvironments: readonly string[];
429+
readonly pricingStatus: UsagePricingStatus;
430+
readonly unpricedShare: number;
431+
readonly records: number;
423432
}) {
424433
const failed = environments.filter((environment) => environment.error !== null);
425434
const stale = environments.filter((environment) =>
426435
staleEnvironments.includes(environment.environmentId),
427436
);
428-
if (failed.length === 0 && stale.length === 0 && duplicateSources.length === 0) {
437+
// Rate provenance only matters once there is something to price, and only
438+
// `unavailable` is worth a line: stale rates move cost by rounding, not by
439+
// orders of magnitude. `unpricedShare` carries the magnitude either way, so
440+
// the message never has to guess how much cost went missing.
441+
//
442+
// The failure and the share are stated as separate facts rather than cause
443+
// and effect. They are aggregated independently, so across several devices
444+
// the environment whose rates failed need not be the one that contributed
445+
// the unpriced records.
446+
const pricingNotice =
447+
records === 0 || unpricedShare === 0
448+
? null
449+
: pricingStatus === "unavailable"
450+
? `Model rates could not be loaded. ${formatPercent(unpricedShare)} of responses are excluded from cost. Token counts are unaffected.`
451+
: `${formatPercent(unpricedShare)} of responses used a model with no published rate and are excluded from cost.`;
452+
453+
if (
454+
failed.length === 0 &&
455+
stale.length === 0 &&
456+
duplicateSources.length === 0 &&
457+
pricingNotice === null
458+
) {
429459
return null;
430460
}
431461

@@ -445,6 +475,7 @@ function UsageCoverageNotice({
445475
{duplicateSources.join(", ")}
446476
</span>
447477
) : null}
478+
{pricingNotice === null ? null : <span>{pricingNotice}</span>}
448479
</div>
449480
);
450481
}

‎packages/shared/src/usageMerge.test.ts‎

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
type EnvironmentId,
44
type UsageBucket,
55
type UsageDay,
6+
type UsagePricingStatus,
67
type UsageProviderKind,
78
type UsageSummary,
89
} from "@t3tools/contracts";
@@ -42,6 +43,7 @@ function summary(
4243
distinctSessions?: number;
4344
}[],
4445
contractVersion: number = USAGE_CONTRACT_VERSION,
46+
pricingStatus: UsagePricingStatus = "fresh",
4547
): UsageSummary {
4648
return {
4749
contractVersion,
@@ -64,7 +66,7 @@ function summary(
6466
distinctSessions: source.distinctSessions ?? 1,
6567
message: null,
6668
})),
67-
pricing: { status: "fresh", source: "litellm", fetchedAt: null, knownModels: 10 },
69+
pricing: { status: pricingStatus, source: "litellm", fetchedAt: null, knownModels: 10 },
6870
scanDurationMs: 1,
6971
};
7072
}
@@ -189,6 +191,49 @@ describe("mergeUsage", () => {
189191
expect(merged.costQuality.cacheSavingsUsd).toBe(4);
190192
});
191193

194+
it("reports the weakest rate table among contributing environments", () => {
195+
// #given one device priced against a live rate table and one that never
196+
// loaded it
197+
const environments = [
198+
environment(
199+
"env-a",
200+
summary([bucket()], [{ provider: "claude", hostId: "mac-a", homePath: "/a/.claude" }]),
201+
),
202+
environment(
203+
"env-b",
204+
summary(
205+
[bucket({ costUsd: 0, costSource: "unpriced", unpricedRecords: 5 })],
206+
[{ provider: "claude", hostId: "mac-b", homePath: "/b/.claude" }],
207+
USAGE_CONTRACT_VERSION,
208+
"unavailable",
209+
),
210+
),
211+
];
212+
213+
// #when the summaries are merged
214+
const merged = mergeUsage(environments, USAGE_CONTRACT_VERSION);
215+
216+
// #then the merged cost is an undercount and says so
217+
expect(merged.pricingStatus).toBe("unavailable");
218+
});
219+
220+
it("ignores the rate table of an environment that contributed no buckets", () => {
221+
// #given an idle second device whose rate table failed to load
222+
const environments = [
223+
environment(
224+
"env-a",
225+
summary([bucket()], [{ provider: "claude", hostId: "mac-a", homePath: "/a/.claude" }]),
226+
),
227+
environment("env-b", summary([], [], USAGE_CONTRACT_VERSION, "unavailable")),
228+
];
229+
230+
// #when the summaries are merged
231+
const merged = mergeUsage(environments, USAGE_CONTRACT_VERSION);
232+
233+
// #then it cannot have skewed a cost it did not contribute to
234+
expect(merged.pricingStatus).toBe("fresh");
235+
});
236+
192237
it("keeps two machines apart when hostname and home path collide", () => {
193238
// Every Mac resolves /Users/theo/.claude, so a hostname clash used to make
194239
// one machine's usage vanish. Filesystem identity separates them.

‎packages/shared/src/usageMerge.ts‎

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import type {
1010
EnvironmentId,
1111
UsageBucket,
12+
UsagePricingStatus,
1213
UsageProviderKind,
1314
UsageSourceFingerprint,
1415
UsageSummary,
@@ -54,6 +55,12 @@ export interface CostQuality {
5455

5556
export interface MergedUsage {
5657
readonly costUsd: number;
58+
/**
59+
* Weakest rate-table provenance among the environments that contributed. A
60+
* single environment without rates makes the merged cost an undercount, so
61+
* the pessimistic status is the honest one to report.
62+
*/
63+
readonly pricingStatus: UsagePricingStatus;
5764
readonly uncachedInputTokens: number;
5865
readonly cachedInputTokens: number;
5966
readonly cacheCreationTokens: number;
@@ -155,8 +162,15 @@ function bucketTokens(bucket: UsageBucket): number {
155162
);
156163
}
157164

165+
const PRICING_STATUS_RANK: Record<UsagePricingStatus, number> = {
166+
unavailable: 0,
167+
cached: 1,
168+
fresh: 2,
169+
};
170+
158171
const EMPTY_MERGED: MergedUsage = {
159172
costUsd: 0,
173+
pricingStatus: "unavailable",
160174
uncachedInputTokens: 0,
161175
cachedInputTokens: 0,
162176
cacheCreationTokens: 0,
@@ -233,13 +247,17 @@ export function mergeUsage(
233247
}
234248
>();
235249
const contributingEnvironments: EnvironmentId[] = [];
250+
const contributingPricingStatuses: UsagePricingStatus[] = [];
236251

237252
for (const environment of current) {
238253
const { buckets, sessions: environmentSessions } = ownedContribution(
239254
environment,
240255
ownerByFingerprint,
241256
);
242-
if (buckets.length > 0) contributingEnvironments.push(environment.environmentId);
257+
if (buckets.length > 0) {
258+
contributingEnvironments.push(environment.environmentId);
259+
contributingPricingStatuses.push(environment.summary.pricing.status);
260+
}
243261
sessions += environmentSessions;
244262

245263
for (const bucket of buckets) {
@@ -326,8 +344,20 @@ export function mergeUsage(
326344
}))
327345
.sort((a, b) => a.day.localeCompare(b.day));
328346

347+
// An environment that contributed nothing cannot have skewed cost, so its rate
348+
// table is not evidence about the numbers on screen.
349+
const pricingStatuses =
350+
contributingPricingStatuses.length > 0
351+
? contributingPricingStatuses
352+
: current.map((environment) => environment.summary.pricing.status);
353+
329354
return {
330355
costUsd,
356+
pricingStatus: pricingStatuses.reduce<UsagePricingStatus>(
357+
(weakest, status) =>
358+
PRICING_STATUS_RANK[status] < PRICING_STATUS_RANK[weakest] ? status : weakest,
359+
"fresh",
360+
),
331361
uncachedInputTokens,
332362
cachedInputTokens,
333363
cacheCreationTokens,

0 commit comments

Comments
 (0)