2026-03-11 21:45:44 +09:00
|
|
|
import process from "node:process"
|
|
|
|
|
|
|
|
|
|
const DEFAULT_ANTHROPIC_ACTUAL_LIMIT = 200_000
|
|
|
|
|
export type ContextLimitModelCacheState = {
|
|
|
|
|
anthropicContext1MEnabled: boolean
|
|
|
|
|
modelContextLimitsCache?: Map<string, number>
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function isAnthropicProvider(providerID: string): boolean {
|
2026-03-12 00:31:02 +09:00
|
|
|
const normalized = providerID.toLowerCase()
|
|
|
|
|
return normalized === "anthropic" || normalized === "google-vertex-anthropic" || normalized === "aws-bedrock-anthropic"
|
2026-03-11 21:45:44 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getAnthropicActualLimit(modelCacheState?: ContextLimitModelCacheState): number {
|
|
|
|
|
return (modelCacheState?.anthropicContext1MEnabled ?? false) ||
|
|
|
|
|
process.env.ANTHROPIC_1M_CONTEXT === "true" ||
|
|
|
|
|
process.env.VERTEX_ANTHROPIC_1M_CONTEXT === "true"
|
|
|
|
|
? 1_000_000
|
|
|
|
|
: DEFAULT_ANTHROPIC_ACTUAL_LIMIT
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-28 15:24:18 +09:00
|
|
|
function supportsCachedAnthropicLimit(modelID: string): boolean {
|
|
|
|
|
return /^claude-(opus|sonnet)-4(?:-|\.)6(?:-high)?$/.test(modelID)
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-11 21:45:44 +09:00
|
|
|
export function resolveActualContextLimit(
|
|
|
|
|
providerID: string,
|
|
|
|
|
modelID: string,
|
|
|
|
|
modelCacheState?: ContextLimitModelCacheState,
|
|
|
|
|
): number | null {
|
|
|
|
|
if (isAnthropicProvider(providerID)) {
|
2026-03-18 12:21:08 +09:00
|
|
|
const explicit1M = getAnthropicActualLimit(modelCacheState)
|
|
|
|
|
if (explicit1M === 1_000_000) return explicit1M
|
|
|
|
|
|
|
|
|
|
const cachedLimit = modelCacheState?.modelContextLimitsCache?.get(`${providerID}/${modelID}`)
|
2026-03-28 15:24:18 +09:00
|
|
|
if (cachedLimit && supportsCachedAnthropicLimit(modelID)) return cachedLimit
|
2026-03-18 12:21:08 +09:00
|
|
|
|
|
|
|
|
return DEFAULT_ANTHROPIC_ACTUAL_LIMIT
|
2026-03-11 21:45:44 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return modelCacheState?.modelContextLimitsCache?.get(`${providerID}/${modelID}`) ?? null
|
|
|
|
|
}
|