fix(cost): read Anthropic thinking and cache-creation detail counts as pydantic ints

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Yujong Lee 2026-09-23 20:07:46 -07:00
parent c8716673db
commit a56310c2df
2 changed files with 50 additions and 9 deletions

View file

@ -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<String, Value>) -> Option<u64> {
.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(

View file

@ -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<u64>,
#[case] expected_five_minute: Option<u64>,
) {
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
);
}