From 58d253231d83f669a2cdfefa5fe479751d22c582 Mon Sep 17 00:00:00 2001 From: luke <97411838+luke2x@users.noreply.github.com> Date: Mon, 12 Oct 2026 09:13:54 +0100 Subject: [PATCH] fix(web): context meter no longer reads 1000k just under a million tokens (#17539) --- apps/web/src/lib/contextWindow.test.ts | 10 ++++++++++ apps/web/src/lib/contextWindow.ts | 3 ++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/apps/web/src/lib/contextWindow.test.ts b/apps/web/src/lib/contextWindow.test.ts index 24adda08ae1736b54c424d62f4e6f60b4db10d87..2847b23cab779ef61c2a960325cc3bbb4475906d 100644 --- a/apps/web/src/lib/contextWindow.test.ts +++ b/apps/web/src/lib/contextWindow.test.ts @@ -58,6 +58,16 @@ describe("V2 context window presentation", () => { it("formats compact token values", () => { expect(formatContextWindowTokens(1_500)).toBe("1.5k"); }); + + it.each([ + [999_499, "999k"], + [999_500, "1m"], + [999_999, "1m"], + [1_000_000, "1m"], + [1_100_000, "1.1m"], + ])("formats %d tokens as %s at the million boundary", (tokens, expected) => { + expect(formatContextWindowTokens(tokens)).toBe(expected); + }); }); describe("live provider-turn usage (#8144)", () => { diff --git a/apps/web/src/lib/contextWindow.ts b/apps/web/src/lib/contextWindow.ts index d06141b0e84ee558cd4f01c3535ab29aa1eab2b3..33f59c3eab493e4a8c9946fb21e519391096a474 100644 --- a/apps/web/src/lib/contextWindow.ts +++ b/apps/web/src/lib/contextWindow.ts @@ -171,7 +171,8 @@ export function formatContextWindowTokens(value: number | null): string { if (value < 10_000) { return `${(value / 1_000).toFixed(1).replace(/\.0$/, "")}k`; } - if (value < 1_000_000) { + // Anything from 999,500 rounds to 1000k, so it belongs to the next unit. + if (value < 999_500) { return `${Math.round(value / 1_000)}k`; } return `${(value / 1_000_000).toFixed(1).replace(/\.0$/, "")}m`; -- 2.54.0