@@ -12,21 +12,8 @@ import {
12
12
ProviderOptions ,
13
13
} from '../types.js' ;
14
14
15
- // Fallback model context window sizes for Anthropic models
16
- // Used only if models.list() call fails or returns incomplete data
17
- const ANTHROPIC_MODEL_LIMITS_FALLBACK : Record < string , number > = {
18
- default : 200000 ,
19
- 'claude-3-7-sonnet-20250219' : 200000 ,
20
- 'claude-3-7-sonnet-latest' : 200000 ,
21
- 'claude-3-5-sonnet-20241022' : 200000 ,
22
- 'claude-3-5-sonnet-latest' : 200000 ,
23
- 'claude-3-haiku-20240307' : 200000 ,
24
- 'claude-3-opus-20240229' : 200000 ,
25
- 'claude-3-sonnet-20240229' : 200000 ,
26
- 'claude-2.1' : 100000 ,
27
- 'claude-2.0' : 100000 ,
28
- 'claude-instant-1.2' : 100000 ,
29
- } ;
15
+ // Cache for model context window sizes
16
+ const modelContextWindowCache : Record < string , number > = { } ;
30
17
31
18
/**
32
19
* Anthropic-specific options
@@ -97,9 +84,6 @@ function addCacheControlToMessages(
97
84
} ) ;
98
85
}
99
86
100
- // Cache for model context window sizes
101
- const modelContextWindowCache : Record < string , number > = { } ;
102
-
103
87
function tokenUsageFromMessage (
104
88
message : Anthropic . Message ,
105
89
model : string ,
@@ -112,12 +96,15 @@ function tokenUsageFromMessage(
112
96
usage . output = message . usage . output_tokens ;
113
97
114
98
const totalTokens = usage . input + usage . output ;
115
- // Use provided context window, or fallback to cached value, or use hardcoded fallback
116
- const maxTokens =
117
- contextWindow ||
118
- modelContextWindowCache [ model ] ||
119
- ANTHROPIC_MODEL_LIMITS_FALLBACK [ model ] ||
120
- ANTHROPIC_MODEL_LIMITS_FALLBACK . default ;
99
+
100
+ // Use provided context window or fallback to cached value
101
+ const maxTokens = contextWindow || modelContextWindowCache [ model ] ;
102
+
103
+ if ( ! maxTokens ) {
104
+ throw new Error (
105
+ `Context window size not available for model: ${ model } . Make sure to initialize the model properly.` ,
106
+ ) ;
107
+ }
121
108
122
109
return {
123
110
usage,
@@ -155,26 +142,28 @@ export class AnthropicProvider implements LLMProvider {
155
142
156
143
// Initialize model context window detection
157
144
// This is async but we don't need to await it here
158
- // If it fails, we'll fall back to hardcoded limits
145
+ // If it fails, an error will be thrown when the model is used
159
146
this . initializeModelContextWindow ( ) . catch ( ( error ) => {
160
- console . warn (
161
- `Failed to initialize model context window: ${ error . message } ` ,
147
+ console . error (
148
+ `Failed to initialize model context window: ${ error . message } . The model will not work until context window information is available. ` ,
162
149
) ;
163
150
} ) ;
164
151
}
165
152
166
153
/**
167
154
* Fetches the model context window size from the Anthropic API
168
155
*
169
- * @returns The context window size if successfully fetched, otherwise undefined
156
+ * @returns The context window size
157
+ * @throws Error if the context window size cannot be determined
170
158
*/
171
- private async initializeModelContextWindow ( ) : Promise < number | undefined > {
159
+ private async initializeModelContextWindow ( ) : Promise < number > {
172
160
try {
173
161
const response = await this . client . models . list ( ) ;
174
162
175
163
if ( ! response ?. data || ! Array . isArray ( response . data ) ) {
176
- console . warn ( `Invalid response from models.list() for ${ this . model } ` ) ;
177
- return undefined ;
164
+ throw new Error (
165
+ `Invalid response from models.list() for ${ this . model } ` ,
166
+ ) ;
178
167
}
179
168
180
169
// Try to find the exact model
@@ -208,15 +197,14 @@ export class AnthropicProvider implements LLMProvider {
208
197
modelContextWindowCache [ this . model ] = contextWindow ;
209
198
return contextWindow ;
210
199
} else {
211
- console . warn ( `No context window information found for ${ this . model } ` ) ;
212
- return undefined ;
200
+ throw new Error (
201
+ `No context window information found for model: ${ this . model } ` ,
202
+ ) ;
213
203
}
214
204
} catch ( error ) {
215
- console . warn (
216
- `Failed to fetch model context window for ${ this . model } : ${ ( error as Error ) . message } ` ,
205
+ throw new Error (
206
+ `Failed to determine context window size for model ${ this . model } : ${ ( error as Error ) . message } ` ,
217
207
) ;
218
- // Will fall back to hardcoded limits
219
- return undefined ;
220
208
}
221
209
}
222
210
0 commit comments