fix(tokens): generalize Gemini context-window detection by tier

windowByModelFamily matched Gemini models by exact version substring
("gemini-1.5-pro", "gemini-1.5-flash", "gemini-2"), so any newer
generation (3.x and beyond) fell through to defaultRemoteUnknownWindow
(128k) purely because this table hadn't been updated yet — observed
via hand's status line showing a >100% context gauge for a
"gemini-3.8-flash"-family model whose real window is far larger, and
which made harness's own preventive compaction unreachable (the
threshold check never fires against a window this far off).

Match on tier (Pro vs. everything else) instead of enumerating every
version: every Pro-tier Gemini release so far ships 2M, everything
else at least 1M, and both hold for a model this table has never seen
the exact version string of.
This commit is contained in:
sausheong committed 2026-09-05 22:06:59 +08:00
1 parent 986c233d39
commit 0ba1b4dbcc
2 files changed
+40 -3

No files matched your search

+17 -3
View File
@@ -141,10 +141,24 @@ func windowByModelFamily(modelID string) int {
return 8192
case strings.HasPrefix(leaf, "gpt-3.5"):
return 16385
case strings.Contains(id, "gemini-1.5-pro"):
case strings.Contains(id, "gemini") && strings.Contains(id, "pro"):
// Every "Pro"-tier Gemini release so far (1.5 Pro onward) has
// shipped the largest window in its generation. Matching on the
// tier rather than enumerating each version keeps a future Pro
// release (2.5, 3, ...) from falling through to the
// flash/other-tier case below, let alone all the way to
// defaultRemoteUnknownWindow.
return 2000000
case strings.Contains(id, "gemini-1.5-flash"),
strings.Contains(id, "gemini-2"):
case strings.Contains(id, "gemini"):
// Every non-Pro Gemini model since 1.5 (Flash and other tiers)
// has shipped at least a 1M window. Matching on the family alone
// — not "gemini-1.5-flash"/"gemini-2" by exact version — means a
// new Gemini generation (3.x and beyond) gets a realistic window
// immediately instead of silently falling back to
// defaultRemoteUnknownWindow (128k), which made preventive
// compaction fire far too early and the context-usage gauge
// read a nonsensical >100% for a model this table simply hadn't
// been updated for yet.
return 1000000
}
return 0
+23
View File
@@ -52,6 +52,29 @@ func TestContextWindowKnown(t *testing.T) {
}
}
// Regression: a Gemini generation newer than the ones explicitly
// enumerated (3.x and beyond) must not fall through to
// defaultRemoteUnknownWindow (128k) just because this table hasn't been
// updated with its exact version string yet — every non-Pro Gemini
// model since 1.5 has shipped at least a 1M window, and every Pro-tier
// release has shipped at least 2M.
func TestContextWindowGeminiFutureGenerationsDefaultByTier(t *testing.T) {
tests := []struct {
model string
want int
}{
{"openai/gemini-3.8-flash-global", 1000000}, // proxied under an unrelated provider label, as via a LiteLLM gateway
{"google/gemini-3-flash", 1000000},
{"google/gemini-3-pro", 2000000},
{"google/gemini-4.0-flash-lite", 1000000},
}
for _, tc := range tests {
t.Run(tc.model, func(t *testing.T) {
assert.Equal(t, tc.want, ContextWindow(tc.model))
})
}
}
func TestContextWindowUnknownRemoteReturnsRemoteFallback(t *testing.T) {
// Non-local unknown models default to 128k — frontier proxies are
// overwhelmingly 128k+, and reactive compaction handles overflow.