MCPcopy Create free account
hub / github.com/OpenHands/OpenHands / formatCompactTokenCount

Function formatCompactTokenCount

src/utils/format-token-count.ts:2–26  ·  view source on GitHub ↗
(value: number)

Source from the content-addressed store, hash-verified

1/** Compact token counts for context-window UI (e.g. 198.5k, 1.0M). */
2export function formatCompactTokenCount(value: number): string {
3 if (value >= 1_000_000) {
4 const millions = value / 1_000_000;
5 if (millions >= 10 && Number.isInteger(millions)) {
6 return `${millions.toFixed(0)}M`;
7 }
8 return `${millions.toFixed(1)}M`;
9 }
10
11 if (value >= 1_000) {
12 const thousands = value / 1_000;
13 if (Number.isInteger(thousands)) {
14 return `${thousands.toFixed(0)}k`;
15 }
16 const rounded = thousands.toFixed(1);
17 // Rounding can carry the value into the next unit (999_999 -> "1000.0k"),
18 // which reads as a four-digit count. Promote it to M instead.
19 if (Number(rounded) >= 1_000) {
20 return `${(Number(rounded) / 1_000).toFixed(1)}M`;
21 }
22 return `${rounded}k`;
23 }
24
25 return value.toLocaleString();
26}
27
28export function getContextWindowUsagePercentage(
29 perTurnToken: number,

Callers 3

ContextWindowMeterFunction · 0.90
useCompactContextActionFunction · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected