formatTokenCount (usage/usage-format.ts)

kimi-code · apps/kimi-code/src/utils/usage/usage-format.ts · typescript · MIT
ui, tag:knn-classified

Pure utilities for formatting token counts, usage percentages, and progress bars in usage reports.

// Source: https://github.com/MoonshotAI/kimi-code | License: MIT | Copyright 2026 Moonshot AI
// Provided via justclaw.it.com; keep this notice with the code.
/**
 * Formatting helpers for the `/usage` slash command.
 *
 * Kept pure + ANSI-free so they're trivial to unit-test; the slash
 * command itself chalks the colour afterwards.
 */

import { type ManagedQuota, type ManagedQuotaEntry } from '@moonshot-ai/kimi-code-oauth';

/**
 * Format a token count in 1024-based units: context sizes are powers of
 * two, so 262144 reads as "256k", not "262.1k". k values at or above
 * 100 are rounded to whole numbers ("977k").
 */
export function formatTokenCount(n: number): string {
  if (!Number.isFinite(n) || n < 0) return '0';
  if (n >= 1024 * 1024) return `${trimDecimal(n / (1024 * 1024))}M`;

... (truncated -- full source via MCP)

See the full source, get the GitHub permalink, and search for more like it.

Get a free API key