Skip to content

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

feat(agent): emit model lifecycle events for supplemental calls

Steven Enamakel1dd393e7ba21
diff --git a/crates/openhuman-core/src/agent/session_host/driver/grounded_close.rs b/crates/openhuman-core/src/agent/session_host/driver/grounded_close.rs
index 5fef8854cb..5a991d7a83 100644
--- a/crates/openhuman-core/src/agent/session_host/driver/grounded_close.rs
+++ b/crates/openhuman-core/src/agent/session_host/driver/grounded_close.rs
@@ -7,11 +7,16 @@
//! its usage in a sidecar; it never owns transcript history or persistence.
use futures::StreamExt;
+use std::sync::{
+ Arc,
+ atomic::{AtomicUsize, Ordering},
+};
use tinyagents_runtime::transcript::message_to_native_transcript as message_to_native_chat_message;
use tinyagents_session::transcript::TranscriptMessage;
-use tinyinference_llm::model::{ModelRequest, ModelStreamItem};
+use tinyinference_llm::model::{CallId, ModelCallCorrelation, ModelRequest, ModelStreamItem};
use tinytools_agent::dialect::ToolDialect;
+use tinyagents_harness::{events::{AgentEvent, EventSink, GenerationEnrichment, GenerationPricing}, ids::RunId};
use tinyagents_harness::middleware::wrap_harness_instruction;
use crate::agent::{
@@ -43,6 +48,72 @@ pub(super) struct RepairUsage {
pub(super) last_call_output_tokens: u64,
}
+/// Event sink and run identity for model calls made after the harness loop.
+/// This is intentionally per-turn and emits at the source call boundary.
+pub(super) struct SupplementalTrace {
+ events: EventSink,
+ run_id: RunId,
+ next_call: AtomicUsize,
+}
+
+impl SupplementalTrace {
+ pub(super) fn new(events: EventSink, run_id: RunId) -> Arc<Self> {
+ Arc::new(Self { events, run_id, next_call: AtomicUsize::new(1) })
+ }
+
+ fn begin(&self, model: &str) -> (CallId, u64) {
+ let n = self.next_call.fetch_add(1, Ordering::Relaxed);
+ let call_id = CallId::new(format!("{}-host-supplemental-{n}", self.run_id.as_str()));
+ let started_at_ms = tinyagents_harness::ids::now_ms();
+ self.events.emit(AgentEvent::ModelStarted { call_id: call_id.clone(), model: model.into() });
+ (call_id, started_at_ms)
+ }
+
+ fn failed(&self, call_id: CallId, model: &str, started_at_ms: u64) {
+ self.events.emit(AgentEvent::ModelFailed {
+ call_id,
+ model: model.into(),
+ started_at_ms: Some(started_at_ms),
+ attempts: Some(1),
+ error: "supplemental model call failed".into(),
+ });
+ }
+
+ fn completed(&self, call_id: CallId, model: &str, started_at_ms: u64, input: serde_json::Value, response: &tinyinference_llm::model::ModelResponse) {
+ self.events.emit(AgentEvent::ModelCompleted {
+ call_id: call_id.clone(),
+ started_at_ms: Some(started_at_ms),
+ usage: response.usage,
+ input: Some(input),
+ output: Some(serde_json::to_value(&response.message).unwrap_or(serde_json::Value::Null)),
+ });
+ let route = response.resolved_route.as_ref();
+ let actual_model = route.map(|route| route.model.as_str())
+ .or_else(|| response.resolved_model.as_ref().map(|model| model.name.as_str()))
+ .unwrap_or(model);
+ let provider_id = route.map(|route| route.provider.as_str()).filter(|provider| valid_provider_id(provider)).map(str::to_owned);
+ let pricing = tinyagents_registry::pricing::lookup(actual_model).map(|price| 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),
+ });
+ let billed = crate::inference::provider::model::usage_info_from_response(response);
+ let estimated_cost_usd = if response.served_from_cache || billed.is_some_and(|usage| usage.charge_reported && !usage.cost_is_estimate) {
+ None
+ } else {
+ billed.and_then(|usage| crate::agent::cost::estimate_call_cost_usd(actual_model, &usage))
+ };
+ let enrichment = GenerationEnrichment { provider_id, pricing, estimated_cost_usd };
+ if enrichment.provider_id.is_some() || enrichment.pricing.is_some() || enrichment.estimated_cost_usd.is_some() {
+ self.events.emit(AgentEvent::GenerationEnriched { call_id, enrichment });
+ }
+ }
+}
+
+fn valid_provider_id(value: &str) -> bool {
+ !value.is_empty() && value.len() <= 64 && value.chars().all(|c| c.is_ascii_alphanumeric() || matches!(c, '_' | '.' | ':' | '-'))
+}
+
impl RepairUsage {
/// Empty accounting for calls made on `model`.
pub(super) fn for_model(model: &str) -> Self {