Skip to content

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

fix(cost): scope subagent usage and cache identity per provider and agent

Steven Enamakel1986447fd0fe
diff --git a/crates/openhuman-core/src/agent/subagent_host/ops/graph/dispatch.rs b/crates/openhuman-core/src/agent/subagent_host/ops/graph/dispatch.rs
index 2766acebab..89a0599fe2 100644
--- a/crates/openhuman-core/src/agent/subagent_host/ops/graph/dispatch.rs
+++ b/crates/openhuman-core/src/agent/subagent_host/ops/graph/dispatch.rs
@@ -183,6 +183,7 @@ pub(in super::super) async fn run_subagent_via_graph(
// (and the telemetry id) before `turn_models` is moved into the runner.
let native_tools = turn_models.native_tools();
let provider_id = turn_models.provider_id().to_string();
+ let usage_provider_id = provider_id.clone();
// `SubagentRunOptions` already carries the child context built at the tool
// dispatch boundary. Reusing that owned value preserves its immediate
// parent-ledger link; forking again here would hide nested child usage in a
@@ -408,17 +409,26 @@ pub(in super::super) async fn run_subagent_via_graph(
)
};
usage.charged_amount_usd += call_cost;
- crate::platform::cost::record_provider_usage(
+ let billed = crate::inference::provider::BilledUsage::from_counts(
+ u.input_tokens,
+ u.output_tokens,
+ )
+ .with_context_window(u.context_window())
+ .with_cached_input_tokens(u.cached_input_tokens())
+ .with_cache_creation_tokens(u.cache_creation_tokens)
+ .with_reasoning_tokens(u.reasoning_tokens);
+ let billed = if u.charged_amount_usd.is_finite() && u.charged_amount_usd > 0.0 {
+ billed.with_charged_usd(call_cost)
+ } else {
+ billed.with_estimated_usd(call_cost)
+ };
+ crate::platform::cost::record_provider_usage_scoped(
model,
- &crate::inference::provider::BilledUsage::from_counts(
- u.input_tokens,
- u.output_tokens,
- )
- .with_context_window(u.context_window())
- .with_cached_input_tokens(u.cached_input_tokens())
- .with_cache_creation_tokens(u.cache_creation_tokens)
- .with_reasoning_tokens(u.reasoning_tokens)
- .with_charged_usd(call_cost),
+ &billed,
+ crate::platform::cost::UsageScope::ambient(
+ Some(&usage_provider_id),
+ Some((agent_id, task_id)),
+ ),
);
tracing::debug!(
agent_id,
diff --git a/crates/openhuman-core/src/platform/cost/report.rs b/crates/openhuman-core/src/platform/cost/report.rs
index 34a4a08a66..6a4e32d8ff 100644
--- a/crates/openhuman-core/src/platform/cost/report.rs
+++ b/crates/openhuman-core/src/platform/cost/report.rs
@@ -257,7 +257,8 @@ pub struct CacheCall {
pub cached_input_tokens: u64,
pub cache_write_tokens: u64,
pub cache_hit_ratio: f64,
- /// A call after the first in its thread that read nothing from the cache.
+ /// A call after the first in its thread (same model and agent) that read
+ /// nothing from the cache.
pub cold: bool,
}
@@ -286,7 +287,9 @@ pub fn build_cache_report(
) -> CacheReport {
let mut ordered: Vec<&CostRecord> = records.iter().filter(|r| filter.admits(r)).collect();
ordered.sort_by_key(|r| r.usage.timestamp);
- let mut threads_seen: HashSet<String> = HashSet::new();
+ // A call is a repeat only against an earlier call with the same cache
+ // identity: a model switch or another agent prompt starts a fresh cache.
+ let mut caches_seen: HashSet<(String, String, Option<String>)> = HashSet::new();
let mut report = CacheReport {
from,
to,
@@ -301,9 +304,13 @@ pub fn build_cache_report(
let usage = &record.usage;
let cached = usage.cached_input_tokens.min(usage.input_tokens);
let thread = usage.scope.thread_id.clone();
- let repeat = thread
- .as_ref()
- .is_some_and(|t| !threads_seen.insert(t.clone()));
+ let repeat = thread.as_ref().is_some_and(|t| {
+ !caches_seen.insert((
+ t.clone(),
+ usage.model.clone(),
+ usage.scope.agent_id.clone(),
+ ))
+ });
let cold = repeat && cached == 0 && usage.input_tokens > 0;
if cold {
report.cold_calls += 1;