fix: prioritize Anthropic 1M limits over cached context limits
This commit is contained in:
@@ -124,4 +124,32 @@ describe("getContextWindowUsage", () => {
|
||||
expect(usage?.usagePercentage).toBeCloseTo(180000 / 262144)
|
||||
expect(usage?.remainingTokens).toBe(82144)
|
||||
})
|
||||
|
||||
describe("#given Anthropic provider with cached context limit and 1M mode enabled", () => {
|
||||
describe("#when context usage is resolved", () => {
|
||||
it("#then should ignore the cached limit and use the 1M Anthropic limit", async () => {
|
||||
// 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", 200000)
|
||||
|
||||
const ctx = createContextUsageMockContext(300000, {
|
||||
providerID: "anthropic",
|
||||
modelID: "claude-sonnet-4-5",
|
||||
})
|
||||
|
||||
// when
|
||||
const usage = await getContextWindowUsage(ctx as never, "ses_cached_anthropic_1m", {
|
||||
anthropicContext1MEnabled: true,
|
||||
modelContextLimitsCache,
|
||||
})
|
||||
|
||||
// then
|
||||
expect(usage?.usagePercentage).toBe(0.3)
|
||||
expect(usage?.remainingTokens).toBe(700000)
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -148,17 +148,18 @@ export async function getContextWindowUsage(
|
||||
const lastTokens = lastAssistant?.tokens;
|
||||
if (!lastAssistant || !lastTokens) return null;
|
||||
|
||||
const cachedLimit =
|
||||
lastAssistant.providerID !== undefined && lastAssistant.modelID !== undefined
|
||||
const modelSpecificLimit =
|
||||
lastAssistant.providerID !== undefined &&
|
||||
lastAssistant.modelID !== undefined &&
|
||||
!isAnthropicProvider(lastAssistant.providerID)
|
||||
? modelCacheState?.modelContextLimitsCache?.get(
|
||||
`${lastAssistant.providerID}/${lastAssistant.modelID}`,
|
||||
)
|
||||
: undefined;
|
||||
const actualLimit =
|
||||
cachedLimit ??
|
||||
(lastAssistant.providerID !== undefined && isAnthropicProvider(lastAssistant.providerID)
|
||||
lastAssistant.providerID !== undefined && isAnthropicProvider(lastAssistant.providerID)
|
||||
? getAnthropicActualLimit(modelCacheState)
|
||||
: null);
|
||||
: modelSpecificLimit ?? null;
|
||||
|
||||
if (!actualLimit) return null;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user