fix: more accurate cost tracking hopefully

This commit is contained in:
2026-02-06 10:13:19 -08:00
parent c471c7ad3d
commit e8e9f5c79c
+19 -7
View File
@@ -717,16 +717,32 @@ fn process_json_line(
// Only update stats if we have usage information // Only update stats if we have usage information
if let Some(usage) = &message.usage { if let Some(usage) = &message.usage {
if let Some(model) = &message.model { // Get model from message, or fall back to last known model from stats
let model = message.model.clone().or_else(|| {
let stats_guard = stats.read();
stats_guard.model.clone()
}).unwrap_or_else(|| {
println!("[WARNING] No model info available for cost calculation, using default");
"claude-sonnet-4-5-20250929".to_string()
});
// Calculate cost for historical tracking (including cache tokens) // Calculate cost for historical tracking (including cache tokens)
let cost_usd = calculate_cost( let cost_usd = calculate_cost(
usage.input_tokens, usage.input_tokens,
usage.output_tokens, usage.output_tokens,
model, &model,
usage.cache_creation_input_tokens, usage.cache_creation_input_tokens,
usage.cache_read_input_tokens, usage.cache_read_input_tokens,
); );
println!("Assistant message tokens - input: {}, output: {}, cache_creation: {:?}, cache_read: {:?}, cost: ${:.4}",
usage.input_tokens,
usage.output_tokens,
usage.cache_creation_input_tokens,
usage.cache_read_input_tokens,
cost_usd
);
// Store cost for later use in output events // Store cost for later use in output events
message_cost = Some(MessageCost { message_cost = Some(MessageCost {
input_tokens: usage.input_tokens, input_tokens: usage.input_tokens,
@@ -741,7 +757,7 @@ fn process_json_line(
stats_guard.add_usage( stats_guard.add_usage(
usage.input_tokens, usage.input_tokens,
usage.output_tokens, usage.output_tokens,
model, &model,
usage.cache_creation_input_tokens, usage.cache_creation_input_tokens,
usage.cache_read_input_tokens, usage.cache_read_input_tokens,
); );
@@ -771,10 +787,6 @@ fn process_json_line(
// Just increment message count if no usage info // Just increment message count if no usage info
stats.write().increment_messages(); stats.write().increment_messages();
} }
} else {
// Just increment message count if no usage info
stats.write().increment_messages();
}
for block in &message.content { for block in &message.content {
match block { match block {