Merge pull request #4139 from code-yeongyu/fix/3450-anthropic-context-limit-revisited
fix(shared): return GA 1M context limit for Anthropic 4.6/4.7 models without cached entry
This commit is contained in:
@@ -42,7 +42,7 @@ describe("preemptive-compaction aws-bedrock-anthropic", () => {
|
|||||||
modelID: "claude-sonnet-4-6",
|
modelID: "claude-sonnet-4-6",
|
||||||
finish: true,
|
finish: true,
|
||||||
tokens: {
|
tokens: {
|
||||||
input: 170000,
|
input: 800000,
|
||||||
output: 1000,
|
output: 1000,
|
||||||
reasoning: 0,
|
reasoning: 0,
|
||||||
cache: { read: 10000, write: 0 },
|
cache: { read: 10000, write: 0 },
|
||||||
|
|||||||
@@ -142,7 +142,7 @@ describe("preemptive-compaction", () => {
|
|||||||
const hook = createPreemptiveCompactionHook(ctx as never, {} as never)
|
const hook = createPreemptiveCompactionHook(ctx as never, {} as never)
|
||||||
const sessionID = "ses_high"
|
const sessionID = "ses_high"
|
||||||
|
|
||||||
// 170K input + 10K cache = 180K → 90% of 200K
|
// 800K input + 10K cache = 810K → 81% of 1M (GA limit for 4.6 models)
|
||||||
await hook.event({
|
await hook.event({
|
||||||
event: {
|
event: {
|
||||||
type: "message.updated",
|
type: "message.updated",
|
||||||
@@ -154,7 +154,7 @@ describe("preemptive-compaction", () => {
|
|||||||
modelID: "claude-sonnet-4-6",
|
modelID: "claude-sonnet-4-6",
|
||||||
finish: true,
|
finish: true,
|
||||||
tokens: {
|
tokens: {
|
||||||
input: 170000,
|
input: 800000,
|
||||||
output: 1000,
|
output: 1000,
|
||||||
reasoning: 0,
|
reasoning: 0,
|
||||||
cache: { read: 10000, write: 0 },
|
cache: { read: 10000, write: 0 },
|
||||||
@@ -190,7 +190,7 @@ describe("preemptive-compaction", () => {
|
|||||||
modelID: "claude-sonnet-4-6",
|
modelID: "claude-sonnet-4-6",
|
||||||
finish: true,
|
finish: true,
|
||||||
tokens: {
|
tokens: {
|
||||||
input: 170000,
|
input: 800000,
|
||||||
output: 1000,
|
output: 1000,
|
||||||
reasoning: 0,
|
reasoning: 0,
|
||||||
cache: { read: 10000, write: 0 },
|
cache: { read: 10000, write: 0 },
|
||||||
@@ -267,7 +267,7 @@ describe("preemptive-compaction", () => {
|
|||||||
modelID: "claude-sonnet-4-6",
|
modelID: "claude-sonnet-4-6",
|
||||||
finish: true,
|
finish: true,
|
||||||
tokens: {
|
tokens: {
|
||||||
input: 170000,
|
input: 800000,
|
||||||
output: 0,
|
output: 0,
|
||||||
reasoning: 0,
|
reasoning: 0,
|
||||||
cache: { read: 10000, write: 0 },
|
cache: { read: 10000, write: 0 },
|
||||||
@@ -313,7 +313,7 @@ describe("preemptive-compaction", () => {
|
|||||||
modelID: "claude-sonnet-4-6",
|
modelID: "claude-sonnet-4-6",
|
||||||
finish: true,
|
finish: true,
|
||||||
tokens: {
|
tokens: {
|
||||||
input: 170000,
|
input: 800000,
|
||||||
output: 0,
|
output: 0,
|
||||||
reasoning: 0,
|
reasoning: 0,
|
||||||
cache: { read: 10000, write: 0 },
|
cache: { read: 10000, write: 0 },
|
||||||
@@ -357,7 +357,7 @@ describe("preemptive-compaction", () => {
|
|||||||
modelID: "claude-sonnet-4-6",
|
modelID: "claude-sonnet-4-6",
|
||||||
finish: true,
|
finish: true,
|
||||||
tokens: {
|
tokens: {
|
||||||
input: 170000,
|
input: 800000,
|
||||||
output: 0,
|
output: 0,
|
||||||
reasoning: 0,
|
reasoning: 0,
|
||||||
cache: { read: 10000, write: 0 },
|
cache: { read: 10000, write: 0 },
|
||||||
@@ -386,7 +386,7 @@ describe("preemptive-compaction", () => {
|
|||||||
modelID: "claude-sonnet-4-6",
|
modelID: "claude-sonnet-4-6",
|
||||||
finish: true,
|
finish: true,
|
||||||
tokens: {
|
tokens: {
|
||||||
input: 170000,
|
input: 800000,
|
||||||
output: 0,
|
output: 0,
|
||||||
reasoning: 0,
|
reasoning: 0,
|
||||||
cache: { read: 10000, write: 0 },
|
cache: { read: 10000, write: 0 },
|
||||||
@@ -504,7 +504,7 @@ describe("preemptive-compaction", () => {
|
|||||||
modelID: "claude-sonnet-4-6",
|
modelID: "claude-sonnet-4-6",
|
||||||
finish: true,
|
finish: true,
|
||||||
tokens: {
|
tokens: {
|
||||||
input: 170000,
|
input: 800000,
|
||||||
output: 0,
|
output: 0,
|
||||||
reasoning: 0,
|
reasoning: 0,
|
||||||
cache: { read: 10000, write: 0 },
|
cache: { read: 10000, write: 0 },
|
||||||
@@ -544,7 +544,7 @@ describe("preemptive-compaction", () => {
|
|||||||
modelID: "claude-sonnet-4-6",
|
modelID: "claude-sonnet-4-6",
|
||||||
finish: true,
|
finish: true,
|
||||||
tokens: {
|
tokens: {
|
||||||
input: 170000,
|
input: 800000,
|
||||||
output: 0,
|
output: 0,
|
||||||
reasoning: 0,
|
reasoning: 0,
|
||||||
cache: { read: 10000, write: 0 },
|
cache: { read: 10000, write: 0 },
|
||||||
@@ -576,7 +576,7 @@ describe("preemptive-compaction", () => {
|
|||||||
const hook = createPreemptiveCompactionHook(ctx as never, {} as never)
|
const hook = createPreemptiveCompactionHook(ctx as never, {} as never)
|
||||||
const sessionID = "ses_recompact"
|
const sessionID = "ses_recompact"
|
||||||
|
|
||||||
// given - first compaction cycle
|
// given - first compaction cycle (810K > 78% of 1M GA limit)
|
||||||
await hook.event({
|
await hook.event({
|
||||||
event: {
|
event: {
|
||||||
type: "message.updated",
|
type: "message.updated",
|
||||||
@@ -588,7 +588,7 @@ describe("preemptive-compaction", () => {
|
|||||||
modelID: "claude-sonnet-4-6",
|
modelID: "claude-sonnet-4-6",
|
||||||
finish: true,
|
finish: true,
|
||||||
tokens: {
|
tokens: {
|
||||||
input: 170000,
|
input: 800000,
|
||||||
output: 0,
|
output: 0,
|
||||||
reasoning: 0,
|
reasoning: 0,
|
||||||
cache: { read: 10000, write: 0 },
|
cache: { read: 10000, write: 0 },
|
||||||
@@ -619,7 +619,7 @@ describe("preemptive-compaction", () => {
|
|||||||
modelID: "claude-sonnet-4-6",
|
modelID: "claude-sonnet-4-6",
|
||||||
finish: true,
|
finish: true,
|
||||||
tokens: {
|
tokens: {
|
||||||
input: 170000,
|
input: 800000,
|
||||||
output: 0,
|
output: 0,
|
||||||
reasoning: 0,
|
reasoning: 0,
|
||||||
cache: { read: 10000, write: 0 },
|
cache: { read: 10000, write: 0 },
|
||||||
@@ -657,7 +657,7 @@ describe("preemptive-compaction", () => {
|
|||||||
modelID: "claude-sonnet-4-6",
|
modelID: "claude-sonnet-4-6",
|
||||||
finish: true,
|
finish: true,
|
||||||
tokens: {
|
tokens: {
|
||||||
input: 170000,
|
input: 800000,
|
||||||
output: 0,
|
output: 0,
|
||||||
reasoning: 0,
|
reasoning: 0,
|
||||||
cache: { read: 10000, write: 0 },
|
cache: { read: 10000, write: 0 },
|
||||||
|
|||||||
@@ -158,6 +158,76 @@ describe("resolveActualContextLimit", () => {
|
|||||||
expect(actualLimit).toBe(200_000)
|
expect(actualLimit).toBe(200_000)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it("returns GA 1M for claude-sonnet-4-6 without cached limit (GA context window)", () => {
|
||||||
|
// given
|
||||||
|
delete process.env[ANTHROPIC_CONTEXT_ENV_KEY]
|
||||||
|
delete process.env[VERTEX_CONTEXT_ENV_KEY]
|
||||||
|
|
||||||
|
// when
|
||||||
|
const actualLimit = resolveActualContextLimit("anthropic", "claude-sonnet-4-6", {
|
||||||
|
anthropicContext1MEnabled: false,
|
||||||
|
})
|
||||||
|
|
||||||
|
// then
|
||||||
|
expect(actualLimit).toBe(1_000_000)
|
||||||
|
})
|
||||||
|
|
||||||
|
it("returns GA 1M for claude-opus-4-6 without cached limit (GA context window)", () => {
|
||||||
|
// given
|
||||||
|
delete process.env[ANTHROPIC_CONTEXT_ENV_KEY]
|
||||||
|
delete process.env[VERTEX_CONTEXT_ENV_KEY]
|
||||||
|
|
||||||
|
// when
|
||||||
|
const actualLimit = resolveActualContextLimit("anthropic", "claude-opus-4-6", {
|
||||||
|
anthropicContext1MEnabled: false,
|
||||||
|
})
|
||||||
|
|
||||||
|
// then
|
||||||
|
expect(actualLimit).toBe(1_000_000)
|
||||||
|
})
|
||||||
|
|
||||||
|
it("returns GA 1M for claude-opus-4-7 without cached limit (GA context window)", () => {
|
||||||
|
// given
|
||||||
|
delete process.env[ANTHROPIC_CONTEXT_ENV_KEY]
|
||||||
|
delete process.env[VERTEX_CONTEXT_ENV_KEY]
|
||||||
|
|
||||||
|
// when
|
||||||
|
const actualLimit = resolveActualContextLimit("anthropic", "claude-opus-4-7", {
|
||||||
|
anthropicContext1MEnabled: false,
|
||||||
|
})
|
||||||
|
|
||||||
|
// then
|
||||||
|
expect(actualLimit).toBe(1_000_000)
|
||||||
|
})
|
||||||
|
|
||||||
|
it("returns GA 1M for claude-sonnet-4-6-high without cached limit (GA context window)", () => {
|
||||||
|
// given
|
||||||
|
delete process.env[ANTHROPIC_CONTEXT_ENV_KEY]
|
||||||
|
delete process.env[VERTEX_CONTEXT_ENV_KEY]
|
||||||
|
|
||||||
|
// when
|
||||||
|
const actualLimit = resolveActualContextLimit("anthropic", "claude-sonnet-4-6-high", {
|
||||||
|
anthropicContext1MEnabled: false,
|
||||||
|
})
|
||||||
|
|
||||||
|
// then
|
||||||
|
expect(actualLimit).toBe(1_000_000)
|
||||||
|
})
|
||||||
|
|
||||||
|
it("returns GA 1M for GA models on google-vertex-anthropic without cached limit", () => {
|
||||||
|
// given
|
||||||
|
delete process.env[ANTHROPIC_CONTEXT_ENV_KEY]
|
||||||
|
delete process.env[VERTEX_CONTEXT_ENV_KEY]
|
||||||
|
|
||||||
|
// when
|
||||||
|
const actualLimit = resolveActualContextLimit("google-vertex-anthropic", "claude-sonnet-4-6", {
|
||||||
|
anthropicContext1MEnabled: false,
|
||||||
|
})
|
||||||
|
|
||||||
|
// then
|
||||||
|
expect(actualLimit).toBe(1_000_000)
|
||||||
|
})
|
||||||
|
|
||||||
it("returns null for non-Anthropic providers without a cached limit", () => {
|
it("returns null for non-Anthropic providers without a cached limit", () => {
|
||||||
// given
|
// given
|
||||||
delete process.env[ANTHROPIC_CONTEXT_ENV_KEY]
|
delete process.env[ANTHROPIC_CONTEXT_ENV_KEY]
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import process from "node:process"
|
import process from "node:process"
|
||||||
|
|
||||||
const DEFAULT_ANTHROPIC_ACTUAL_LIMIT = 200_000
|
const DEFAULT_ANTHROPIC_ACTUAL_LIMIT = 200_000
|
||||||
|
const ANTHROPIC_GA_1M_LIMIT = 1_000_000
|
||||||
export type ContextLimitModelCacheState = {
|
export type ContextLimitModelCacheState = {
|
||||||
anthropicContext1MEnabled: boolean
|
anthropicContext1MEnabled: boolean
|
||||||
modelContextLimitsCache?: Map<string, number>
|
modelContextLimitsCache?: Map<string, number>
|
||||||
@@ -15,11 +16,11 @@ function getAnthropicActualLimit(modelCacheState?: ContextLimitModelCacheState):
|
|||||||
return (modelCacheState?.anthropicContext1MEnabled ?? false) ||
|
return (modelCacheState?.anthropicContext1MEnabled ?? false) ||
|
||||||
process.env.ANTHROPIC_1M_CONTEXT === "true" ||
|
process.env.ANTHROPIC_1M_CONTEXT === "true" ||
|
||||||
process.env.VERTEX_ANTHROPIC_1M_CONTEXT === "true"
|
process.env.VERTEX_ANTHROPIC_1M_CONTEXT === "true"
|
||||||
? 1_000_000
|
? ANTHROPIC_GA_1M_LIMIT
|
||||||
: DEFAULT_ANTHROPIC_ACTUAL_LIMIT
|
: DEFAULT_ANTHROPIC_ACTUAL_LIMIT
|
||||||
}
|
}
|
||||||
|
|
||||||
function supportsCachedAnthropicLimit(modelID: string): boolean {
|
function hasGA1MContext(modelID: string): boolean {
|
||||||
return /^claude-(opus|sonnet)-4(?:-|\.)(?:6|7)(?:-high)?$/.test(modelID)
|
return /^claude-(opus|sonnet)-4(?:-|\.)(?:6|7)(?:-high)?$/.test(modelID)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -30,10 +31,12 @@ export function resolveActualContextLimit(
|
|||||||
): number | null {
|
): number | null {
|
||||||
if (isAnthropicProvider(providerID)) {
|
if (isAnthropicProvider(providerID)) {
|
||||||
const explicit1M = getAnthropicActualLimit(modelCacheState)
|
const explicit1M = getAnthropicActualLimit(modelCacheState)
|
||||||
if (explicit1M === 1_000_000) return explicit1M
|
if (explicit1M === ANTHROPIC_GA_1M_LIMIT) return explicit1M
|
||||||
|
|
||||||
const cachedLimit = modelCacheState?.modelContextLimitsCache?.get(`${providerID}/${modelID}`)
|
const cachedLimit = modelCacheState?.modelContextLimitsCache?.get(`${providerID}/${modelID}`)
|
||||||
if (cachedLimit && supportsCachedAnthropicLimit(modelID)) return cachedLimit
|
if (cachedLimit && hasGA1MContext(modelID)) return cachedLimit
|
||||||
|
|
||||||
|
if (hasGA1MContext(modelID)) return ANTHROPIC_GA_1M_LIMIT
|
||||||
|
|
||||||
return DEFAULT_ANTHROPIC_ACTUAL_LIMIT
|
return DEFAULT_ANTHROPIC_ACTUAL_LIMIT
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user