fix(shared): respect cached model context limits for Anthropic providers post-GA
After Anthropic's 1M context GA (2026-03-13), the beta header is no longer sent. The existing detection relied solely on the beta header to set anthropicContext1MEnabled, causing all Anthropic models to fall back to the 200K default despite models.dev reporting 1M. Update resolveActualContextLimit to check per-model cached limits from provider config (populated from models.dev data) when the explicit 1M flag is not set. Priority order: 1. Explicit 1M mode (beta header or env var) - all Anthropic models 2. Per-model cached limit from provider config 3. Default 200K fallback This preserves the #2460 fix (explicit 1M flag always wins over cached values) while allowing GA models to use their correct limits. Fixes premature context warnings at 140K and unnecessary compaction at 156K for opus-4-6 and sonnet-4-6 users without env var workaround.
This commit is contained in:
@@ -28,21 +28,52 @@ describe("resolveActualContextLimit", () => {
|
||||
resetContextLimitEnv()
|
||||
})
|
||||
|
||||
it("returns the default Anthropic limit when 1M mode is disabled despite a cached limit", () => {
|
||||
it("returns cached limit for Anthropic models when 1M mode is disabled (GA support)", () => {
|
||||
// given
|
||||
delete process.env[ANTHROPIC_CONTEXT_ENV_KEY]
|
||||
delete process.env[VERTEX_CONTEXT_ENV_KEY]
|
||||
const modelContextLimitsCache = new Map<string, number>()
|
||||
modelContextLimitsCache.set("anthropic/claude-sonnet-4-5", 123456)
|
||||
modelContextLimitsCache.set("anthropic/claude-opus-4-6", 1_000_000)
|
||||
|
||||
// when
|
||||
const actualLimit = resolveActualContextLimit("anthropic", "claude-sonnet-4-5", {
|
||||
const actualLimit = resolveActualContextLimit("anthropic", "claude-opus-4-6", {
|
||||
anthropicContext1MEnabled: false,
|
||||
modelContextLimitsCache,
|
||||
})
|
||||
|
||||
// then — models.dev reports 1M for GA models, resolver should respect it
|
||||
expect(actualLimit).toBe(1_000_000)
|
||||
})
|
||||
|
||||
it("returns default 200K for Anthropic models without cached limit and 1M mode disabled", () => {
|
||||
// given
|
||||
delete process.env[ANTHROPIC_CONTEXT_ENV_KEY]
|
||||
delete process.env[VERTEX_CONTEXT_ENV_KEY]
|
||||
|
||||
// when
|
||||
const actualLimit = resolveActualContextLimit("anthropic", "claude-sonnet-4-5", {
|
||||
anthropicContext1MEnabled: false,
|
||||
})
|
||||
|
||||
// then
|
||||
expect(actualLimit).toBe(200000)
|
||||
expect(actualLimit).toBe(200_000)
|
||||
})
|
||||
|
||||
it("explicit 1M mode takes priority over cached limit", () => {
|
||||
// given
|
||||
delete process.env[ANTHROPIC_CONTEXT_ENV_KEY]
|
||||
delete process.env[VERTEX_CONTEXT_ENV_KEY]
|
||||
const modelContextLimitsCache = new Map<string, number>()
|
||||
modelContextLimitsCache.set("anthropic/claude-sonnet-4-5", 200_000)
|
||||
|
||||
// when
|
||||
const actualLimit = resolveActualContextLimit("anthropic", "claude-sonnet-4-5", {
|
||||
anthropicContext1MEnabled: true,
|
||||
modelContextLimitsCache,
|
||||
})
|
||||
|
||||
// then — explicit 1M flag overrides cached 200K
|
||||
expect(actualLimit).toBe(1_000_000)
|
||||
})
|
||||
|
||||
it("treats Anthropics aliases as Anthropic providers", () => {
|
||||
|
||||
@@ -26,7 +26,13 @@ export function resolveActualContextLimit(
|
||||
modelCacheState?: ContextLimitModelCacheState,
|
||||
): number | null {
|
||||
if (isAnthropicProvider(providerID)) {
|
||||
return getAnthropicActualLimit(modelCacheState)
|
||||
const explicit1M = getAnthropicActualLimit(modelCacheState)
|
||||
if (explicit1M === 1_000_000) return explicit1M
|
||||
|
||||
const cachedLimit = modelCacheState?.modelContextLimitsCache?.get(`${providerID}/${modelID}`)
|
||||
if (cachedLimit) return cachedLimit
|
||||
|
||||
return DEFAULT_ANTHROPIC_ACTUAL_LIMIT
|
||||
}
|
||||
|
||||
return modelCacheState?.modelContextLimitsCache?.get(`${providerID}/${modelID}`) ?? null
|
||||
|
||||
Reference in New Issue
Block a user