/** Formats a token count for compact human-facing status text. */ export function formatTokenCount(value?: number): string { if (value === undefined || !Number.isFinite(value)) { return "0"; } const safe = Math.max(0, value); if (safe >= 1_000_000) { return `${(safe / 1_000_000).toFixed(1)}m`; } if (safe >= 1_000) { const precision = safe >= 10_000 ? 0 : 1; const formattedThousands = (safe / 1_000).toFixed(precision); if (Number(formattedThousands) >= 1_000) { return `${(safe / 1_000_000).toFixed(1)}m`; } return `${formattedThousands}k`; } return String(Math.round(safe)); }