@@ -35,6 +35,31 @@ describe("CostCalculator", () => {
35
35
expect ( cost . outputCost ) . toEqual ( 75 ) ;
36
36
} ) ;
37
37
38
+ test ( "should calculate cost with cache tokens for opus-4 model" , ( ) => {
39
+ const models = calculator . listModels ( ) ;
40
+ const testModel = models . find ( ( m ) => m . includes ( "claude-opus-4-20250514" ) ) ;
41
+
42
+ expect ( testModel ) . toBeDefined ( ) ;
43
+
44
+ const cost = calculator . calculateCost (
45
+ testModel as string ,
46
+ /* input tokens */ 1_000_000 ,
47
+ /* output tokens */ 1_000_000 ,
48
+ /* cache creation tokens */ 1_000_000 ,
49
+ /* cache read tokens */ 1_000_000 ,
50
+ ) ;
51
+
52
+ expect ( cost . inputTokens ) . toBe ( 1_000_000 ) ;
53
+ expect ( cost . outputTokens ) . toBe ( 1_000_000 ) ;
54
+ expect ( cost . cacheCreationTokens ) . toBe ( 1_000_000 ) ;
55
+ expect ( cost . cacheReadTokens ) . toBe ( 1_000_000 ) ;
56
+ expect ( cost . inputCost ) . toEqual ( 15 ) ;
57
+ expect ( cost . outputCost ) . toEqual ( 75 ) ;
58
+ expect ( cost . cacheCreationCost ) . toEqual ( 18.75 ) ;
59
+ expect ( cost . cacheReadCost ) . toEqual ( 1.5 ) ;
60
+ expect ( cost . totalCost ) . toEqual ( 15 + 75 + 18.75 + 1.5 ) ;
61
+ } ) ;
62
+
38
63
test ( "should calculate cost for a sonnet-4 model" , ( ) => {
39
64
// Using sonnet-4 model as an example
40
65
// https://www.anthropic.com/pricing
@@ -57,6 +82,33 @@ describe("CostCalculator", () => {
57
82
expect ( cost . outputCost ) . toEqual ( 15 ) ;
58
83
} ) ;
59
84
85
+ test ( "should calculate cost with cache tokens for sonnet-4 model" , ( ) => {
86
+ const models = calculator . listModels ( ) ;
87
+ const testModel = models . find ( ( m ) =>
88
+ m . includes ( "claude-sonnet-4-20250514" ) ,
89
+ ) ;
90
+
91
+ expect ( testModel ) . toBeDefined ( ) ;
92
+
93
+ const cost = calculator . calculateCost (
94
+ testModel as string ,
95
+ /* input tokens */ 1_000_000 ,
96
+ /* output tokens */ 1_000_000 ,
97
+ /* cache creation tokens */ 1_000_000 ,
98
+ /* cache read tokens */ 1_000_000 ,
99
+ ) ;
100
+
101
+ expect ( cost . inputTokens ) . toBe ( 1_000_000 ) ;
102
+ expect ( cost . outputTokens ) . toBe ( 1_000_000 ) ;
103
+ expect ( cost . cacheCreationTokens ) . toBe ( 1_000_000 ) ;
104
+ expect ( cost . cacheReadTokens ) . toBe ( 1_000_000 ) ;
105
+ expect ( cost . inputCost ) . toEqual ( 3 ) ;
106
+ expect ( cost . outputCost ) . toEqual ( 15 ) ;
107
+ expect ( cost . cacheCreationCost ) . toEqual ( 3.75 ) ;
108
+ expect ( cost . cacheReadCost ) . toEqual ( 0.3 ) ;
109
+ expect ( cost . totalCost ) . toEqual ( 3 + 15 + 3.75 + 0.3 ) ;
110
+ } ) ;
111
+
60
112
test ( "should throw error for unknown model" , ( ) => {
61
113
expect ( ( ) => {
62
114
calculator . calculateCost ( "unknown-model" , 1000 ) ;
0 commit comments