diff --git a/tokens/tokens.go b/tokens/tokens.go index a0c3468..6c5ea1c 100644 --- a/tokens/tokens.go +++ b/tokens/tokens.go @@ -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 diff --git a/tokens/tokens_test.go b/tokens/tokens_test.go index d70a145..4724eba 100644 --- a/tokens/tokens_test.go +++ b/tokens/tokens_test.go @@ -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.