fix(cost): coerce off-peak rates, uplift multipliers, and media durations like Python

Off-peak rate strings are trimmed like float(), uplift multipliers go through
wire::py_float so padded strings and booleans convert, and any nonzero audio
or video duration replaces the per-token charge the way Python's truthiness
check does

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Yujong Lee 2026-09-23 19:39:54 -07:00
parent 923f32e4ce
commit 1437b87f38
6 changed files with 58 additions and 8 deletions

View file

@ -59,7 +59,7 @@ pub fn calculate_input_cost(
.unwrap_or(rates.cache_read);
let audio_rate_key = tier_key("input_cost_per_audio_token", service_tier);
let audio_cost = if details.audio_tokens > 0
&& !(details.audio_length_seconds > 0.0
&& !(details.audio_length_seconds != 0.0
&& model_info
.get("input_cost_per_audio_per_second")
.is_some_and(|value| !value.is_null()))
@ -87,7 +87,7 @@ pub fn calculate_input_cost(
0.0
};
let video_cost = if details.video_tokens > 0
&& !(details.video_length_seconds > 0.0
&& !(details.video_length_seconds != 0.0
&& model_info
.get("input_cost_per_video_per_second")
.is_some_and(|value| !value.is_null()))

View file

@ -117,7 +117,7 @@ pub fn open_off_peak_block(model_info: &Value, at: Timestamp) -> Option<&Value>
pub fn parse_off_peak_rate(value: Option<&Value>) -> Option<f64> {
match value? {
Value::Number(number) => number.as_f64(),
Value::String(number) => number.parse().ok(),
Value::String(number) => number.trim().parse().ok(),
_ => None,
}
}

View file

@ -1,11 +1,12 @@
use serde_json::Value;
use crate::wire::py_float;
fn multiplier(value: Option<&Value>) -> f64 {
match value {
Some(Value::Number(value)) => value.as_f64().unwrap_or(1.0),
Some(Value::String(value)) => value.parse().unwrap_or(1.0),
_ => 1.0,
}
value
.filter(|value| !value.is_null())
.and_then(py_float)
.unwrap_or(1.0)
}
pub fn get_regional_uplift_multiplier(model_info: &Value, data_residency: Option<&str>) -> f64 {

View file

@ -124,3 +124,24 @@ fn calculate_input_cost_uses_count_and_duration_instead_of_duplicate_token_charg
let actual = calculate_input_cost(&details, &model_info, rates(), None);
assert!((actual - expected).abs() < 1e-12);
}
#[rstest]
#[case::positive_seconds(3.0, 3.0 * 0.01)]
#[case::negative_seconds_still_replace_tokens(-1.0, 0.0)]
#[case::zero_seconds_bill_tokens(0.0, 300.0 * 1e-6)]
fn calculate_input_cost_treats_any_nonzero_audio_duration_as_reported(
#[case] seconds: f64,
#[case] expected: f64,
) {
let details = ParsedPromptDetails {
audio_tokens: 300,
audio_length_seconds: seconds,
..ParsedPromptDetails::default()
};
let model_info = json!({
"input_cost_per_audio_token": 1e-6,
"input_cost_per_audio_per_second": 0.01
});
let actual = calculate_input_cost(&details, &model_info, rates(), None);
assert!((actual - expected).abs() < 1e-12);
}

View file

@ -89,6 +89,9 @@ fn open_off_peak_block_requires_mapping_and_matching_window() {
#[case(json!(0), Some(0.0))]
#[case(json!(true), None)]
#[case(json!("bad"), None)]
#[case(json!(" 5e-7 "), Some(5e-7))]
#[case(json!(null), None)]
#[case(json!([1]), None)]
fn parse_off_peak_rate_accepts_numeric_strings_and_rejects_bools(
#[case] raw: Value,
#[case] expected: Option<f64>,

View file

@ -107,3 +107,28 @@ fn calculate_generic_cost_from_model_info_with_region_scales_both_sides() {
"generic totals exclude the provider-specific geo multiplier; only the anthropic wrapper applies it"
);
}
#[rstest]
#[case::padded_string(json!(" 1.5 "), 1.5)]
#[case::boolean_false(json!(false), 0.0)]
#[case::boolean_true(json!(true), 1.0)]
#[case::null(json!(null), 1.0)]
#[case::unparseable(json!("high"), 1.0)]
#[case::object(json!({"x": 2}), 1.0)]
fn uplift_multipliers_are_coerced_like_python_float(
#[case] raw: serde_json::Value,
#[case] expected: f64,
) {
let model_info = json!({
"regional_processing_uplift_multiplier_eu": raw,
"regional_endpoint_uplift_multiplier": raw
});
assert_eq!(
get_regional_uplift_multiplier(&model_info, Some("eu")),
expected
);
assert_eq!(
get_vertex_regional_endpoint_uplift(&model_info, Some("us-east5")),
expected
);
}