@@ -103,4 +103,60 @@ describe("provider usage extraction", () => {
103103 ( normalized . cacheWrite1hTokens ?? 0 )
104104 expect ( promptTokens ) . toBe ( 2600 )
105105 } )
106+
107+ // The invariant above is not OpenAI-specific: handler.ts adds the same four
108+ // buckets for every provider to pick the long-context tier. The wire shapes
109+ // disagree about whether the cache buckets live inside the prompt count --
110+ // OpenAI's input_tokens includes them, Anthropic's excludes them, Google has
111+ // no cache-write bucket at all -- so the guard belongs on the normalized
112+ // side, where all three must agree. Same 2,600-token prompt three ways.
113+ test ( "every helper partitions the prompt into the same four buckets" , ( ) => {
114+ const cases = [
115+ {
116+ name : "openai" ,
117+ expectedInput : 200 ,
118+ normalized : providers . openai . normalizeUsage ( {
119+ input_tokens : 2600 ,
120+ input_tokens_details : { cached_tokens : 2000 , cache_write_tokens : 400 } ,
121+ output_tokens : 10 ,
122+ } ) ,
123+ } ,
124+ {
125+ name : "anthropic" ,
126+ expectedInput : 200 ,
127+ normalized : providers . anthropic . normalizeUsage ( {
128+ input_tokens : 200 ,
129+ cache_read_input_tokens : 2000 ,
130+ cache_creation : { ephemeral_5m_input_tokens : 400 } ,
131+ output_tokens : 10 ,
132+ } ) ,
133+ } ,
134+ {
135+ name : "google" ,
136+ expectedInput : 600 ,
137+ normalized : providers . google . normalizeUsage (
138+ providers . google . extractUsage ( {
139+ usageMetadata : {
140+ promptTokenCount : 2600 ,
141+ cachedContentTokenCount : 2000 ,
142+ candidatesTokenCount : 10 ,
143+ } ,
144+ } ) ,
145+ ) ,
146+ } ,
147+ ]
148+
149+ for ( const { name, expectedInput, normalized } of cases ) {
150+ const promptTokens =
151+ normalized . inputTokens +
152+ ( normalized . cacheReadTokens ?? 0 ) +
153+ ( normalized . cacheWrite5mTokens ?? 0 ) +
154+ ( normalized . cacheWrite1hTokens ?? 0 )
155+ expect ( { name, inputTokens : normalized . inputTokens , promptTokens } ) . toEqual ( {
156+ name,
157+ inputTokens : expectedInput ,
158+ promptTokens : 2600 ,
159+ } )
160+ }
161+ } )
106162} )
0 commit comments