1- import type { UsageProviderKind } from "@t3tools/contracts" ;
1+ import type { UsagePricingStatus , UsageProviderKind } from "@t3tools/contracts" ;
22import { CheckIcon , RefreshCwIcon , XIcon } from "lucide-react" ;
33import { 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 */
479502function 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 ) ) }
0 commit comments