diff --git a/litellm-rust/crates/cost/src/anthropic_usage.rs b/litellm-rust/crates/cost/src/anthropic_usage.rs index f4e0829eff7..d9f72204a06 100644 --- a/litellm-rust/crates/cost/src/anthropic_usage.rs +++ b/litellm-rust/crates/cost/src/anthropic_usage.rs @@ -3,6 +3,8 @@ use std::collections::BTreeMap; use serde_json::{Map, Value}; +use crate::wire::lax_count; + use crate::responses_usage::{ CacheCreationTokenDetails, ChatUsage, CompletionTokenDetails, PromptTokenDetails, }; @@ -64,15 +66,16 @@ fn cache_creation_details( let Some(details) = usage.get("cache_creation").and_then(Value::as_object) else { return Ok(None); }; + let pydantic_count = |field: &str| { + details + .get(field) + .filter(|value| !value.is_null()) + .map(|value| lax_count(value).ok_or(CostError::InvalidUsage)) + .transpose() + }; Ok(Some(CacheCreationTokenDetails { - ephemeral_5m_input_tokens: details - .get("ephemeral_5m_input_tokens") - .map(|value| count(Some(value))) - .transpose()?, - ephemeral_1h_input_tokens: details - .get("ephemeral_1h_input_tokens") - .map(|value| count(Some(value))) - .transpose()?, + ephemeral_5m_input_tokens: pydantic_count("ephemeral_5m_input_tokens")?, + ephemeral_1h_input_tokens: pydantic_count("ephemeral_1h_input_tokens")?, })) } @@ -81,7 +84,7 @@ fn thinking_tokens(usage: &Map) -> Option { .get("output_tokens_details") .and_then(Value::as_object) .and_then(|details| details.get("thinking_tokens")) - .and_then(Value::as_u64) + .and_then(lax_count) } pub fn transform_anthropic_usage_to_chat_usage( diff --git a/litellm-rust/crates/cost/tests/python_anthropic_usage.rs b/litellm-rust/crates/cost/tests/python_anthropic_usage.rs index 5f07cd59664..05d23e4688f 100644 --- a/litellm-rust/crates/cost/tests/python_anthropic_usage.rs +++ b/litellm-rust/crates/cost/tests/python_anthropic_usage.rs @@ -155,3 +155,41 @@ fn iteration_sums_overflow_errors_where_python_yields_a_big_int() { "divergence: Python sums arbitrary-precision ints and returns a huge cost" ); } + +#[rstest] +#[case::integer(json!(5), Some(5), Some(5))] +#[case::numeric_string(json!("5"), Some(5), Some(5))] +#[case::integral_float(json!(5.0), Some(5), Some(5))] +fn calculate_usage_reads_detail_counts_with_pydantic_lax_ints( + #[case] count: Value, + #[case] expected_thinking: Option, + #[case] expected_five_minute: Option, +) { + let usage = transform_anthropic_usage_to_chat_usage( + &json!({ + "input_tokens": 10, + "output_tokens": 20, + "cache_creation_input_tokens": 5, + "cache_creation": {"ephemeral_5m_input_tokens": count}, + "output_tokens_details": {"thinking_tokens": count} + }), + None, + true, + ) + .unwrap(); + assert_eq!( + usage + .completion_tokens_details + .as_ref() + .and_then(|details| details.reasoning_tokens), + expected_thinking + ); + assert_eq!( + usage + .prompt_tokens_details + .as_ref() + .and_then(|details| details.cache_creation_token_details.as_ref()) + .and_then(|details| details.ephemeral_5m_input_tokens), + expected_five_minute + ); +}