Skip to content

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

fix(agent): correct cost tracking for subagent and flow operations

Steven Enamakel3b8cb401f20d
diff --git a/crates/openhuman-core/src/agent/context/stats.rs b/crates/openhuman-core/src/agent/context/stats.rs
index a3afcfe0f9..d73d2bdecb 100644
--- a/crates/openhuman-core/src/agent/context/stats.rs
+++ b/crates/openhuman-core/src/agent/context/stats.rs
@@ -39,8 +39,8 @@ impl ContextStatsState {
pub(crate) fn record_usage(&mut self, usage: &BilledUsage) {
self.last_input_tokens = usage.input_tokens;
self.last_output_tokens = usage.output_tokens;
- if usage.context_window > 0 {
- self.context_window = usage.context_window;
+ if usage.context_window() > 0 {
+ self.context_window = usage.context_window();
}
let total = usage.input_tokens + usage.output_tokens;
if let Ok(mut sm) = self.session_memory.lock() {
diff --git a/crates/openhuman-core/src/agent/cost.rs b/crates/openhuman-core/src/agent/cost.rs
index 135967a77a..24bb95870a 100644
--- a/crates/openhuman-core/src/agent/cost.rs
+++ b/crates/openhuman-core/src/agent/cost.rs
@@ -103,7 +103,7 @@ pub(crate) fn lookup_pricing(model: &str) -> ModelPricing {
/// usage. Used as a fallback when `charged_amount_usd` is missing.
pub fn estimate_call_cost_usd(model: &str, usage: &BilledUsage) -> f64 {
let pricing = lookup_pricing(model);
- let cached = usage.cached_input_tokens;
+ let cached = usage.cached_input_tokens();
let standard_input = usage.input_tokens.saturating_sub(cached);
let m = 1_000_000.0_f64;
(standard_input as f64) / m * pricing.input_per_mtok_usd
@@ -151,7 +151,7 @@ impl TurnCost {
self.output_tokens = self.output_tokens.saturating_add(usage.output_tokens);
self.cached_input_tokens = self
.cached_input_tokens
- .saturating_add(usage.cached_input_tokens);
+ .saturating_add(usage.cached_input_tokens());
if usage.charged_amount_usd > 0.0 {
self.charged_usd += usage.charged_amount_usd;
} else {
diff --git a/crates/openhuman-core/src/agent/session_host/driver/grounded_close.rs b/crates/openhuman-core/src/agent/session_host/driver/grounded_close.rs
index 0a38ec7f23..4eefb42b23 100644
--- a/crates/openhuman-core/src/agent/session_host/driver/grounded_close.rs
+++ b/crates/openhuman-core/src/agent/session_host/driver/grounded_close.rs
@@ -39,7 +39,7 @@ impl RepairUsage {
if let Some(usage) = usage {
self.input_tokens += usage.input_tokens;
self.output_tokens += usage.output_tokens;
- self.cached_input_tokens += usage.cached_input_tokens;
+ self.cached_input_tokens += usage.cached_input_tokens();
self.charged_amount_usd += usage.charged_amount_usd;
}
}
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 d64bcdeccb..978c45bce2 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
@@ -396,7 +396,7 @@ pub(in super::super) async fn run_subagent_via_graph(
// transcript meta / cost dashboard.
usage.input_tokens += u.input_tokens;
usage.output_tokens += u.output_tokens;
- usage.cached_input_tokens += u.cached_input_tokens;
+ usage.cached_input_tokens += u.cached_input_tokens();
let call_cost =
if u.charged_amount_usd.is_finite() && u.charged_amount_usd > 0.0 {
u.charged_amount_usd
@@ -405,19 +405,19 @@ pub(in super::super) async fn run_subagent_via_graph(
model,
u.input_tokens,
u.output_tokens,
- u.cached_input_tokens,
+ u.cached_input_tokens(),
)
};
usage.charged_amount_usd += call_cost;
crate::platform::cost::record_provider_usage(
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),
+ &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),
);
tracing::debug!(
agent_id,
input_tokens = u.input_tokens,
output_tokens = u.output_tokens,
- cached_input_tokens = u.cached_input_tokens,
+ cached_input_tokens = u.cached_input_tokens(),
call_cost,
"[subagent] cap-hit summary call folded + priced + recorded into cost tracker (#4467, item 2)"
);
diff --git a/crates/openhuman-core/src/agent/tinyagents/observability/event_bridge.rs b/crates/openhuman-core/src/agent/tinyagents/observability/event_bridge.rs
index e009f2a423..e518c12251 100644
--- a/crates/openhuman-core/src/agent/tinyagents/observability/event_bridge.rs
+++ b/crates/openhuman-core/src/agent/tinyagents/observability/event_bridge.rs
@@ -378,7 +378,7 @@ impl OpenhumanEventBridge {
// back to the catalogue window and the crate token counts when absent.
let context_window = carried
.as_ref()
- .map(|u| u.context_window)
+ .map(|u| u.context_window())
.filter(|w| *w > 0)
.unwrap_or_else(|| {
crate::platform::cost::catalog::lookup(&self.model)
diff --git a/crates/openhuman-core/src/flows/tinyflows/caps/prompt.rs b/crates/openhuman-core/src/flows/tinyflows/caps/prompt.rs
index 2c0e8112f9..9bfcba50f8 100644
--- a/crates/openhuman-core/src/flows/tinyflows/caps/prompt.rs
+++ b/crates/openhuman-core/src/flows/tinyflows/caps/prompt.rs
@@ -45,8 +45,8 @@ pub(crate) fn usage_to_json(usage: &Option<BilledUsage>) -> Value {
Some(u) => json!({
"input_tokens": u.input_tokens,
"output_tokens": u.output_tokens,
- "context_window": u.context_window,
- "cached_input_tokens": u.cached_input_tokens,
+ "context_window": u.context_window(),
+ "cached_input_tokens": u.cached_input_tokens(),
"cache_creation_tokens": u.cache_creation_tokens,
"reasoning_tokens": u.reasoning_tokens,
"charged_amount_usd": u.charged_amount_usd,
diff --git a/crates/openhuman-core/src/platform/cost/global.rs b/crates/openhuman-core/src/platform/cost/global.rs
index 24804b4475..c6e746ccb0 100644
--- a/crates/openhuman-core/src/platform/cost/global.rs
+++ b/crates/openhuman-core/src/platform/cost/global.rs
@@ -174,7 +174,7 @@ pub(super) fn build_token_usage(model: &str, usage: &BilledUsage) -> Option<Toke
input_tokens: usage.input_tokens,
output_tokens: usage.output_tokens,
total_tokens,
- cached_input_tokens: usage.cached_input_tokens.min(usage.input_tokens),
+ cached_input_tokens: usage.cached_input_tokens().min(usage.input_tokens),
cache_creation_tokens: usage.cache_creation_tokens,
reasoning_tokens: usage.reasoning_tokens,
cost_usd: if usage.charged_amount_usd.is_finite() && usage.charged_amount_usd >= 0.0 {