Skip to content

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

feat(observability): bridge tinyagents turn events into cost scope

Steven Enamakel868d2ea18599
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 4b4a41a813..4f89760931 100644
--- a/crates/openhuman-core/src/agent/tinyagents/observability/event_bridge.rs
+++ b/crates/openhuman-core/src/agent/tinyagents/observability/event_bridge.rs
@@ -57,6 +57,10 @@ pub(crate) struct OpenhumanEventBridge {
pub(super) max_iterations: u32,
/// `None` for a parent turn; `Some` to emit child-scoped `Subagent*` events.
pub(super) scope: Option<SubagentScope>,
+ /// The agent definition the run acts as (its memory identity's agent),
+ /// attributed on usage records of a top-level turn. Set once by the turn
+ /// that owns the run ([`Self::set_definition`]).
+ pub(super) definition: std::sync::OnceLock<String>,
/// Shared with the model adapter so thinking deltas line up with the
/// model call (iteration) they belong to.
pub(super) cursor: IterationCursor,
@@ -160,6 +164,7 @@ impl OpenhumanEventBridge {
provider_id: provider_id.into(),
max_iterations: max_iterations as u32,
scope,
+ definition: std::sync::OnceLock::new(),
cursor,
tool_names,
failure_map,
@@ -173,6 +178,14 @@ impl OpenhumanEventBridge {
})
}
+ /// Names the agent definition this run acts as, for usage attribution. The
+ /// first call wins.
+ pub(crate) fn set_definition(&self, definition: Option<&str>) {
+ if let Some(definition) = definition.filter(|definition| !definition.is_empty()) {
+ let _ = self.definition.set(definition.to_owned());
+ }
+ }
+
/// Resolve `tool_name` against the turn's registered tool sets and
/// compute the presentation pair from the tool's OWN
/// [`tinytools::Tool::display_label`] / [`tinytools::Tool::display_detail`]
@@ -403,7 +416,8 @@ impl OpenhumanEventBridge {
crate::platform::cost::record_provider_usage_scoped(
&self.model,
&usage_info,
- crate::platform::cost::UsageScope::ambient(Some(&self.provider_id), subagent),
+ crate::platform::cost::UsageScope::ambient(Some(&self.provider_id), subagent)
+ .with_definition(self.definition.get().map(String::as_str)),
);
// The cost footer is a top-level surface; for a child run the global
diff --git a/crates/openhuman-core/src/agent/tinyagents/turn_exec.rs b/crates/openhuman-core/src/agent/tinyagents/turn_exec.rs
index 61d215b4b8..b0ca40288e 100644
--- a/crates/openhuman-core/src/agent/tinyagents/turn_exec.rs
+++ b/crates/openhuman-core/src/agent/tinyagents/turn_exec.rs
@@ -375,6 +375,12 @@ async fn drive_turn(
provider_usage_carry.clone(),
bridge_tool_sets.clone(),
);
+ bridge.set_definition(
+ run_context
+ .memory_identity
+ .as_ref()
+ .and_then(|identity| identity.agent_id.as_deref()),
+ );
sink.subscribe(bridge.clone());
bridge
};
diff --git a/crates/openhuman-core/src/platform/cost/scope.rs b/crates/openhuman-core/src/platform/cost/scope.rs
index 39c44d668b..0cc4aa068f 100644
--- a/crates/openhuman-core/src/platform/cost/scope.rs
+++ b/crates/openhuman-core/src/platform/cost/scope.rs
@@ -2,10 +2,11 @@
//!
//! Usage is recorded deep in the agent loop, where no caller passes thread or
//! agent ids down. They are already in scope as task-locals, though: the turn
-//! origin (thread, channel or job, bound on the turn's `CoreContext`), the memory identity (the agent definition
-//! acting) and the [`CoreContext`] (the embedded or SaaS user agent). The
-//! recording site adds what only it knows — the provider, and the sub-agent
-//! when a delegated child made the call.
+//! origin (thread, channel or job, bound on the turn's `CoreContext`) and the
+//! [`CoreContext`] (the embedded or SaaS user agent). The recording site adds
+//! what only it knows — the provider, the sub-agent when a delegated child made
+//! the call, and the agent definition acting when it holds the run
+//! ([`UsageScope::with_definition`]); the memory identity is not ambient.
//!
//! [`CoreContext`]: crate::core::runtime::CoreContext
@@ -19,13 +20,12 @@ impl UsageScope {
pub fn ambient(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 definition = crate::memory::scope::current().and_then(|identity| identity.agent_id);
let session_agent = crate::core::runtime::current_tenant()
.ok()
.and_then(|tenant| tenant.agent);
let (agent_id, subagent_task_id) = match subagent {
Some((agent, task)) => (Some(agent.to_string()), Some(task.to_string())),
- None => (definition, None),
+ None => (None, None),
};
Self {
thread_id,
@@ -38,6 +38,20 @@ impl UsageScope {
}
}
+impl UsageScope {
+ /// This scope attributed to the agent definition `definition` acting,
+ /// unless a sub-agent already names the agent.
+ #[must_use]
+ pub fn with_definition(mut self, definition: Option<&str>) -> Self {
+ if self.agent_id.is_none() {
+ self.agent_id = definition
+ .filter(|definition| !definition.is_empty())
+ .map(str::to_owned);
+ }
+ self
+ }
+}
+
/// The thread a turn origin names and its short label.
pub(crate) fn describe_origin(
origin: Option<&AgentTurnOrigin>,