File size: 797 Bytes
5f40163 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | /** 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);
}
|