use serde_json::Value;
#[derive(Debug, Default)]
pub struct TokenCounts {
pub input_tokens: i64,
pub output_tokens: i64,
pub cache_read: i64,
pub cache_creation: i64,
pub cache_creation_5m: i64,
pub cache_creation_1h: i64,
pub total: i64,
}
pub fn extract_token_counts(usage: &Value) -> TokenCounts {
let mut counts = TokenCounts::default();
if let Some(usage_obj) = usage.as_object() {
if let Some(input) = usage_obj.get("input_tokens").and_then(|v| v.as_i64()) {
counts.input_tokens = input;
}
if let Some(output) = usage_obj.get("output_tokens").and_then(|v| v.as_i64()) {
counts.output_tokens = output;
}
if let Some(cache_read) = usage_obj
.get("cache_read_input_tokens")
.and_then(|v| v.as_i64())
{
counts.cache_read = cache_read;
}
if let Some(cache_creation) = usage_obj
.get("cache_creation_input_tokens")
.and_then(|v| v.as_i64())
{
counts.cache_creation = cache_creation;
}
if let Some(cc_split) = usage_obj.get("cache_creation").and_then(|v| v.as_object()) {
counts.cache_creation_5m = cc_split
.get("ephemeral_5m_input_tokens")
.and_then(|v| v.as_i64())
.unwrap_or(0);
counts.cache_creation_1h = cc_split
.get("ephemeral_1h_input_tokens")
.and_then(|v| v.as_i64())
.unwrap_or(0);
if counts.cache_creation == 0 {
counts.cache_creation = counts.cache_creation_5m + counts.cache_creation_1h;
}
} else {
counts.cache_creation_5m = counts.cache_creation;
}
if let Some(total_usage) = usage_obj
.get("total_token_usage")
.and_then(|v| v.as_object())
{
if let Some(input) = total_usage.get("input_tokens").and_then(|v| v.as_i64()) {
counts.input_tokens = input;
}
if let Some(output) = total_usage.get("output_tokens").and_then(|v| v.as_i64()) {
counts.output_tokens += output;
}
if let Some(reasoning) = total_usage
.get("reasoning_output_tokens")
.and_then(|v| v.as_i64())
{
counts.output_tokens += reasoning;
}
if let Some(cache_read) = total_usage
.get("cached_input_tokens")
.and_then(|v| v.as_i64())
{
counts.cache_read = cache_read;
}
if let Some(total) = total_usage.get("total_tokens").and_then(|v| v.as_i64()) {
counts.total = total;
return counts; }
}
counts.total =
counts.input_tokens + counts.output_tokens + counts.cache_read + counts.cache_creation;
}
counts
}
#[cfg(test)]
mod tests {
use super::*;
use serde_json::json;
#[test]
fn claude_format_without_ttl_split_defaults_to_5m() {
let usage = json!({
"input_tokens": 100,
"output_tokens": 50,
"cache_read_input_tokens": 200,
"cache_creation_input_tokens": 10_000
});
let c = extract_token_counts(&usage);
assert_eq!(c.cache_creation, 10_000);
assert_eq!(c.cache_creation_5m, 10_000);
assert_eq!(c.cache_creation_1h, 0);
}
#[test]
fn claude_code_ttl_split_is_preserved() {
let usage = json!({
"input_tokens": 6,
"output_tokens": 866,
"cache_read_input_tokens": 16_651,
"cache_creation_input_tokens": 10_338,
"cache_creation": {
"ephemeral_5m_input_tokens": 0,
"ephemeral_1h_input_tokens": 10_338
}
});
let c = extract_token_counts(&usage);
assert_eq!(c.cache_creation, 10_338);
assert_eq!(c.cache_creation_5m, 0);
assert_eq!(c.cache_creation_1h, 10_338);
}
#[test]
fn ttl_split_mixed_5m_and_1h() {
let usage = json!({
"cache_creation_input_tokens": 1_500,
"cache_creation": {
"ephemeral_5m_input_tokens": 500,
"ephemeral_1h_input_tokens": 1_000
}
});
let c = extract_token_counts(&usage);
assert_eq!(c.cache_creation, 1_500);
assert_eq!(c.cache_creation_5m, 500);
assert_eq!(c.cache_creation_1h, 1_000);
}
#[test]
fn ttl_split_derives_total_when_scalar_missing() {
let usage = json!({
"cache_creation": {
"ephemeral_5m_input_tokens": 200,
"ephemeral_1h_input_tokens": 300
}
});
let c = extract_token_counts(&usage);
assert_eq!(c.cache_creation, 500);
assert_eq!(c.cache_creation_5m, 200);
assert_eq!(c.cache_creation_1h, 300);
}
#[test]
fn codex_format_no_cache_creation() {
let usage = json!({
"total_token_usage": {
"input_tokens": 1000,
"output_tokens": 500,
"cached_input_tokens": 200,
"total_tokens": 1700
}
});
let c = extract_token_counts(&usage);
assert_eq!(c.cache_creation, 0);
assert_eq!(c.cache_creation_5m, 0);
assert_eq!(c.cache_creation_1h, 0);
assert_eq!(c.cache_read, 200);
}
}