Skip to content

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

refactor(subagent): track subagent cost provenance with CostTally

Steven Enamakelebd9de4db542
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 e1dd298528..071eae563c 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
@@ -25,7 +25,8 @@ pub(in super::super) struct AggregatedUsage {
pub(in super::super) input_tokens: u64,
pub(in super::super) output_tokens: u64,
pub(in super::super) cached_input_tokens: u64,
- pub(in super::super) charged_amount_usd: f64,
+ /// Cost of the run's calls: reported charge, catalog estimate, or unknown.
+ pub(in super::super) cost: crate::agent::cost::CostTally,
}
/// Drive a sub-agent turn on the tinyagents harness. Returns
@@ -298,12 +299,8 @@ pub(in super::super) async fn run_subagent_via_graph(
input_tokens: snapshot.input_tokens,
output_tokens: snapshot.output_tokens,
cached_input_tokens: snapshot.cached_input_tokens,
- charged_amount_usd: crate::platform::cost::catalog::estimate_cost_usd(
- model,
- snapshot.input_tokens,
- snapshot.output_tokens,
- snapshot.cached_input_tokens,
- ),
+ // Each answered call was priced as it arrived.
+ cost: snapshot.cost,
};
(recovered, unanswered, usage, snapshot.model_calls)
};
@@ -365,7 +362,7 @@ pub(in super::super) async fn run_subagent_via_graph(
// outcome now reports both) so sub-agent spend rolls into the parent
// instead of being recorded as uncached and $0.
cached_input_tokens: outcome.cached_input_tokens,
- charged_amount_usd: outcome.charged_amount_usd,
+ cost: outcome.cost,
};
// Cap hit with work still pending: summarize the run-so-far into a resumable
@@ -397,18 +394,8 @@ pub(in super::super) async fn run_subagent_via_graph(
usage.input_tokens += u.input_tokens;
usage.output_tokens += u.output_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
- } else {
- crate::platform::cost::catalog::estimate_cost_usd(
- model,
- u.input_tokens,
- u.output_tokens,
- u.cached_input_tokens(),
- )
- };
- usage.charged_amount_usd += call_cost;
+ let call_cost = crate::agent::cost::call_cost(model, &u);
+ usage.cost.add(call_cost);
let billed = crate::inference::provider::BilledUsage::from_counts(
u.input_tokens,
u.output_tokens,
@@ -417,13 +404,12 @@ pub(in super::super) async fn run_subagent_via_graph(
.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.cost_is_estimate
- && 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)
+ let billed = match call_cost {
+ crate::agent::cost::CallCost::Charged(usd) => billed.with_charged_usd(usd),
+ crate::agent::cost::CallCost::Estimated(usd) => {
+ billed.with_estimated_usd(usd)
+ }
+ crate::agent::cost::CallCost::Unknown => billed,
};
crate::platform::cost::record_provider_usage_scoped(
model,
diff --git a/crates/openhuman-core/src/agent/subagent_host/ops/runner.rs b/crates/openhuman-core/src/agent/subagent_host/ops/runner.rs
index 2eeb4a694c..9ce15779e6 100644
--- a/crates/openhuman-core/src/agent/subagent_host/ops/runner.rs
+++ b/crates/openhuman-core/src/agent/subagent_host/ops/runner.rs
@@ -1044,7 +1044,12 @@ async fn run_typed_mode(
input_tokens,
output_tokens,
cached_input_tokens,
- charged_amount_usd,
+ // A custom turn graph reports its own spend; it is
+ // taken as that graph's charge, never re-estimated.
+ cost: crate::agent::cost::CostTally {
+ known_usd: charged_amount_usd,
+ source: crate::agent::cost::CostSource::Charged,
+ },
},
res.early_exit_tool,
res.hit_cap,
@@ -1106,7 +1111,8 @@ async fn run_typed_mode(
input_tokens: agg_usage.input_tokens,
output_tokens: agg_usage.output_tokens,
cached_input_tokens: agg_usage.cached_input_tokens,
- charged_amount_usd: agg_usage.charged_amount_usd,
+ charged_amount_usd: agg_usage.cost.known_usd,
+ cost_source: agg_usage.cost.source,
};
// A nested child records on this run's isolated ledger. Fold those totals
// into the completed child before writing the immediate parent's ledger so
@@ -1121,6 +1127,7 @@ async fn run_typed_mode(
.cached_input_tokens
.saturating_add(entry.usage.cached_input_tokens);
usage.charged_amount_usd += entry.usage.charged_amount_usd;
+ usage.cost_source = usage.cost_source.max(entry.usage.cost_source);
}
Ok(SubagentRunOutcome {
task_id: task_id.to_string(),