Token Usage Formatting Utilities (utils/tokenUsageFormatting.ts)

OpenBitFun · src/web-ui/src/shared/utils/tokenUsageFormatting.ts · typescript · MIT
ui, analytics, dev-tools, database, lang:typescript, tag:utils, tag:tokens

Formats token counts and cache hit rates into human‑readable strings with locale support.

// Source: https://github.com/GCWing/OpenBitFun | License: MIT | Copyright 2026 CWing
// Provided via justclaw.it.com; keep this notice with the code.
export type LocalizedNumberFormatter = (
  value: number,
  options?: Intl.NumberFormatOptions,
) => string;

const COMPACT_TOKEN_OPTIONS: Intl.NumberFormatOptions = {
  maximumFractionDigits: 2,
};

const TOKEN_UNITS = [
  { divisor: 1_000, suffix: 'K' },
  { divisor: 1_000_000, suffix: 'M' },
  { divisor: 1_000_000_000, suffix: 'B' },
] as const;

const CACHE_HIT_RATE_OPTIONS: Intl.NumberFormatOptions = {
  style: 'percent',
  minimumFractionDigits: 2,
  maximumFractionDigits: 2,
};

/** Format a token count with fixed K/M/B units in every locale. */
export function formatTokenCount(
  value: number,
  formatNumber: LocalizedNumberFormatte

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

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

Get a free API key