Skip to content

Never stop coding. Free MIT AI gateway: one endpoint, 375 providers (150+ free), 1,400+ models Kimi, Claude, GPT, Gemini, GLM, DeepSeek, MiniMax. Works with Claude Code, Codex, Cursor, OpenCode, Cline & Copilot. Quota-aware auto-fallback, RTK+Caveman compression saves 15-95% tokens, MCP/A2A, Desktop/PWA. Built by 900+ contributors

feat: add K/M/B/T cost shortener to prevent UI overflow (#1902)

Paijod7a14cecdeda
diff --git a/src/shared/utils/formatting.ts b/src/shared/utils/formatting.ts
index 6dd941cf2a..30274f3270 100644
--- a/src/shared/utils/formatting.ts
+++ b/src/shared/utils/formatting.ts
@@ -146,6 +146,55 @@ export function formatCost(usd: number | null | undefined): string {
export const fmtCost = formatCost;
+/**
+ * Format a USD cost for display using abbreviated K/M/B/T suffixes.
+ * Sub-cent values show additional precision.
+ * - Values >= 1T are shown as $X.XT
+ * - Values >= 1B are shown as $X.XB
+ * - Values >= 1M are shown as $X.XM
+ * - Values >= 1K are shown as $X.XK
+ * - Otherwise shown as $X.XX
+ * @param {number} usd - Cost in USD
+ * @returns {string}
+ */
+export function formatCostAbbreviated(usd: number | null | undefined): string {
+ const value = Number(usd || 0);
+ if (!Number.isFinite(value) || value === 0) return "$0";
+ const abs = Math.abs(value);
+ if (abs < 0.01) {
+ if (value < 0) {
+ return `-$${Math.abs(value).toFixed(6)}`;
+ }
+ return `$${value.toFixed(6)}`;
+ }
+ let divisor: number, suffix: string;
+ if (abs >= 1_000_000_000_000) {
+ divisor = 1_000_000_000_000;
+ suffix = "T";
+ } else if (abs >= 1_000_000_000) {
+ divisor = 1_000_000_000;
+ suffix = "B";
+ } else if (abs >= 1_000_000) {
+ divisor = 1_000_000;
+ suffix = "M";
+ } else if (abs >= 1_000) {
+ divisor = 1_000;
+ suffix = "K";
+ } else {
+ if (value < 0) {
+ return `-$${Math.abs(value).toFixed(2)}`;
+ }
+ return `$${value.toFixed(2)}`;
+ }
+ const abbreviated = abs / divisor;
+ let formatted = abbreviated.toFixed(1);
+ if (formatted.includes(".")) {
+ formatted = formatted.replace(/\.?0+$/, "");
+ }
+ const sign = value < 0 ? "-" : "";
+ return `${sign}$${formatted}${suffix}`;
+}
+
/**
* Truncate a URL for compact display.
* @param {string} url - Full URL