Skip to content

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

feat(agent): emit generation enrichment with provider and pricing data

Steven Enamakelcf6f646af394
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 791c889335..3ff0df03c9 100644
--- a/crates/openhuman-core/src/agent/tinyagents/observability/event_bridge.rs
+++ b/crates/openhuman-core/src/agent/tinyagents/observability/event_bridge.rs
@@ -17,6 +17,14 @@ use super::bridge_types::{ProviderUsageCarry, SubagentScope, ToolFailureMap, Too
use tinyagents_harness::events::{CallDedupe, IterationCursor, OrderedSender};
use tinyagents_harness::events::{AgentEvent, EventSink};
+fn valid_provider_id(value: &str) -> bool {
+ !value.is_empty()
+ && value.len() <= 64
+ && value
+ .chars()
+ .all(|character| character.is_ascii_alphanumeric() || matches!(character, '_' | '.' | ':' | '-'))
+}
+
#[derive(Default)]
pub(super) struct BridgeState {
pub(super) input_tokens: u64,
@@ -226,6 +234,32 @@ impl OpenhumanEventBridge {
}
}
+ pub(super) fn base_generation_enrichment(
+ &self,
+ ) -> tinyagents_harness::events::GenerationEnrichment {
+ let provider_id = valid_provider_id(&self.provider_id).then(|| self.provider_id.clone());
+ let pricing = if self.model.trim().to_ascii_lowercase().ends_with(":free") {
+ Some(tinyagents_harness::events::GenerationPricing {
+ input_per_mtok_usd: Some(0.0),
+ cached_input_per_mtok_usd: Some(0.0),
+ output_per_mtok_usd: Some(0.0),
+ })
+ } else {
+ tinyagents_registry::pricing::lookup(&self.model).map(|price| {
+ tinyagents_harness::events::GenerationPricing {
+ input_per_mtok_usd: Some(price.input_per_mtok_usd),
+ cached_input_per_mtok_usd: Some(price.cached_input_per_mtok_usd),
+ output_per_mtok_usd: Some(price.output_per_mtok_usd),
+ }
+ })
+ };
+ tinyagents_harness::events::GenerationEnrichment {
+ provider_id,
+ pricing,
+ estimated_cost_usd: None,
+ }
+ }
+
/// 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`]
@@ -369,6 +403,22 @@ impl OpenhumanEventBridge {
},
};
let call_cost = cost.usd();
+ if let tinyagents_harness::cost::CallCost::Estimated(estimated_cost_usd) = cost
+ && let Some(call_id) = self
+ .call_ids
+ .lock()
+ .unwrap_or_else(std::sync::PoisonError::into_inner)
+ .get(&iteration)
+ .cloned()
+ {
+ self.emit_generation_enrichment(
+ call_id,
+ tinyagents_harness::events::GenerationEnrichment {
+ estimated_cost_usd: Some(estimated_cost_usd),
+ ..Default::default()
+ },
+ );
+ }
// The context window + cache-creation/reasoning breakdown only exist on
// the carried provider usage (the crate `Usage` mapping drops them); fall
// back to the catalogue window and the crate token counts when absent.
diff --git a/crates/openhuman-core/src/agent/tinyagents/observability/event_projection.rs b/crates/openhuman-core/src/agent/tinyagents/observability/event_projection.rs
index 3848a6acea..2728c40858 100644
--- a/crates/openhuman-core/src/agent/tinyagents/observability/event_projection.rs
+++ b/crates/openhuman-core/src/agent/tinyagents/observability/event_projection.rs
@@ -14,8 +14,16 @@ use super::event_bridge::OpenhumanEventBridge;
impl EventListener for OpenhumanEventBridge {
fn on_event(&self, record: &EventRecord) {
match &record.event {
- AgentEvent::ModelStarted { .. } => {
+ AgentEvent::ModelStarted { call_id, .. } => {
let iteration = tinyagents_harness::events::advance_iteration(&self.cursor);
+ self.call_ids
+ .lock()
+ .unwrap_or_else(std::sync::PoisonError::into_inner)
+ .insert(iteration, call_id.clone());
+ self.emit_generation_enrichment(
+ call_id.clone(),
+ self.base_generation_enrichment(),
+ );
match &self.scope {
None => self.send(AgentProgress::IterationStarted {
iteration,
@@ -143,6 +151,10 @@ impl EventListener for OpenhumanEventBridge {
..
} => {
let iteration = self.iteration();
+ self.call_ids
+ .lock()
+ .unwrap_or_else(std::sync::PoisonError::into_inner)
+ .remove(&iteration);
let usage = usage.unwrap_or_default();
// Prefer the figures `record_usage` resolved for this call
// (charged>estimate cost + carried cache/reasoning breakdown —
diff --git a/crates/openhuman-core/src/agent/tinyagents/turn_exec.rs b/crates/openhuman-core/src/agent/tinyagents/turn_exec.rs
index c2a6b11b74..bdde2ce640 100644
--- a/crates/openhuman-core/src/agent/tinyagents/turn_exec.rs
+++ b/crates/openhuman-core/src/agent/tinyagents/turn_exec.rs
@@ -451,6 +451,7 @@ async fn drive_turn(
.as_ref()
.and_then(|identity| identity.agent_id.as_deref()),
);
+ bridge.set_native_events(sink.clone());
sink.subscribe(bridge.clone());
bridge
};