Download src/utils/format-token-count.ts from SaylorTwift/openhands: direct link, hf CLI and curl.
- Browser
- Download file 797 Bytes
-
https://huggingface.co/SaylorTwift/openhands/resolve/main/src/utils/format-token-count.ts
- Command line
-
hf download hf://SaylorTwift/openhands/src/utils/format-token-count.ts
-
curl -L -o format-token-count.ts https://huggingface.co/SaylorTwift/openhands/resolve/main/src/utils/format-token-count.ts
797 Bytes
| /** Compact token counts for context-window UI (e.g. 198.5k, 1.0M). */ | |
| export function formatCompactTokenCount(value: number): string { | |
| if (value >= 1_000_000) { | |
| const millions = value / 1_000_000; | |
| if (millions >= 10 && Number.isInteger(millions)) { | |
| return `${millions.toFixed(0)}M`; | |
| } | |
| return `${millions.toFixed(1)}M`; | |
| } | |
| if (value >= 1_000) { | |
| const thousands = value / 1_000; | |
| if (Number.isInteger(thousands)) { | |
| return `${thousands.toFixed(0)}k`; | |
| } | |
| return `${thousands.toFixed(1)}k`; | |
| } | |
| return value.toLocaleString(); | |
| } | |
| export function getContextWindowUsagePercentage( | |
| perTurnToken: number, | |
| contextWindow: number, | |
| ): number { | |
| if (contextWindow <= 0) { | |
| return 0; | |
| } | |
| return Math.min(100, (perTurnToken / contextWindow) * 100); | |
| } | |