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:
@@ -136,8 +136,8 @@ describe("context-window-monitor modelContextLimitsCache", () => {
|
||||
})
|
||||
|
||||
describe("#given Anthropic provider with cached context limit and 1M mode disabled", () => {
|
||||
describe("#when cached usage exceeds the Anthropic default limit", () => {
|
||||
it("#then should ignore the cached limit and append the reminder from the default Anthropic limit", async () => {
|
||||
describe("#when cached usage is below threshold of cached limit", () => {
|
||||
it("#then should respect the cached limit and skip the reminder", async () => {
|
||||
// given
|
||||
const modelContextLimitsCache = new Map<string, number>()
|
||||
modelContextLimitsCache.set("anthropic/claude-sonnet-4-5", 500000)
|
||||
@@ -146,7 +146,7 @@ describe("context-window-monitor modelContextLimitsCache", () => {
|
||||
anthropicContext1MEnabled: false,
|
||||
modelContextLimitsCache,
|
||||
})
|
||||
const sessionID = "ses_anthropic_default_overrides_cached_limit"
|
||||
const sessionID = "ses_anthropic_cached_limit_respected"
|
||||
|
||||
await hook.event({
|
||||
event: {
|
||||
@@ -173,11 +173,51 @@ describe("context-window-monitor modelContextLimitsCache", () => {
|
||||
const output = createOutput()
|
||||
await hook["tool.execute.after"]({ tool: "bash", sessionID, callID: "call_1" }, output)
|
||||
|
||||
// then
|
||||
// then — 160K/500K = 32%, well below 70% threshold
|
||||
expect(output.output).toBe("original")
|
||||
})
|
||||
})
|
||||
|
||||
describe("#when cached usage exceeds threshold of cached limit", () => {
|
||||
it("#then should use the cached limit for the reminder", async () => {
|
||||
// given
|
||||
const modelContextLimitsCache = new Map<string, number>()
|
||||
modelContextLimitsCache.set("anthropic/claude-sonnet-4-5", 500000)
|
||||
|
||||
const hook = createContextWindowMonitorHook({} as never, {
|
||||
anthropicContext1MEnabled: false,
|
||||
modelContextLimitsCache,
|
||||
})
|
||||
const sessionID = "ses_anthropic_cached_limit_exceeded"
|
||||
|
||||
await hook.event({
|
||||
event: {
|
||||
type: "message.updated",
|
||||
properties: {
|
||||
info: {
|
||||
role: "assistant",
|
||||
sessionID,
|
||||
providerID: "anthropic",
|
||||
modelID: "claude-sonnet-4-5",
|
||||
finish: true,
|
||||
tokens: {
|
||||
input: 350000,
|
||||
output: 0,
|
||||
reasoning: 0,
|
||||
cache: { read: 10000, write: 0 },
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
// when
|
||||
const output = createOutput()
|
||||
await hook["tool.execute.after"]({ tool: "bash", sessionID, callID: "call_1" }, output)
|
||||
|
||||
// then — 360K/500K = 72%, above 70% threshold, uses cached 500K limit
|
||||
expect(output.output).toContain("context remaining")
|
||||
expect(output.output).toContain("200,000-token context window")
|
||||
expect(output.output).not.toContain("500,000-token context window")
|
||||
expect(output.output).not.toContain("1,000,000-token context window")
|
||||
expect(output.output).toContain("500,000-token context window")
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user