fix: capture accurate usage #80

Merged
naomi merged 1 commits from fix/tokens into main 2026-01-29 13:34:38 -08:00
+16 -1
View File
@@ -704,7 +704,7 @@ fn process_json_line(
subtype,
result,
permission_denials,
usage: _,
usage,
..
} => {
let state = if subtype == "success" {
@@ -713,6 +713,21 @@ fn process_json_line(
CharacterState::Error
};
// Track token usage from Result messages if available
// This captures tokens from tool outputs and other operations
if let Some(usage_info) = usage {
// We need the model info to calculate cost properly
// For now, use the last known model from stats
let model = {
let stats_guard = stats.read();
stats_guard.model.clone().unwrap_or_else(|| "claude-opus-4-20250514".to_string())
};
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);
}
// Always emit updated stats on result message (less frequent)
// This includes the latest session duration
let newly_unlocked = {