Files
openclaw/src/utils/token-format.ts
Jayesh Betala e8b35a8280 fix(status): render sub-1000 token counts as plain integers (#89736)
* fix(status): render sub-1000 token counts as plain integers

formatKTokens always divided by 1000 and appended "k", so token counts
below 1000 rendered as misleading fractional k in `openclaw status`
output (e.g. 999 rounded up across the boundary to "1.0k", 420 -> "0.4k",
a 300-token cache write -> "write 0.3k").

Guard value < 1000 to render the plain rounded integer, matching the
canonical formatTokenCount convention (src/utils/usage-format.ts). The
>=1000 "k" behavior is unchanged. Adds focused regression tests for the
0/420/999/1000/12000 boundary and small-session/small-cache status lines.

Fixes #89735

* fix(status): reuse canonical token formatter

* refactor(status): extract lightweight token formatter

---------

Co-authored-by: Vincent Koc <25068+vincentkoc@users.noreply.github.com>
2026-06-14 10:24:42 +08:00

20 lines
632 B
TypeScript

/** 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));
}