Restored the short form of large token numbers

This commit is contained in:
Thorsten Sommer committed 2026-09-23 18:18:22 +02:00
1 parent 39c93f6812
commit bbd73f389b
3 files changed
+27 -21

No files matched your search

+14 -6
View File
@@ -6,11 +6,10 @@ namespace AIStudio.Chat;
/// Writes a number of tokens the way a person reads it next to their input field.
/// </summary>
/// <remarks>
/// Written out in full up to a million, because below that the short form saves nothing: "11.86k"
/// and "11,860" are both six characters, and "999.99k" and "999,990" are both seven. All the
/// prefix does in that range is ask the reader to know what it stands for, and not every reader
/// does. From a million on it earns its place -- nine characters of digits become five -- and two
/// decimals there keep the resolution a person acts on.
/// A context window of a million tokens written out in full is eight characters of noise under a
/// text field, and nobody reads the last five of them. So everything from a thousand on is
/// shortened, and two decimals keep the resolution a person acts on: the difference between 1.20k
/// and 1.80k is one they can see, while the last three digits of 1,234 are not.
///
/// The culture is passed in rather than taken from the thread. AI Studio's language is chosen in
/// its settings and does not move the thread's culture along with it, so a German who picked German
@@ -21,7 +20,7 @@ public static class TokenAmount
/// <summary>
/// Below this, the exact number is shown.
/// </summary>
private const int EXACT_BELOW = 1_000_000;
private const int EXACT_BELOW = 1_000;
/// <summary>
/// Writes a number of tokens.
@@ -34,6 +33,15 @@ public static class TokenAmount
if (tokens < EXACT_BELOW)
return tokens.ToString("N0", culture);
//
// Rounded before the unit is chosen, not after. Otherwise the few hundred tokens just below
// a million round up inside their own unit and read as "1,000.00k", which is a number
// nobody writes.
//
var thousands = tokens / 1_000d;
if (Math.Round(thousands, 2) < 1_000d)
return $"{thousands.ToString("N2", culture)}k";
return $"{(tokens / 1_000_000d).ToString("N2", culture)}M";
}
}