( model: string, betas?: string[], )
| 52 | } |
| 53 | |
| 54 | export function getContextWindowForModel( |
| 55 | model: string, |
| 56 | betas?: string[], |
| 57 | ): number { |
| 58 | // Allow override via environment variable (ant-only) |
| 59 | // This takes precedence over all other context window resolution, including 1M detection, |
| 60 | // so users can cap the effective context window for local decisions (auto-compact, etc.) |
| 61 | // while still using a 1M-capable endpoint. |
| 62 | if ( |
| 63 | isInternalBuild() && |
| 64 | process.env.CLAUDE_CODE_MAX_CONTEXT_TOKENS |
| 65 | ) { |
| 66 | const override = parseInt(process.env.CLAUDE_CODE_MAX_CONTEXT_TOKENS, 10) |
| 67 | if (!isNaN(override) && override > 0) { |
| 68 | return override |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | // Managed model profile is authoritative. This runs BEFORE the [1m] regex |
| 73 | // so that attaching [1m] to an alias that resolves to a managed model |
| 74 | // (e.g. `sonnet[1m]` -> Kimi K2.7, which has a 200K usable budget) cannot |
| 75 | // silently inflate the context window past what the model can actually |
| 76 | // serve. The [1m] tag still works as expected for unmanaged Anthropic-style |
| 77 | // models below. |
| 78 | const ncodeModel = resolveNCodeManagedModel(model) |
| 79 | if (ncodeModel) { |
| 80 | return ncodeModel.contextWindow |
| 81 | } |
| 82 | |
| 83 | // [1m] suffix — explicit client-side opt-in for unmanaged Anthropic models. |
| 84 | if (has1mContext(model)) { |
| 85 | return 1_000_000 |
| 86 | } |
| 87 | |
| 88 | const cap = getModelCapability(model) |
| 89 | if (cap?.max_input_tokens && cap.max_input_tokens >= 100_000) { |
| 90 | if ( |
| 91 | cap.max_input_tokens > MODEL_CONTEXT_WINDOW_DEFAULT && |
| 92 | is1mContextDisabled() |
| 93 | ) { |
| 94 | return MODEL_CONTEXT_WINDOW_DEFAULT |
| 95 | } |
| 96 | return cap.max_input_tokens |
| 97 | } |
| 98 | |
| 99 | if (betas?.includes(CONTEXT_1M_BETA_HEADER) && modelSupports1M(model)) { |
| 100 | return 1_000_000 |
| 101 | } |
| 102 | if (getSonnet1mExpTreatmentEnabled(model)) { |
| 103 | return 1_000_000 |
| 104 | } |
| 105 | if (isInternalBuild()) { |
| 106 | const antModel = resolveAntModel(model) |
| 107 | if (antModel?.contextWindow) { |
| 108 | return antModel.contextWindow |
| 109 | } |
| 110 | } |
| 111 | return MODEL_CONTEXT_WINDOW_DEFAULT |
no test coverage detected