Enterprise agent platform for building, deploying, and operating AI teammates.
import React, { useEffect, useState } from 'react';
import { cn, getApiUrl } from '@/lib/utils';
import { apiRequest } from '@/lib/api-wrapper';
import { ChevronDown, Sparkles } from 'lucide-react';
import { useI18n, type Locale } from '@/contexts/i18n-context';
import { Popover, PopoverContent, PopoverTrigger } from '@/components/ui/popover';
interface TokenUsageDisplayProps {
taskId: number | null;
isRunning: boolean;
className?: string;
}
interface TokenUsage {
input_tokens: number;
output_tokens: number;
total_tokens: number;
llm_calls: number;
cached_input_tokens: number;
model_usage: ModelTokenUsage[];
media_usage: MediaUsage[];
}
interface ModelTokenUsage {
model_id: string;
model_name: string;
input_tokens: number;
output_tokens: number;
cached_input_tokens?: number;
}
interface MediaUsage {
model_id: string;
model_name: string;
unit: string;
call_type: string;
resolution?: string;
quantity: number;
calls: number;
provider_tokens?: number;
tokens_estimated?: boolean;
}
/**
* Coerce one raw media row into a fully-typed MediaUsage, or drop it.
*
* `token_usage_details` is free-form legacy JSON, so a row can be null, a
* non-object, or carry a numeric field as a string. Both matter here: reducing
* over a null row throws on property access, and a string `quantity` like "4"
* passes a `> 0` check while failing `Number.isFinite` in the formatter, so it
* silently renders "0 sec" instead of the real value. Normalising once at the
* fetch boundary keeps every consumer below dealing in real numbers.
*/
function normalizeMediaUsage(raw: unknown): MediaUsage | null {
// Arrays are typeof 'object' too, and would otherwise normalise into a bogus
// all-zero row rather than being dropped.
if (typeof raw !== 'object' || raw === null || Array.isArray(raw)) return null;
const row = raw as Record<string, unknown>;
const num = (value: unknown): number => {
const parsed = typeof value === 'string' ? Number(value) : value;
return typeof parsed === 'number' && Number.isFinite(parsed) ? Math.max(0, parsed) : 0;
};
const str = (value: unknown): string => (typeof value === 'string' ? value : '');
return {
model_id: str(row.model_id),
model_name: str(row.model_name),
unit: str(row.unit),
call_type: str(row.call_type),
resolution: str(row.resolution),
quantity: num(row.quantity),
calls: num(row.calls),
provider_tokens: num(row.provider_tokens),
tokens_estimated: row.tokens_estimated === true,
};
}
// Build formatters lazily per locale so this file stays valid regardless of the
// locale set (the SaaS overlay adds more locales than the standalone build).
const compactTokenFormatters = new Map<Locale, Intl.NumberFormat>();
const exactTokenFormatters = new Map<Locale, Intl.NumberFormat>();
const mediaQuantityFormatters = new Map<Locale, Intl.NumberFormat>();
function getFormatter(
cache: Map<Locale, Intl.NumberFormat>,
locale: Locale,
options?: Intl.NumberFormatOptions,
): Intl.NumberFormat {
let formatter = cache.get(locale);
if (!formatter) {
formatter = new Intl.NumberFormat(locale, options);
cache.set(locale, formatter);
}
return formatter;
}
function normalizeTokenCount(value: number): number {
return Number.isFinite(value) ? Math.max(0, Math.trunc(value)) : 0;
}
export function formatTokenCount(value: number, locale: Locale = 'en'): string {
return getFormatter(compactTokenFormatters, locale, {
notation: 'compact',
compactDisplay: 'short',
maximumFractionDigits: 2,
})
.format(normalizeTokenCount(value))
.toLowerCase();
}
export function formatExactTokenCount(value: number, locale: Locale = 'en'): string {
return getFormatter(exactTokenFormatters, locale).format(normalizeTokenCount(value));
}
function formatMediaQuantity(value: number, locale: Locale = 'en'): string {
const safe = Number.isFinite(value) ? Math.max(0, value) : 0;
// Media quantities can be fractional (e.g. audio seconds); keep up to one
// decimal but drop trailing ".0" for whole counts like image counts.
//
// A positive quantity below the rounding threshold must not render as "0":
// the caller already decided this row is measured (quantity > 0), so a bare
// "0" contradicts that and reads as "this cost nothing". Show "<0.1" instead
// — the exact value is not meaningful at that scale, but "some, but less
// than a tenth" is. Exact zero never reaches here; it takes the unmeasured
// branch at the call site.
if (safe > 0 && safe < 0.05) {
return `<${getFormatter(mediaQuantityFormatters, locale, {
maximumFractionDigits: 1,
}).format(0.1)}`;
}
return getFormatter(mediaQuantityFormatters, locale, {
maximumFractionDigits: 1,
}).format(safe);
}
export function TokenUsageDisplay({ taskId, isRunning, className }: TokenUsageDisplayProps) {
const [usage, setUsage] = useState<TokenUsage | null>(null);
const { locale, t, tDynamic } = useI18n();
useEffect(() => {
if (!taskId) return;
let isMounted = true;
let intervalId: NodeJS.Timeout;
const fetchUsage = async () => {
try {
const response = await apiRequest(`${getApiUrl()}/api/chat/task/${taskId}`);
if (response.ok && isMounted) {
const data = await response.json();
setUsage({
input_tokens: data.input_tokens || 0,
output_tokens: data.output_tokens || 0,
total_tokens: data.total_tokens || 0,
llm_calls: data.llm_calls || 0,
cached_input_tokens: data.cached_input_tokens || 0,
model_usage: Array.isArray(data.model_usage) ? data.model_usage : [],
// `data` is `any` from response.json(), so annotate explicitly:
// without it the filter callback's parameter is implicitly `any`
// and the build fails under noImplicitAny.
media_usage: ((Array.isArray(data.media_usage)
? data.media_usage
: []) as unknown[])
.map(normalizeMediaUsage)
.filter((row: MediaUsage | null): row is MediaUsage => row !== null),
});
}
} catch (error) {
console.error('Failed to fetch token usage:', error);
}
};
// Initial fetch
fetchUsage();
// Poll if running
if (isRunning) {
intervalId = setInterval(fetchUsage, 15000); // Poll every 15 seconds
}
return () => {
isMounted = false;
if (intervalId) clearInterval(intervalId);
};
}, [taskId, isRunning]);
if (!usage) return null;
const attributedModelCount = usage.model_usage.filter((model) => Boolean(model.model_id)).length;
const unattributedUsageCount = usage.model_usage.length - attributedModelCount;
const modelUsageLabel = attributedModelCount === 0
? t('chatPage.tokenUsage.unattributedCount', { count: unattributedUsageCount })
: unattributedUsageCount > 0
? t(
attributedModelCount === 1
? 'chatPage.tokenUsage.oneModelWithUnattributed'
: 'chatPage.tokenUsage.modelsWithUnattributed',
{ count: attributedModelCount, unattributed: unattributedUsageCount },
)
: t(
attributedModelCount === 1
? 'chatPage.tokenUsage.oneModel'
: 'chatPage.tokenUsage.models',
{ count: attributedModelCount },
);
// Rows are normalised at the fetch boundary, so calls is always a finite
// non-negative number here.
const totalMediaCalls = usage.media_usage.reduce((sum, media) => sum + media.calls, 0);
const mediaUsageLabel = t(
totalMediaCalls === 1
? 'chatPage.tokenUsage.mediaCall'
: 'chatPage.tokenUsage.mediaCalls',
{ count: totalMediaCalls },
);
const formatMediaType = (callType: string) =>
callType
? tDynamic(
`chatPage.tokenUsage.mediaType.${callType}`,
callType,
)
: '-';
// Plural form is chosen by the raw quantity, not the formatted string: a
// sub-threshold value renders as "<0.1" yet is grammatically plural, and
// locales without plural distinction just carry the same text in both keys.
const formatMediaUnit = (unit: string, quantity: number) =>
unit
? tDynamic(
`chatPage.tokenUsage.unit.${unit}.${quantity === 1 ? 'one' : 'other'}`,
unit,
)
: '';
const renderDetails = (inPopover: boolean) => (
<div className={cn(
'flex-wrap items-center gap-x-3 gap-y-1 px-3 py-2 text-xs sm:text-sm',
inPopover ? 'flex w-full' : 'inline-flex rounded-xl border bg-card/80',
!inPopover && className,
)}>
<span className="flex items-center gap-1.5 whitespace-nowrap">
<Sparkles className="w-4 h-4 text-indigo-500" />
<span className="font-medium text-foreground" title={formatExactTokenCount(usage.input_tokens, locale)}>
{formatTokenCount(usage.input_tokens, locale)}
</span>
<span
className="text-muted-foreground"
title={t('chatPage.tokenUsage.input')}
>
{t('chatPage.tokenUsage.inputShort')}
</span>
{usage.cached_input_tokens > 0 && usage.input_tokens > 0 && (
<span
className="text-muted-foreground"
title={`${t('chatPage.tokenUsage.cached')}: ${formatExactTokenCount(usage.cached_input_tokens, locale)}`}
>
{t('chatPage.tokenUsage.cachedShare', {
pct: Math.min(
100,
Math.round((usage.cached_input_tokens / usage.input_tokens) * 100),
),
})}
</span>
)}
</span>
<span className="flex items-center gap-1.5 whitespace-nowrap">
<span className="font-medium text-foreground" title={formatExactTokenCount(usage.output_tokens, locale)}>
{formatTokenCount(usage.output_tokens, locale)}
</span>
<span
className="text-muted-foreground"
title={t('chatPage.tokenUsage.output')}
>
{t('chatPage.tokenUsage.outputShort')}
</span>
</span>
{usage.model_usage.length > 0 && (
<Popover>
<PopoverTrigger asChild>
<button
type="button"
className="flex items-center gap-1 whitespace-nowrap rounded-md px-1.5 py-0.5 text-muted-foreground transition-colors hover:bg-muted hover:text-foreground"
>
<span>{modelUsageLabel}</span>
<ChevronDown className="h-3.5 w-3.5" />
</button>
</PopoverTrigger>
<PopoverContent
align="end"
className="w-[32rem] max-w-[calc(100vw-2rem)] max-h-[60dvh] overflow-auto p-0"
>
<div className="border-b px-3 py-2.5 text-sm font-medium">
{t('chatPage.tokenUsage.byModel')}
</div>
<div className="grid min-w-[28rem] grid-cols-[minmax(0,1fr)_5rem_5rem_5rem] gap-x-4 gap-y-2 p-3 text-xs">
<span className="text-muted-foreground">{t('chatPage.tokenUsage.model')}</span>
<span className="text-right text-muted-foreground">
{t('chatPage.tokenUsage.inputShort')}
</span>
<span
className="text-right text-muted-foreground"
title={t('chatPage.tokenUsage.cached')}
>
{t('chatPage.tokenUsage.cachedShort')}
</span>
<span className="text-right text-muted-foreground">
{t('chatPage.tokenUsage.outputShort')}
</span>
{usage.model_usage.map((model) => (
<React.Fragment key={JSON.stringify([model.model_id, model.model_name])}>
<span className="min-w-0" title={model.model_name || model.model_id}>
<span className="block truncate font-medium">
{model.model_name || model.model_id || t('chatPage.tokenUsage.unknownModel')}
</span>
{model.model_id && model.model_name && model.model_id !== model.model_name && (
<span className="block truncate text-[10px] text-muted-foreground">
{model.model_id}
</span>
)}
{!model.model_id && model.model_name && (
<span className="block truncate text-[10px] text-muted-foreground">
{t('chatPage.tokenUsage.unattributed')}
</span>
)}
</span>
<span className="text-right tabular-nums" title={formatExactTokenCount(model.input_tokens, locale)}>
{formatTokenCount(model.input_tokens, locale)}
</span>
<span
className="text-right tabular-nums text-muted-foreground"
title={formatExactTokenCount(model.cached_input_tokens ?? 0, locale)}
>
{formatTokenCount(model.cached_input_tokens ?? 0, locale)}
</span>
<span className="text-right tabular-nums" title={formatExactTokenCount(model.output_tokens, locale)}>
{formatTokenCount(model.output_tokens, locale)}
</span>
</React.Fragment>
))}
</div>
</PopoverContent>
</Popover>
)}
{usage.media_usage.length > 0 && (
<Popover>
<PopoverTrigger asChild>
<button
type="button"
className="flex items-center gap-1 whitespace-nowrap rounded-md px-1.5 py-0.5 text-muted-foreground transition-colors hover:bg-muted hover:text-foreground"
>
<span>{mediaUsageLabel}</span>
<ChevronDown className="h-3.5 w-3.5" />
</button>
</PopoverTrigger>
<PopoverContent
align="end"
className="w-[32rem] max-w-[calc(100vw-2rem)] max-h-[60dvh] overflow-auto p-0"
>
<div className="border-b px-3 py-2.5 text-sm font-medium">
{t('chatPage.tokenUsage.mediaByModel')}
</div>
<div className="grid min-w-[24rem] grid-cols-[minmax(0,1fr)_7rem_6rem] gap-x-4 gap-y-2 p-3 text-xs">
<span className="text-muted-foreground">{t('chatPage.tokenUsage.model')}</span>
<span className="text-muted-foreground">{t('chatPage.tokenUsage.callType')}</span>
<span className="text-right text-muted-foreground">
{t('chatPage.tokenUsage.quantity')}
</span>
{usage.media_usage.map((media) => (
<React.Fragment
key={JSON.stringify([
media.model_id,
media.model_name,
media.unit,
media.call_type,
media.resolution ?? "",
])}
>
<span className="min-w-0" title={media.model_name || media.model_id}>
<span className="block truncate font-medium">
{media.model_name || media.model_id || t('chatPage.tokenUsage.unknownModel')}
</span>
{/* Same secondary-identity rule as the LLM rows above: the
backend groups media by `model_id or model_name`, so two
distinct configured IDs sharing one provider-facing name
are separate billable rows. Without the id they render
identically and look like a duplicate. */}
{media.model_id && media.model_name && media.model_id !== media.model_name && (
<span className="block truncate text-[10px] text-muted-foreground">
{media.model_id}
</span>
)}
{media.resolution && (
<span className="block truncate text-[10px] text-muted-foreground">
{media.resolution}
</span>
)}
</span>
<span className="min-w-0 truncate" title={formatMediaType(media.call_type)}>
{formatMediaType(media.call_type)}
</span>
<span className="text-right tabular-nums">
{/* quantity 0 means the call happened but the provider
reported no measurable size (e.g. an async video with
no duration yet) — say so rather than showing "0 sec",
which reads as "this cost nothing". */}
{media.quantity > 0
? /* Joined via filter so an unrecognised (or empty) unit
renders as "4" rather than "4 " with a trailing space. */
[
formatMediaQuantity(media.quantity, locale),
formatMediaUnit(media.unit, media.quantity),
]
.filter(Boolean)
.join(' ')
: t('chatPage.tokenUsage.unmeasured')}
{media.provider_tokens ? (
<span className="block text-[10px] text-muted-foreground">
{formatTokenCount(media.provider_tokens, locale)}
{media.tokens_estimated ? '~' : ''}
{' '}
{t('chatPage.tokenUsage.tokensShort')}
</span>
) : null}
</span>
</React.Fragment>
))}
</div>
</PopoverContent>
</Popover>
)}
</div>
);
const summaryLabel = `${t('chatPage.tokenUsage.input')}: ${formatExactTokenCount(usage.input_tokens, locale)}, ${t('chatPage.tokenUsage.output')}: ${formatExactTokenCount(usage.output_tokens, locale)}${totalMediaCalls > 0 ? `, ${mediaUsageLabel}` : ''}`;
return (
<>
<Popover>
<PopoverTrigger asChild>
<button
type="button"
className={`inline-flex h-9 min-w-0 items-center gap-1.5 rounded-xl border bg-card/80 px-2 text-xs hover:bg-muted sm:hidden ${className || ""}`}
title={summaryLabel}
>
<Sparkles className="h-4 w-4 shrink-0 text-indigo-500" />
<span className="truncate font-medium tabular-nums">
{usage.total_tokens > 0 || totalMediaCalls === 0
? `${formatTokenCount(usage.total_tokens, locale)} ${t('chatPage.tokenUsage.tokensShort')}`
: mediaUsageLabel}
</span>
<span className="sr-only"> {t('chatPage.tokenUsage.details')}</span>
<ChevronDown className="h-3.5 w-3.5 shrink-0 text-muted-foreground" />
</button>
</PopoverTrigger>
<PopoverContent side="top" align="end" className="w-80 max-w-[calc(100vw-2rem)] max-h-[60dvh] overflow-y-auto p-0 sm:hidden">
{renderDetails(true)}
</PopoverContent>
</Popover>
<div className="hidden sm:contents">{renderDetails(false)}</div>
</>
);
}