@@ -12,8 +12,18 @@ import {
12
12
ProviderOptions ,
13
13
} from '../types.js' ;
14
14
15
- // Cache for model context window sizes
16
- const modelContextWindowCache : Record < string , number > = { } ;
15
+ const ANTHROPIC_CONTEXT_WINDOWS : Record < string , number > = {
16
+ 'claude-3-7-sonnet-20250219' : 200000 ,
17
+ 'claude-3-7-sonnet-latest' : 200000 ,
18
+ 'claude-3-5-sonnet-20241022' : 200000 ,
19
+ 'claude-3-5-sonnet-latest' : 200000 ,
20
+ 'claude-3-haiku-20240307' : 200000 ,
21
+ 'claude-3-opus-20240229' : 200000 ,
22
+ 'claude-3-sonnet-20240229' : 200000 ,
23
+ 'claude-2.1' : 100000 ,
24
+ 'claude-2.0' : 100000 ,
25
+ 'claude-instant-1.2' : 100000 ,
26
+ } ;
17
27
18
28
/**
19
29
* Anthropic-specific options
@@ -87,7 +97,7 @@ function addCacheControlToMessages(
87
97
function tokenUsageFromMessage (
88
98
message : Anthropic . Message ,
89
99
model : string ,
90
- contextWindow : number ,
100
+ contextWindow : number | undefined ,
91
101
) {
92
102
const usage = new TokenUsage ( ) ;
93
103
usage . input = message . usage . input_tokens ;
@@ -100,7 +110,7 @@ function tokenUsageFromMessage(
100
110
return {
101
111
usage,
102
112
totalTokens,
103
- maxTokens : contextWindow ,
113
+ contextWindow,
104
114
} ;
105
115
}
106
116
@@ -131,64 +141,12 @@ export class AnthropicProvider implements LLMProvider {
131
141
} ) ;
132
142
}
133
143
134
- /**
135
- * Fetches the model context window size from the Anthropic API
136
- *
137
- * @returns The context window size
138
- * @throws Error if the context window size cannot be determined
139
- */
140
- private async getModelContextWindow ( ) : Promise < number > {
141
- const cachedContextWindow = modelContextWindowCache [ this . model ] ;
142
- if ( cachedContextWindow !== undefined ) {
143
- return cachedContextWindow ;
144
- }
145
- const response = await this . client . models . list ( ) ;
146
-
147
- if ( ! response ?. data || ! Array . isArray ( response . data ) ) {
148
- throw new Error ( `Invalid response from models.list() for ${ this . model } ` ) ;
149
- }
150
-
151
- // Try to find the exact model
152
- let model = response . data . find ( ( m ) => m . id === this . model ) ;
153
-
154
- // If not found, try to find a model that starts with the same name
155
- // This helps with model aliases like 'claude-3-sonnet-latest'
156
- if ( ! model ) {
157
- // Split by '-latest' or '-20' to get the base model name
158
- const parts = this . model . split ( '-latest' ) ;
159
- const modelPrefix =
160
- parts . length > 1 ? parts [ 0 ] : this . model . split ( '-20' ) [ 0 ] ;
161
-
162
- if ( modelPrefix ) {
163
- model = response . data . find ( ( m ) => m . id . startsWith ( modelPrefix ) ) ;
164
-
165
- if ( model ) {
166
- console . info (
167
- `Model ${ this . model } not found, using ${ model . id } for context window size` ,
168
- ) ;
169
- }
170
- }
171
- }
172
-
173
- // Using type assertion to access context_window property
174
- // The Anthropic API returns context_window but it may not be in the TypeScript definitions
175
- if ( model && 'context_window' in model ) {
176
- const contextWindow = ( model as any ) . context_window ;
177
- // Cache the result for future use
178
- modelContextWindowCache [ this . model ] = contextWindow ;
179
- return contextWindow ;
180
- } else {
181
- throw new Error (
182
- `No context window information found for model: ${ this . model } ` ,
183
- ) ;
184
- }
185
- }
186
-
187
144
/**
188
145
* Generate text using Anthropic API
189
146
*/
190
147
async generateText ( options : GenerateOptions ) : Promise < LLMResponse > {
191
- const modelContextWindow = await this . getModelContextWindow ( ) ;
148
+ const modelContextWindow = ANTHROPIC_CONTEXT_WINDOWS [ this . model ] ;
149
+
192
150
const { messages, functions, temperature = 0.7 , maxTokens, topP } = options ;
193
151
194
152
// Extract system message
@@ -252,7 +210,7 @@ export class AnthropicProvider implements LLMProvider {
252
210
toolCalls : toolCalls ,
253
211
tokenUsage : tokenInfo . usage ,
254
212
totalTokens : tokenInfo . totalTokens ,
255
- maxTokens : tokenInfo . maxTokens ,
213
+ contextWindow : tokenInfo . contextWindow ,
256
214
} ;
257
215
}
258
216
0 commit comments