diff --git a/crates/hx-core/src/stats.rs b/crates/hx-core/src/stats.rs index 275594e..83b7a8c 100644 --- a/crates/hx-core/src/stats.rs +++ b/crates/hx-core/src/stats.rs @@ -36,6 +36,44 @@ pub fn report(s: &Session) -> String { total_in / n ); + // Requests that wrote a lot of cache: a regular spacing (every 8th turn) points at masking steps, + // irregular ones at cache expiry (idle > 5 min) or other prefix changes. + let writes: Vec<(usize, u64, u64)> = s + .usages() + .iter() + .enumerate() + .map(|(k, (_, u))| (k + 1, u.cache_write_tokens, u.cache_read_tokens)) + .collect(); + let avg_w = cw / n; + let mut big_w: Vec<_> = writes.iter().filter(|w| w.1 > 2 * avg_w.max(1)).collect(); + big_w.sort_by_key(|w| std::cmp::Reverse(w.1)); + let _ = writeln!( + o, + "requests writing > 2x the average cache write ({avg_w}): {} of {n}", + big_w.len() + ); + for (k, w, r) in big_w.iter().take(10) { + let _ = writeln!(o, " request {k}: write {w}, read {r}"); + } + + // assistant side: bytes the model produced that are resent every request + let (mut text_b, mut call_b) = (0usize, 0usize); + for m in msgs.iter().filter(|m| m.role == Role::Assistant) { + for b in &m.content { + match b { + ContentBlock::Text { text } => text_b += text.len(), + ContentBlock::ToolUse { input, .. } => call_b += input.to_string().len(), + _ => {} + } + } + } + let _ = writeln!( + o, + "assistant output kept in history: text {} tok, tool-call arguments {} tok (once)", + text_b / 4, + call_b / 4 + ); + // carried cost of tool results on the current path let mut names: HashMap<&str, &str> = HashMap::new(); for m in &msgs { @@ -226,4 +264,32 @@ mod tests { // calls, once, sent, unmasked assert!(n[2] < n[3] && n[2] > 0, "{row}"); } + + #[test] + fn reports_cache_writes_and_assistant_bytes() { + let d = tempfile::tempdir().unwrap(); + let mut s = Session::open(d.path().join("s.jsonl")).unwrap(); + s.append(Message::user("go")).unwrap(); + for i in 0..5 { + let u = Usage { + cache_write_tokens: if i == 3 { 10_000 } else { 100 }, + ..Default::default() + }; + s.append_full( + Message { + role: Role::Assistant, + content: vec![ContentBlock::Text { + text: "y".repeat(400), + }], + }, + None, + Some(u), + ) + .unwrap(); + s.append(Message::user("more")).unwrap(); + } + let r = report(&s); + assert!(r.contains("request 4: write 10000"), "{r}"); + assert!(r.contains("text 500 tok"), "{r}"); + } }