@@ -12,8 +12,18 @@ import {
1212 ProviderOptions ,
1313} from '../types.js' ;
1414
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+ } ;
1727
1828/**
1929 * Anthropic-specific options
@@ -87,7 +97,7 @@ function addCacheControlToMessages(
8797function tokenUsageFromMessage (
8898 message : Anthropic . Message ,
8999 model : string ,
90- contextWindow : number ,
100+ contextWindow : number | undefined ,
91101) {
92102 const usage = new TokenUsage ( ) ;
93103 usage . input = message . usage . input_tokens ;
@@ -100,7 +110,7 @@ function tokenUsageFromMessage(
100110 return {
101111 usage,
102112 totalTokens,
103- maxTokens : contextWindow ,
113+ contextWindow,
104114 } ;
105115}
106116
@@ -131,64 +141,12 @@ export class AnthropicProvider implements LLMProvider {
131141 } ) ;
132142 }
133143
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-
187144 /**
188145 * Generate text using Anthropic API
189146 */
190147 async generateText ( options : GenerateOptions ) : Promise < LLMResponse > {
191- const modelContextWindow = await this . getModelContextWindow ( ) ;
148+ const modelContextWindow = ANTHROPIC_CONTEXT_WINDOWS [ this . model ] ;
149+
192150 const { messages, functions, temperature = 0.7 , maxTokens, topP } = options ;
193151
194152 // Extract system message
@@ -252,7 +210,7 @@ export class AnthropicProvider implements LLMProvider {
252210 toolCalls : toolCalls ,
253211 tokenUsage : tokenInfo . usage ,
254212 totalTokens : tokenInfo . totalTokens ,
255- maxTokens : tokenInfo . maxTokens ,
213+ contextWindow : tokenInfo . contextWindow ,
256214 } ;
257215 }
258216
0 commit comments