@@ -114,9 +114,34 @@ pub(crate) fn calculate_cost_from_pricing(usage: crate::TokenUsageRaw, pricing:
114114 let cache_create_1h_cost_above_200k = pricing
115115 . input_above_200k
116116 . map ( |c| c * CACHE_CREATE_1H_INPUT_MULTIPLIER ) ;
117- let threshold = pricing
118- . long_context_threshold
119- . unwrap_or ( crate :: pricing:: DEFAULT_LONG_CONTEXT_THRESHOLD_TOKENS ) ;
117+
118+ // OpenAI two-stage pricing: a per-model `long_context_threshold` means the
119+ // request's input size selects the tier and every bucket is billed entirely
120+ // at that tier's rate. The whole request switches once input exceeds the
121+ // threshold, so this is not a marginal breakpoint. This mirrors the Codex
122+ // per-request tiering in `calculate_codex_model_cost`.
123+ if let Some ( threshold) = pricing. long_context_threshold {
124+ let long_context = usage. input_tokens > threshold;
125+ let rate = |base : f64 , above : Option < f64 > | {
126+ if long_context {
127+ above. unwrap_or ( base)
128+ } else {
129+ base
130+ }
131+ } ;
132+ return usage. input_tokens as f64 * rate ( pricing. input , pricing. input_above_200k )
133+ + usage. output_tokens as f64 * rate ( pricing. output , pricing. output_above_200k )
134+ + cache_create_5m_tokens as f64
135+ * rate ( pricing. cache_create , pricing. cache_create_above_200k )
136+ + cache_create_1h_tokens as f64
137+ * rate ( cache_create_1h_cost, cache_create_1h_cost_above_200k)
138+ + usage. cache_read_input_tokens as f64
139+ * rate ( pricing. cache_read , pricing. cache_read_above_200k ) ;
140+ }
141+
142+ // LiteLLM `*_above_200k_tokens` data keeps its marginal above-threshold
143+ // semantics at the default 200K boundary.
144+ let threshold = crate :: pricing:: DEFAULT_LONG_CONTEXT_THRESHOLD_TOKENS ;
120145 tiered_cost (
121146 usage. input_tokens ,
122147 pricing. input ,
@@ -225,6 +250,51 @@ mod tests {
225250 assert ! ( ( cost - 12.5 ) . abs( ) < f64 :: EPSILON ) ;
226251 }
227252
253+ #[ test]
254+ fn prices_two_stage_model_as_whole_request_at_long_context_rates ( ) {
255+ let pricing = PricingMap :: load_embedded ( ) ;
256+
257+ // gpt-5.6-sol has a 272K threshold with long-context rates of
258+ // $10/$45 per 1M input/output tokens and a $1 per 1M cache-read rate.
259+ let long = TokenUsageRaw {
260+ input_tokens : 300_000 ,
261+ output_tokens : 1_000 ,
262+ cache_read_input_tokens : 100 ,
263+ ..TokenUsageRaw :: default ( )
264+ } ;
265+ let cost = calculate_cost_for_usage (
266+ Some ( "gpt-5.6-sol" ) ,
267+ long,
268+ None ,
269+ CostMode :: Calculate ,
270+ Some ( & pricing) ,
271+ ) ;
272+ // The whole request switches to long rates once input exceeds 272K,
273+ // including the output and cache-read buckets that are individually
274+ // far below the threshold: 3.0 + 0.045 + 0.0001.
275+ assert ! ( ( cost - 3.0451 ) . abs( ) < 1e-9 , "long-context cost was {cost}" ) ;
276+
277+ // Below the threshold every bucket stays on the short-context rates:
278+ // 0.5 + 0.03 + 0.00005.
279+ let short = TokenUsageRaw {
280+ input_tokens : 100_000 ,
281+ output_tokens : 1_000 ,
282+ cache_read_input_tokens : 100 ,
283+ ..TokenUsageRaw :: default ( )
284+ } ;
285+ let cost = calculate_cost_for_usage (
286+ Some ( "gpt-5.6-sol" ) ,
287+ short,
288+ None ,
289+ CostMode :: Calculate ,
290+ Some ( & pricing) ,
291+ ) ;
292+ assert ! (
293+ ( cost - 0.53005 ) . abs( ) < 1e-9 ,
294+ "short-context cost was {cost}"
295+ ) ;
296+ }
297+
228298 #[ test]
229299 fn parses_cache_creation_breakdown_from_usage_json ( ) {
230300 let usage = serde_json:: from_str :: < TokenUsageRaw > (
0 commit comments