Skip to content

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

feat(cost): add usage scope attribution to token usage records

Steven Enamakel8a050004d7bd
diff --git a/crates/openhuman-core/src/platform/cost/global.rs b/crates/openhuman-core/src/platform/cost/global.rs
index c6e746ccb0..b7e7be4aee 100644
--- a/crates/openhuman-core/src/platform/cost/global.rs
+++ b/crates/openhuman-core/src/platform/cost/global.rs
@@ -194,6 +194,7 @@ pub(super) fn build_token_usage(model: &str, usage: &BilledUsage) -> Option<Toke
// `tinyagents/observability.rs`.
run_id: None,
root_run_id: None,
+ scope: UsageScope::default(),
timestamp: chrono::Utc::now(),
})
}
@@ -256,6 +257,7 @@ pub fn record_embedding_usage(
cost_source: CostSource::Estimated,
run_id: None,
root_run_id: None,
+ scope: UsageScope::default(),
timestamp: chrono::Utc::now(),
};
log::debug!(
diff --git a/crates/openhuman-core/src/platform/cost/types.rs b/crates/openhuman-core/src/platform/cost/types.rs
index 4bdc1b207d..63652d233d 100644
--- a/crates/openhuman-core/src/platform/cost/types.rs
+++ b/crates/openhuman-core/src/platform/cost/types.rs
@@ -40,10 +40,51 @@ pub struct TokenUsage {
/// See [`Self::run_id`]. Additive, optional, defaults to `None`.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub root_run_id: Option<String>,
+ /// Who and what this call was for: thread, agent, provider, origin. Lets
+ /// reports group spend and cache hits by more than the model. Additive and
+ /// skipped when empty, so records written before it still load and old
+ /// consumers see no change.
+ #[serde(default, skip_serializing_if = "UsageScope::is_empty")]
+ pub scope: UsageScope,
/// Timestamp of the request
pub timestamp: chrono::DateTime<chrono::Utc>,
}
+/// Attribution of one recorded model call (see [`TokenUsage::scope`]).
+///
+/// Every field is optional: each is filled from what the recording site knows
+/// (`super::scope::UsageScope::ambient`), and a record from an older build
+/// has none of them.
+#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
+pub struct UsageScope {
+ /// The conversation thread the call served.
+ #[serde(default, skip_serializing_if = "Option::is_none")]
+ pub thread_id: Option<String>,
+ /// What started the turn: `web_chat`, `channel:<name>`, `cron`,
+ /// `background`, `workflow`, `cli`, `direct_chat`.
+ #[serde(default, skip_serializing_if = "Option::is_none")]
+ pub origin: Option<String>,
+ /// The agent definition that made the call (`orchestrator`, `planner`, …).
+ #[serde(default, skip_serializing_if = "Option::is_none")]
+ pub agent_id: Option<String>,
+ /// The sub-agent task, when a delegated child made the call.
+ #[serde(default, skip_serializing_if = "Option::is_none")]
+ pub subagent_task_id: Option<String>,
+ /// The embedded agent (in SaaS mode, the user) the call ran for.
+ #[serde(default, skip_serializing_if = "Option::is_none")]
+ pub session_agent: Option<String>,
+ /// The inference provider that served it.
+ #[serde(default, skip_serializing_if = "Option::is_none")]
+ pub provider: Option<String>,
+}
+
+impl UsageScope {
+ /// Whether nothing is attributed.
+ pub fn is_empty(&self) -> bool {
+ self == &Self::default()
+ }
+}
+
/// Source of a cost value persisted in [`TokenUsage`].
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
#[serde(rename_all = "snake_case")]
@@ -92,6 +133,7 @@ impl TokenUsage {
cost_source: CostSource::Estimated,
run_id: None,
root_run_id: None,
+ scope: UsageScope::default(),
timestamp: chrono::Utc::now(),
}
}