show token usage on per-agent web ui after each turn
This commit is contained in:
parent
ca86bcf4bd
commit
ce740483c6
6 changed files with 91 additions and 1 deletions
|
|
@ -276,7 +276,34 @@ async fn run_claude(prompt: &str, files: &TurnFiles, bus: &Bus) -> Result<bool>
|
|||
flag_out.store(true, Ordering::Relaxed);
|
||||
}
|
||||
match serde_json::from_str::<serde_json::Value>(&line) {
|
||||
Ok(v) => bus_out.emit(LiveEvent::Stream(v)),
|
||||
Ok(v) => {
|
||||
// Extract token usage from the final `result` event and
|
||||
// store it in the bus for the web UI to surface.
|
||||
if v.get("type").and_then(|t| t.as_str()) == Some("result") {
|
||||
if let Some(u) = v.get("usage") {
|
||||
let usage = crate::events::TokenUsage {
|
||||
input_tokens: u
|
||||
.get("input_tokens")
|
||||
.and_then(|v| v.as_u64())
|
||||
.unwrap_or(0),
|
||||
output_tokens: u
|
||||
.get("output_tokens")
|
||||
.and_then(|v| v.as_u64())
|
||||
.unwrap_or(0),
|
||||
cache_read_input_tokens: u
|
||||
.get("cache_read_input_tokens")
|
||||
.and_then(|v| v.as_u64())
|
||||
.unwrap_or(0),
|
||||
cache_creation_input_tokens: u
|
||||
.get("cache_creation_input_tokens")
|
||||
.and_then(|v| v.as_u64())
|
||||
.unwrap_or(0),
|
||||
};
|
||||
bus_out.record_usage(usage);
|
||||
}
|
||||
}
|
||||
bus_out.emit(LiveEvent::Stream(v));
|
||||
}
|
||||
Err(_) => bus_out.emit(LiveEvent::Note(format!("(non-json) {line}"))),
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue