( model: string, betas?: string[], )
| 54 | } |
| 55 | |
| 56 | export function getContextWindowForModel( |
| 57 | model: string, |
| 58 | betas?: string[], |
| 59 | ): number { |
| 60 | // Allow override via environment variable (ant-only) |
| 61 | // This takes precedence over all other context window resolution, including 1M detection, |
| 62 | // so users can cap the effective context window for local decisions (auto-compact, etc.) |
| 63 | // while still using a 1M-capable endpoint. |
| 64 | if ( |
| 65 | process.env.USER_TYPE === 'ant' && |
| 66 | process.env.CLAUDE_CODE_MAX_CONTEXT_TOKENS |
| 67 | ) { |
| 68 | const override = parseInt(process.env.CLAUDE_CODE_MAX_CONTEXT_TOKENS, 10) |
| 69 | if (!isNaN(override) && override > 0) { |
| 70 | return override |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | // [1m] suffix — explicit client-side opt-in, respected over all detection |
| 75 | if (has1mContext(model)) { |
| 76 | return 1_000_000 |
| 77 | } |
| 78 | |
| 79 | const cap = getModelCapability(model) |
| 80 | if (cap?.max_input_tokens && cap.max_input_tokens >= 100_000) { |
| 81 | if ( |
| 82 | cap.max_input_tokens > MODEL_CONTEXT_WINDOW_DEFAULT && |
| 83 | is1mContextDisabled() |
| 84 | ) { |
| 85 | return MODEL_CONTEXT_WINDOW_DEFAULT |
| 86 | } |
| 87 | return cap.max_input_tokens |
| 88 | } |
| 89 | |
| 90 | if (betas?.includes(CONTEXT_1M_BETA_HEADER) && modelSupports1M(model)) { |
| 91 | return 1_000_000 |
| 92 | } |
| 93 | if (getSonnet1mExpTreatmentEnabled(model)) { |
| 94 | return 1_000_000 |
| 95 | } |
| 96 | if (process.env.USER_TYPE === 'ant') { |
| 97 | const antModel = resolveAntModel(model) |
| 98 | if (antModel?.contextWindow) { |
| 99 | return antModel.contextWindow |
| 100 | } |
| 101 | } |
| 102 | return MODEL_CONTEXT_WINDOW_DEFAULT |
| 103 | } |
| 104 | |
| 105 | export function getSonnet1mExpTreatmentEnabled(model: string): boolean { |
| 106 | if (is1mContextDisabled()) { |
no test coverage detected