Skip to content

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

feat(cost): attribute provider usage to sub-agents

Steven Enamakel1e3767dbb36a
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 dd14734295..159daaaf3c 100644
--- a/crates/openhuman-core/src/agent/tinyagents/observability/event_bridge.rs
+++ b/crates/openhuman-core/src/agent/tinyagents/observability/event_bridge.rs
@@ -451,7 +451,15 @@ impl OpenhumanEventBridge {
cache_creation_tokens
);
}
- crate::platform::cost::record_provider_usage(&self.model, &usage_info);
+ let subagent = self
+ .scope
+ .as_ref()
+ .map(|child| (child.agent_id.as_str(), child.task_id.as_str()));
+ crate::platform::cost::record_provider_usage_scoped(
+ &self.model,
+ &usage_info,
+ crate::platform::cost::UsageScope::ambient(Some(&self.provider_id), subagent),
+ );
// The cost footer is a top-level surface; for a child run the global
// cost tracker feed above is the authoritative accounting and the parent
diff --git a/crates/openhuman-core/src/platform/cost/global.rs b/crates/openhuman-core/src/platform/cost/global.rs
index b7e7be4aee..ef9553ed69 100644
--- a/crates/openhuman-core/src/platform/cost/global.rs
+++ b/crates/openhuman-core/src/platform/cost/global.rs
@@ -23,7 +23,7 @@ use crate::config::CostConfig;
use crate::inference::provider::types::BilledUsage;
use super::tracker::CostTracker;
-use super::types::{CostSource, TokenUsage};
+use super::types::{CostSource, TokenUsage, UsageScope};
static GLOBAL_TRACKER: RwLock<Option<Arc<CostTracker>>> = RwLock::new(None);
@@ -144,9 +144,24 @@ pub fn try_global() -> Option<Arc<CostTracker>> {
/// `"anthropic/claude-sonnet-4-20250514"`) and is used as the bucket key
/// in per-model aggregates.
pub fn record_provider_usage(model: &str, usage: &BilledUsage) {
- let Some(token_usage) = build_token_usage(model, usage) else {
+ record_provider_usage_scoped(model, usage, UsageScope::ambient(None, None));
+}
+
+/// [`record_provider_usage`] with the call's attribution. The event bridge
+/// passes its provider and, for a delegated child, the sub-agent; everything
+/// else comes from the recording task (see [`UsageScope::ambient`]).
+pub fn record_provider_usage_scoped(model: &str, usage: &BilledUsage, scope: UsageScope) {
+ let Some(mut token_usage) = build_token_usage(model, usage) else {
return;
};
+ log::trace!(
+ "[cost] attributing usage model={model} thread={:?} agent={:?} origin={:?} provider={:?}",
+ scope.thread_id,
+ scope.agent_id,
+ scope.origin,
+ scope.provider
+ );
+ token_usage.scope = scope;
let Some(tracker) = try_global() else {
return;
};
diff --git a/crates/openhuman-core/src/platform/cost/mod.rs b/crates/openhuman-core/src/platform/cost/mod.rs
index 89f72291a0..8873db7472 100644
--- a/crates/openhuman-core/src/platform/cost/mod.rs
+++ b/crates/openhuman-core/src/platform/cost/mod.rs
@@ -3,12 +3,14 @@ mod global;
pub mod route;
mod rpc;
mod schemas;
+pub mod scope;
pub mod tools;
pub mod tracker;
pub mod types;
pub use global::{
- init_global, rebind_global, record_embedding_usage, record_provider_usage, try_global,
+ init_global, rebind_global, record_embedding_usage, record_provider_usage,
+ record_provider_usage_scoped, try_global,
};
pub use route::{route_for_model, CostRoute};
pub use schemas::{
@@ -18,7 +20,7 @@ pub use schemas::{
pub use tracker::CostTracker;
pub use types::{
BudgetStatus, CostDashboard, CostRecord, CostSource, CostSummary, DailyCostEntry, ModelStats,
- TokenUsage,
+ TokenUsage, UsageScope,
};
/// Serialises tests that touch the process-global [`CostTracker`].