@@ -154,29 +154,69 @@ export class AnthropicProvider implements LLMProvider {
154154 } ) ;
155155
156156 // Initialize model context window detection
157- this . initializeModelContextWindow ( ) ;
157+ // This is async but we don't need to await it here
158+ // If it fails, we'll fall back to hardcoded limits
159+ this . initializeModelContextWindow ( ) . catch ( ( error ) => {
160+ console . warn (
161+ `Failed to initialize model context window: ${ error . message } ` ,
162+ ) ;
163+ } ) ;
158164 }
159165
160166 /**
161167 * Fetches the model context window size from the Anthropic API
168+ *
169+ * @returns The context window size if successfully fetched, otherwise undefined
162170 */
163- private async initializeModelContextWindow ( ) : Promise < void > {
171+ private async initializeModelContextWindow ( ) : Promise < number | undefined > {
164172 try {
165173 const response = await this . client . models . list ( ) ;
166- const model = response . data . find ( ( m ) => m . id === this . model ) ;
174+
175+ if ( ! response ?. data || ! Array . isArray ( response . data ) ) {
176+ console . warn ( `Invalid response from models.list() for ${ this . model } ` ) ;
177+ return undefined ;
178+ }
179+
180+ // Try to find the exact model
181+ let model = response . data . find ( ( m ) => m . id === this . model ) ;
182+
183+ // If not found, try to find a model that starts with the same name
184+ // This helps with model aliases like 'claude-3-sonnet-latest'
185+ if ( ! model ) {
186+ // Split by '-latest' or '-20' to get the base model name
187+ const parts = this . model . split ( '-latest' ) ;
188+ const modelPrefix =
189+ parts . length > 1 ? parts [ 0 ] : this . model . split ( '-20' ) [ 0 ] ;
190+
191+ if ( modelPrefix ) {
192+ model = response . data . find ( ( m ) => m . id . startsWith ( modelPrefix ) ) ;
193+
194+ if ( model ) {
195+ console . info (
196+ `Model ${ this . model } not found, using ${ model . id } for context window size` ,
197+ ) ;
198+ }
199+ }
200+ }
167201
168202 // Using type assertion to access context_window property
169203 // The Anthropic API returns context_window but it may not be in the TypeScript definitions
170204 if ( model && 'context_window' in model ) {
171- this . modelContextWindow = ( model as any ) . context_window ;
205+ const contextWindow = ( model as any ) . context_window ;
206+ this . modelContextWindow = contextWindow ;
172207 // Cache the result for future use
173- modelContextWindowCache [ this . model ] = ( model as any ) . context_window ;
208+ modelContextWindowCache [ this . model ] = contextWindow ;
209+ return contextWindow ;
210+ } else {
211+ console . warn ( `No context window information found for ${ this . model } ` ) ;
212+ return undefined ;
174213 }
175214 } catch ( error ) {
176215 console . warn (
177216 `Failed to fetch model context window for ${ this . model } : ${ ( error as Error ) . message } ` ,
178217 ) ;
179218 // Will fall back to hardcoded limits
219+ return undefined ;
180220 }
181221 }
182222
0 commit comments