Merge pull request 'hx stats: cache-write outliers and assistant-side bytes' (#109) from stats-writes into main
ci / check (push) Canceled after 0s

This commit was merged in pull request #109.
This commit is contained in:
hermes committed 2026-10-01 22:28:32 +00:00
commit 166d9fec87
1 file changed
+66
+66
View File
@@ -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}");
}
}