Skip to content

Commit b65043a

Browse files
fix(pricing): apply two-stage rates to whole request and per-model split
Co-authored-by: Codesmith <[email protected]>
1 parent 7d66785 commit b65043a

3 files changed

Lines changed: 106 additions & 6 deletions

File tree

‎rust/crates/ccusage/src/adapter/codex/aggregate.rs‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -336,8 +336,11 @@ fn accumulate_codex_event_into_group(
336336
model_usage.reasoning_output_tokens += event.reasoning_output_tokens;
337337
model_usage.total_tokens += event.total_tokens;
338338
// Each event is one request, so its input size decides the pricing tier
339-
// here; the summed totals cannot recover per-request context sizes.
340-
if event.input_tokens > crate::pricing::OPENAI_LONG_CONTEXT_THRESHOLD_TOKENS {
339+
// here; the summed totals cannot recover per-request context sizes. The
340+
// boundary is per model (OpenAI's 272K models and any future tier with a
341+
// different threshold) rather than a single global constant, matching the
342+
// threshold used to price the long-context buckets.
343+
if event.input_tokens > crate::pricing::long_context_split_threshold(model) {
341344
model_usage.long_context_input_tokens += event.input_tokens;
342345
model_usage.long_context_cached_input_tokens += event.cached_input_tokens;
343346
model_usage.long_context_output_tokens += event.output_tokens;

‎rust/crates/ccusage/src/cost.rs‎

Lines changed: 73 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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>(

‎rust/crates/ccusage/src/pricing.rs‎

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1345,6 +1345,19 @@ fn builtin_long_context_rates(base_model: &str) -> Option<LongContextRates> {
13451345
}
13461346
}
13471347

1348+
/// Input-token boundary above which a request is billed at a model's
1349+
/// long-context tier. Codex aggregates per-model token sums before pricing is
1350+
/// applied, so each request's tier must be decided during aggregation from the
1351+
/// model's own threshold rather than a single global constant. This returns the
1352+
/// same value `apply_builtin_long_context_rates` stamps onto
1353+
/// `Pricing::long_context_threshold`, and falls back to the default 200K
1354+
/// boundary used for LiteLLM `*_above_200k_tokens` data.
1355+
pub(crate) fn long_context_split_threshold(model: &str) -> u64 {
1356+
builtin_long_context_rates(model_without_date_suffix(model))
1357+
.map(|rates| rates.threshold)
1358+
.unwrap_or(DEFAULT_LONG_CONTEXT_THRESHOLD_TOKENS)
1359+
}
1360+
13481361
/// Strips a trailing release-date suffix (`-YYYY-MM-DD` or `-YYYYMMDD`) so
13491362
/// date-pinned pricing keys share their base model's long-context rates.
13501363
fn model_without_date_suffix(model: &str) -> &str {
@@ -1474,7 +1487,7 @@ fn fetch_json_url(url: &str) -> std::io::Result<String> {
14741487
mod tests {
14751488
use super::{
14761489
BUILD_TIME_MODELS_DEV_JSON, BUILD_TIME_PRICING_JSON, Pricing, PricingMap,
1477-
embedded_models_dev_pricing, model_without_date_suffix,
1490+
embedded_models_dev_pricing, long_context_split_threshold, model_without_date_suffix,
14781491
};
14791492
use ccusage_test_support::fs_fixture;
14801493
use std::sync::atomic::{AtomicUsize, Ordering};
@@ -2124,6 +2137,20 @@ mod tests {
21242137
assert_eq!(gpt_55.long_context_threshold, None);
21252138
}
21262139

2140+
#[test]
2141+
fn long_context_split_threshold_is_per_model() {
2142+
// OpenAI two-stage models switch tiers above 272K input tokens.
2143+
assert_eq!(long_context_split_threshold("gpt-5.6-sol"), 272_000);
2144+
assert_eq!(long_context_split_threshold("gpt-5.5"), 272_000);
2145+
assert_eq!(long_context_split_threshold("gpt-5.5-pro"), 272_000);
2146+
// Date-pinned keys share their base model's boundary.
2147+
assert_eq!(long_context_split_threshold("gpt-5.5-2026-04-23"), 272_000);
2148+
// Models without a built-in tier fall back to the 200K default used for
2149+
// LiteLLM `*_above_200k_tokens` data.
2150+
assert_eq!(long_context_split_threshold("gpt-5"), 200_000);
2151+
assert_eq!(long_context_split_threshold("gpt-5.4-mini"), 200_000);
2152+
}
2153+
21272154
#[test]
21282155
fn strips_model_date_suffixes() {
21292156
assert_eq!(model_without_date_suffix("gpt-5.5-2026-04-23"), "gpt-5.5");

0 commit comments

Comments
 (0)