Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

fix(cost): treat calls before the report window as cache warm-up

Steven Enamakel4754e7c86360
diff --git a/crates/openhuman-core/src/platform/cost/report.rs b/crates/openhuman-core/src/platform/cost/report.rs
index 412e620cf6..9028dfc8bc 100644
--- a/crates/openhuman-core/src/platform/cost/report.rs
+++ b/crates/openhuman-core/src/platform/cost/report.rs
@@ -86,6 +86,10 @@ impl GroupKey {
}
}
+/// How far before a cache report's window its records are read, to know
+/// which threads were already under way when the window opened.
+pub const CACHE_LOOKBACK: chrono::Duration = chrono::Duration::hours(24);
+
/// The `origin` of an embedding batch's record.
pub const EMBEDDING_ORIGIN: &str = "embedding";
@@ -283,7 +287,10 @@ pub struct CacheReport {
pub uncached_premium_usd: f64,
}
-/// The cache behaviour of `records` that pass `filter`, oldest call first.
+/// The cache behaviour of the `records` in `[from, to]` that pass `filter`,
+/// oldest call first. Records from before `from` are not reported; they only
+/// tell which caches a call in range could already have found warm (see
+/// [`CACHE_LOOKBACK`]).
pub fn build_cache_report(
records: &[CostRecord],
from: DateTime<Utc>,
@@ -321,8 +328,24 @@ pub fn build_cache_report(
};
for record in ordered {
let usage = &record.usage;
+ if usage.timestamp > to {
+ continue;
+ }
let cached = usage.cached_input_tokens.min(usage.input_tokens);
let thread = usage.scope.thread_id.clone();
+ // A call from before the window only marks its cache as seen, so a
+ // thread that began earlier is a repeat from its first call in range.
+ if usage.timestamp < from {
+ if let Some(t) = thread {
+ caches_seen.insert((
+ t,
+ usage.model.clone(),
+ usage.scope.agent_id.clone(),
+ usage.scope.provider.clone(),
+ ));
+ }
+ continue;
+ }
let repeat = thread.as_ref().is_some_and(|t| {
!caches_seen.insert((
t.clone(),
diff --git a/crates/openhuman-core/src/platform/cost/report_tests.rs b/crates/openhuman-core/src/platform/cost/report_tests.rs
index 0213d9bb93..6668596bf5 100644
--- a/crates/openhuman-core/src/platform/cost/report_tests.rs
+++ b/crates/openhuman-core/src/platform/cost/report_tests.rs
@@ -363,3 +363,15 @@ fn a_misspelt_filter_key_does_not_deserialize() {
}));
assert!(parsed.is_err());
}
+
+#[test]
+fn a_thread_begun_before_the_window_is_already_seen() {
+ let (from, to) = window();
+ // September 30th: before the window, only seeds the thread's cache.
+ let mut before = record((1, 9), "model/a", Some("t9"), Some("x"), 1000, 0, 0.1, true);
+ before.usage.timestamp = from - chrono::Duration::hours(2);
+ let inside = record((1, 9), "model/a", Some("t9"), Some("x"), 1000, 0, 0.1, true);
+ let report = build_cache_report(&[before, inside], from, to, &ReportFilter::default());
+ assert_eq!(report.calls.len(), 1, "the earlier call is not reported");
+ assert_eq!(report.cold_calls, 1, "its first call in range is a repeat");
+}
diff --git a/crates/openhuman-core/src/platform/cost/rpc.rs b/crates/openhuman-core/src/platform/cost/rpc.rs
index 658ce0f907..3e00bc5097 100644
--- a/crates/openhuman-core/src/platform/cost/rpc.rs
+++ b/crates/openhuman-core/src/platform/cost/rpc.rs
@@ -514,7 +514,7 @@ pub fn cache_report(
let tracker = resolve_tracker(config)?;
let (from, to) = report_window(days);
let records = tracker
- .records_between(from, to)
+ .records_between(from - super::report::CACHE_LOOKBACK, to)
.context("cost cache report query failed")?;
let mut report = super::report::build_cache_report(&records, from, to, filter);
let keep = limit.clamp(1, 1000);