Skip to content
TypeScript

fix(web): context meter no longer reads 1000k just under a million tokens (#17539)

luke58d253231d83
diff --git a/apps/web/src/lib/contextWindow.test.ts b/apps/web/src/lib/contextWindow.test.ts
index 24adda08ae1..2847b23cab7 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 d06141b0e84..33f59c3eab4 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`;