@@ -12,6 +12,11 @@ export const liteLLMModelPricingSchema = v.object({
1212 max_tokens : v . optional ( v . number ( ) ) ,
1313 max_input_tokens : v . optional ( v . number ( ) ) ,
1414 max_output_tokens : v . optional ( v . number ( ) ) ,
15+ // 1M context window pricing
16+ input_cost_per_token_above_200k_tokens : v . optional ( v . number ( ) ) ,
17+ output_cost_per_token_above_200k_tokens : v . optional ( v . number ( ) ) ,
18+ cache_creation_input_token_cost_above_200k_tokens : v . optional ( v . number ( ) ) ,
19+ cache_read_input_token_cost_above_200k_tokens : v . optional ( v . number ( ) ) ,
1520} ) ;
1621
1722export type LiteLLMModelPricing = v . InferOutput < typeof liteLLMModelPricingSchema > ;
@@ -212,27 +217,72 @@ export class LiteLLMPricingFetcher implements Disposable {
212217 pricing : LiteLLMModelPricing ,
213218 ) : number {
214219 let cost = 0 ;
220+ const CONTEXT_THRESHOLD = 200_000 ;
215221
216- if ( pricing . input_cost_per_token != null ) {
217- cost += tokens . input_tokens * pricing . input_cost_per_token ;
222+ // Calculate input tokens cost
223+ if ( tokens . input_tokens > 0 ) {
224+ if ( tokens . input_tokens > CONTEXT_THRESHOLD && pricing . input_cost_per_token_above_200k_tokens != null ) {
225+ // Split tokens into two buckets: below and above 200k
226+ const tokensBelow200k = Math . min ( tokens . input_tokens , CONTEXT_THRESHOLD ) ;
227+ const tokensAbove200k = Math . max ( 0 , tokens . input_tokens - CONTEXT_THRESHOLD ) ;
228+
229+ if ( pricing . input_cost_per_token != null ) {
230+ cost += tokensBelow200k * pricing . input_cost_per_token ;
231+ }
232+ cost += tokensAbove200k * pricing . input_cost_per_token_above_200k_tokens ;
233+ }
234+ else if ( pricing . input_cost_per_token != null ) {
235+ cost += tokens . input_tokens * pricing . input_cost_per_token ;
236+ }
218237 }
219238
220- if ( pricing . output_cost_per_token != null ) {
221- cost += tokens . output_tokens * pricing . output_cost_per_token ;
239+ // Calculate output tokens cost
240+ if ( tokens . output_tokens > 0 ) {
241+ if ( tokens . output_tokens > CONTEXT_THRESHOLD && pricing . output_cost_per_token_above_200k_tokens != null ) {
242+ // Split tokens into two buckets: below and above 200k
243+ const tokensBelow200k = Math . min ( tokens . output_tokens , CONTEXT_THRESHOLD ) ;
244+ const tokensAbove200k = Math . max ( 0 , tokens . output_tokens - CONTEXT_THRESHOLD ) ;
245+
246+ if ( pricing . output_cost_per_token != null ) {
247+ cost += tokensBelow200k * pricing . output_cost_per_token ;
248+ }
249+ cost += tokensAbove200k * pricing . output_cost_per_token_above_200k_tokens ;
250+ }
251+ else if ( pricing . output_cost_per_token != null ) {
252+ cost += tokens . output_tokens * pricing . output_cost_per_token ;
253+ }
222254 }
223255
224- if (
225- tokens . cache_creation_input_tokens != null
226- && pricing . cache_creation_input_token_cost != null
227- ) {
228- cost
229- += tokens . cache_creation_input_tokens
230- * pricing . cache_creation_input_token_cost ;
256+ // Calculate cache creation cost
257+ if ( tokens . cache_creation_input_tokens != null && tokens . cache_creation_input_tokens > 0 ) {
258+ if ( tokens . cache_creation_input_tokens > CONTEXT_THRESHOLD && pricing . cache_creation_input_token_cost_above_200k_tokens != null ) {
259+ const tokensBelow200k = Math . min ( tokens . cache_creation_input_tokens , CONTEXT_THRESHOLD ) ;
260+ const tokensAbove200k = Math . max ( 0 , tokens . cache_creation_input_tokens - CONTEXT_THRESHOLD ) ;
261+
262+ if ( pricing . cache_creation_input_token_cost != null ) {
263+ cost += tokensBelow200k * pricing . cache_creation_input_token_cost ;
264+ }
265+ cost += tokensAbove200k * pricing . cache_creation_input_token_cost_above_200k_tokens ;
266+ }
267+ else if ( pricing . cache_creation_input_token_cost != null ) {
268+ cost += tokens . cache_creation_input_tokens * pricing . cache_creation_input_token_cost ;
269+ }
231270 }
232271
233- if ( tokens . cache_read_input_tokens != null && pricing . cache_read_input_token_cost != null ) {
234- cost
235- += tokens . cache_read_input_tokens * pricing . cache_read_input_token_cost ;
272+ // Calculate cache read cost
273+ if ( tokens . cache_read_input_tokens != null && tokens . cache_read_input_tokens > 0 ) {
274+ if ( tokens . cache_read_input_tokens > CONTEXT_THRESHOLD && pricing . cache_read_input_token_cost_above_200k_tokens != null ) {
275+ const tokensBelow200k = Math . min ( tokens . cache_read_input_tokens , CONTEXT_THRESHOLD ) ;
276+ const tokensAbove200k = Math . max ( 0 , tokens . cache_read_input_tokens - CONTEXT_THRESHOLD ) ;
277+
278+ if ( pricing . cache_read_input_token_cost != null ) {
279+ cost += tokensBelow200k * pricing . cache_read_input_token_cost ;
280+ }
281+ cost += tokensAbove200k * pricing . cache_read_input_token_cost_above_200k_tokens ;
282+ }
283+ else if ( pricing . cache_read_input_token_cost != null ) {
284+ cost += tokens . cache_read_input_tokens * pricing . cache_read_input_token_cost ;
285+ }
236286 }
237287
238288 return cost ;
@@ -303,5 +353,99 @@ if (import.meta.vitest != null) {
303353
304354 expect ( cost ) . toBeCloseTo ( ( 1000 * 1.25e-6 ) + ( 500 * 1e-5 ) + ( 200 * 1.25e-7 ) ) ;
305355 } ) ;
356+
357+ it ( 'calculates cost with 1M context window pricing for input tokens' , async ( ) => {
358+ using fetcher = new LiteLLMPricingFetcher ( {
359+ offline : true ,
360+ offlineLoader : async ( ) => ( {
361+ 'claude-4-sonnet-20250514' : {
362+ input_cost_per_token : 3e-6 ,
363+ output_cost_per_token : 1.5e-5 ,
364+ input_cost_per_token_above_200k_tokens : 6e-6 ,
365+ output_cost_per_token_above_200k_tokens : 2.25e-5 ,
366+ } ,
367+ } ) ,
368+ } ) ;
369+
370+ // Test with 300k input tokens (200k at normal rate, 100k at higher rate)
371+ const cost = await Result . unwrap ( fetcher . calculateCostFromTokens ( {
372+ input_tokens : 300_000 ,
373+ output_tokens : 1000 ,
374+ } , 'claude-4-sonnet-20250514' ) ) ;
375+
376+ const expectedCost = ( 200_000 * 3e-6 ) + ( 100_000 * 6e-6 ) + ( 1000 * 1.5e-5 ) ;
377+ expect ( cost ) . toBeCloseTo ( expectedCost ) ;
378+ } ) ;
379+
380+ it ( 'calculates cost with 1M context window pricing for output tokens' , async ( ) => {
381+ using fetcher = new LiteLLMPricingFetcher ( {
382+ offline : true ,
383+ offlineLoader : async ( ) => ( {
384+ 'claude-4-sonnet-20250514' : {
385+ input_cost_per_token : 3e-6 ,
386+ output_cost_per_token : 1.5e-5 ,
387+ input_cost_per_token_above_200k_tokens : 6e-6 ,
388+ output_cost_per_token_above_200k_tokens : 2.25e-5 ,
389+ } ,
390+ } ) ,
391+ } ) ;
392+
393+ // Test with 250k output tokens (200k at normal rate, 50k at higher rate)
394+ const cost = await Result . unwrap ( fetcher . calculateCostFromTokens ( {
395+ input_tokens : 1000 ,
396+ output_tokens : 250_000 ,
397+ } , 'claude-4-sonnet-20250514' ) ) ;
398+
399+ const expectedCost = ( 1000 * 3e-6 ) + ( 200_000 * 1.5e-5 ) + ( 50_000 * 2.25e-5 ) ;
400+ expect ( cost ) . toBeCloseTo ( expectedCost ) ;
401+ } ) ;
402+
403+ it ( 'calculates cost with 1M context window pricing for cache tokens' , async ( ) => {
404+ using fetcher = new LiteLLMPricingFetcher ( {
405+ offline : true ,
406+ offlineLoader : async ( ) => ( {
407+ 'claude-4-sonnet-20250514' : {
408+ input_cost_per_token : 3e-6 ,
409+ cache_creation_input_token_cost : 3.75e-6 ,
410+ cache_read_input_token_cost : 3e-7 ,
411+ cache_creation_input_token_cost_above_200k_tokens : 7.5e-6 ,
412+ cache_read_input_token_cost_above_200k_tokens : 6e-7 ,
413+ } ,
414+ } ) ,
415+ } ) ;
416+
417+ // Test with 300k cache creation and read tokens
418+ const cost = await Result . unwrap ( fetcher . calculateCostFromTokens ( {
419+ input_tokens : 1000 ,
420+ output_tokens : 0 ,
421+ cache_creation_input_tokens : 300_000 ,
422+ cache_read_input_tokens : 250_000 ,
423+ } , 'claude-4-sonnet-20250514' ) ) ;
424+
425+ const expectedCost = ( 1000 * 3e-6 )
426+ + ( 200_000 * 3.75e-6 ) + ( 100_000 * 7.5e-6 ) // cache creation
427+ + ( 200_000 * 3e-7 ) + ( 50_000 * 6e-7 ) ; // cache read
428+ expect ( cost ) . toBeCloseTo ( expectedCost ) ;
429+ } ) ;
430+
431+ it ( 'handles models without 1M context pricing correctly' , async ( ) => {
432+ using fetcher = new LiteLLMPricingFetcher ( {
433+ offline : true ,
434+ offlineLoader : async ( ) => ( {
435+ 'gpt-5' : {
436+ input_cost_per_token : 1e-6 ,
437+ output_cost_per_token : 2e-6 ,
438+ } ,
439+ } ) ,
440+ } ) ;
441+
442+ // Should use normal pricing for all tokens
443+ const cost = await Result . unwrap ( fetcher . calculateCostFromTokens ( {
444+ input_tokens : 300_000 ,
445+ output_tokens : 250_000 ,
446+ } , 'gpt-5' ) ) ;
447+
448+ expect ( cost ) . toBeCloseTo ( ( 300_000 * 1e-6 ) + ( 250_000 * 2e-6 ) ) ;
449+ } ) ;
306450 } ) ;
307451}
0 commit comments