fix: include cache tokens in cost calculations

This commit is contained in:
2026-02-06 09:36:44 -08:00
parent 1492983ba9
commit 4aa538db43
3 changed files with 79 additions and 11 deletions
+36 -7
View File
@@ -604,8 +604,14 @@ fn process_json_line(
// Only update stats if we have usage information
if let Some(usage) = &message.usage {
if let Some(model) = &message.model {
// Calculate cost for historical tracking
let cost_usd = calculate_cost(usage.input_tokens, usage.output_tokens, model);
// Calculate cost for historical tracking (including cache tokens)
let cost_usd = calculate_cost(
usage.input_tokens,
usage.output_tokens,
model,
usage.cache_creation_input_tokens,
usage.cache_read_input_tokens,
);
// Store cost for later use in output events
message_cost = Some(MessageCost {
@@ -618,7 +624,13 @@ fn process_json_line(
{
let mut stats_guard = stats.write();
stats_guard.increment_messages();
stats_guard.add_usage(usage.input_tokens, usage.output_tokens, model);
stats_guard.add_usage(
usage.input_tokens,
usage.output_tokens,
model,
usage.cache_creation_input_tokens,
usage.cache_read_input_tokens,
);
stats_guard.get_session_duration();
// Attribute tokens to tools if any tools were used in this message
@@ -768,12 +780,29 @@ fn process_json_line(
stats_guard.model.clone().unwrap_or_else(|| "claude-opus-4-20250514".to_string())
};
// Calculate cost for historical tracking
let cost_usd = calculate_cost(usage_info.input_tokens, usage_info.output_tokens, &model);
// Calculate cost for historical tracking (including cache tokens)
let cost_usd = calculate_cost(
usage_info.input_tokens,
usage_info.output_tokens,
&model,
usage_info.cache_creation_input_tokens,
usage_info.cache_read_input_tokens,
);
let mut stats_guard = stats.write();
stats_guard.add_usage(usage_info.input_tokens, usage_info.output_tokens, &model);
println!("Result message tokens - input: {}, output: {}", usage_info.input_tokens, usage_info.output_tokens);
stats_guard.add_usage(
usage_info.input_tokens,
usage_info.output_tokens,
&model,
usage_info.cache_creation_input_tokens,
usage_info.cache_read_input_tokens,
);
println!("Result message tokens - input: {}, output: {}, cache_creation: {:?}, cache_read: {:?}",
usage_info.input_tokens,
usage_info.output_tokens,
usage_info.cache_creation_input_tokens,
usage_info.cache_read_input_tokens
);
// Record to historical cost tracking
let app_clone = app.clone();