Skip to content

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

refactor(cost): thread dispatch context into usage scope

Steven Enamakel1e8b50385352
diff --git a/crates/openhuman-core/src/platform/cost/global.rs b/crates/openhuman-core/src/platform/cost/global.rs
index 659f2b20b8..5138a6b120 100644
--- a/crates/openhuman-core/src/platform/cost/global.rs
+++ b/crates/openhuman-core/src/platform/cost/global.rs
@@ -195,13 +195,13 @@ pub(crate) fn tracker_in(
/// `model` is the model identifier the request was routed to (e.g.
/// `"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) {
- record_provider_usage_scoped(model, usage, UsageScope::ambient(None, None));
+pub fn record_provider_usage(ctx: &DispatchCtx, model: &str, usage: &BilledUsage) {
+ record_provider_usage_scoped(model, usage, UsageScope::in_ctx(ctx, 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`]).
+/// else comes from the recording task (see [`UsageScope::in_ctx`]).
pub fn record_provider_usage_scoped(model: &str, usage: &BilledUsage, scope: UsageScope) {
let Some(mut token_usage) = build_token_usage(model, usage) else {
return;
@@ -291,6 +291,7 @@ pub(super) fn build_token_usage(model: &str, usage: &BilledUsage) -> Option<Toke
/// - `dimensions` — embedding vector dimensionality (logging context only).
/// - `vector_count` — number of vectors produced (logging context only).
pub fn record_embedding_usage(
+ ctx: &DispatchCtx,
provider: &str,
model: &str,
input_tokens: u64,
@@ -328,7 +329,7 @@ pub fn record_embedding_usage(
root_run_id: None,
scope: UsageScope {
origin: Some(super::report::EMBEDDING_ORIGIN.to_string()),
- ..UsageScope::ambient(Some(provider), None)
+ ..UsageScope::in_ctx(ctx, Some(provider), None)
},
timestamp: chrono::Utc::now(),
};
diff --git a/crates/openhuman-core/src/platform/cost/scope.rs b/crates/openhuman-core/src/platform/cost/scope.rs
index 0cc4aa068f..f3f49e6368 100644
--- a/crates/openhuman-core/src/platform/cost/scope.rs
+++ b/crates/openhuman-core/src/platform/cost/scope.rs
@@ -11,16 +11,22 @@
//! [`CoreContext`]: crate::core::runtime::CoreContext
use crate::agent::turn_origin::{AgentTurnOrigin, TrustedAutomationSource};
+use crate::core::runtime::DispatchCtx;
use super::types::UsageScope;
impl UsageScope {
- /// The scope of the calling task, with `provider` and the sub-agent (its
- /// definition and task id) supplied by the recording site.
- pub fn ambient(provider: Option<&str>, subagent: Option<(&str, &str)>) -> Self {
+ /// The scope of a call running under `ctx`, with `provider` and the
+ /// sub-agent (its definition and task id) supplied by the recording site.
+ /// The user agent comes from `ctx`'s tenant, not from task state.
+ pub fn in_ctx(
+ ctx: &DispatchCtx,
+ provider: Option<&str>,
+ subagent: Option<(&str, &str)>,
+ ) -> Self {
let origin = crate::core::runtime::CoreContext::current_turn_origin();
let (thread_id, origin) = describe_origin(origin.as_ref());
- let session_agent = crate::core::runtime::current_tenant()
+ let session_agent = ctx.tenant()
.ok()
.and_then(|tenant| tenant.agent);
let (agent_id, subagent_task_id) = match subagent {
diff --git a/crates/openhuman-core/src/platform/cost/types.rs b/crates/openhuman-core/src/platform/cost/types.rs
index 9aaed9b962..97d0fbab03 100644
--- a/crates/openhuman-core/src/platform/cost/types.rs
+++ b/crates/openhuman-core/src/platform/cost/types.rs
@@ -53,7 +53,7 @@ pub struct TokenUsage {
/// 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
+/// (`super::scope::UsageScope::in_ctx`), and a record from an older build
/// has none of them.
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct UsageScope {