The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.
Rust agent-orchestrationai-agentsai-assistantdesktopllmlocal-firstmcppersonal-aiprivacyrustsecond-braintauri
refactor(agent): move turn cost accounting into the harness
Steven Enamakele52ec803a8f7
diff --git a/crates/openhuman-core/src/agent/cost.rs b/crates/openhuman-core/src/agent/cost.rsindex 7efbe7ec07..b548a38f36 100644--- a/crates/openhuman-core/src/agent/cost.rs+++ b/crates/openhuman-core/src/agent/cost.rs@@ -17,7 +17,7 @@//! $4.25 when the provider billed about $0.30.use crate::inference::provider::BilledUsage;-use tinyagents_harness::cost::{CallCost, CallUsage, CostTally};+use tinyagents_harness::cost::{CallCost, CallUsage};/// Whether a model names an explicitly free OpenRouter variant.pub(crate) fn is_free_model(model: &str) -> bool {@@ -62,38 +62,12 @@ pub fn call_cost(model: &str, usage: &BilledUsage) -> CallCost {tinyagents_harness::cost::call_cost(lookup_pricing(model).as_ref(), &call_usage(usage))}-/// Running cost / token tally across every provider call inside a-/// single turn of the tool-call loop.-#[derive(Debug, Clone, Default)]-pub struct TurnCost {- pub input_tokens: u64,- pub output_tokens: u64,- pub cached_input_tokens: u64,- pub cost: CostTally,- pub call_count: u32,-}--impl TurnCost {- /// New empty accumulator.- pub fn new() -> Self {- Self::default()- }-- /// Fold a single provider call's usage into the running totals.- pub fn add_call(&mut self, model: &str, usage: &BilledUsage) {- self.input_tokens = self.input_tokens.saturating_add(usage.input_tokens);- self.output_tokens = self.output_tokens.saturating_add(usage.output_tokens);- self.cached_input_tokens = self- .cached_input_tokens- .saturating_add(usage.cached_input_tokens());- self.cost.add(call_cost(model, usage));- self.call_count = self.call_count.saturating_add(1);- }-- /// The turn's cost so far, or `None` when any call's cost is unknown.- pub fn total_usd(&self) -> Option<f64> {- self.cost.usd()- }+/// A turn's accumulator, priced with the product catalog: what the stop-hook+/// chain starts every run from.+pub(crate) fn turn_cost() -> tinyagents_harness::cost::TurnCost {+ tinyagents_harness::cost::TurnCost::with_pricing(std::sync::Arc::new(|model: &str| {+ lookup_pricing(model)+ }))}#[cfg(test)]diff --git a/crates/openhuman-core/src/agent/goals/runtime.rs b/crates/openhuman-core/src/agent/goals/runtime.rsindex f925366c9e..7c43f1c910 100644--- a/crates/openhuman-core/src/agent/goals/runtime.rs+++ b/crates/openhuman-core/src/agent/goals/runtime.rs@@ -247,12 +247,12 @@ impl GoalBudgetStopHook {}#[async_trait]-impl StopHook<crate::agent::cost::TurnCost> for GoalBudgetStopHook {+impl StopHook<tinyagents_harness::cost::TurnCost> for GoalBudgetStopHook {fn name(&self) -> &str {"thread_goal_budget"}- async fn check(&self, ctx: &TurnState<'_, crate::agent::cost::TurnCost>) -> StopDecision {+ async fn check(&self, ctx: &TurnState<'_, tinyagents_harness::cost::TurnCost>) -> StopDecision {let store = goals_store(&self.workspace_dir);let in_flight = turn_tokens(ctx.cost.input_tokens, ctx.cost.output_tokens);match self.guard.check(&store, in_flight).await {diff --git a/crates/openhuman-core/src/agent/goals/runtime_tests.rs b/crates/openhuman-core/src/agent/goals/runtime_tests.rsindex e9caea2b65..86489b8537 100644--- a/crates/openhuman-core/src/agent/goals/runtime_tests.rs+++ b/crates/openhuman-core/src/agent/goals/runtime_tests.rs@@ -1,5 +1,5 @@use super::*;-use crate::agent::cost::TurnCost;+use tinyagents_harness::cost::TurnCost;use crate::inference::provider::BilledUsage;fn cost_with_tokens(input: u64, output: u64) -> TurnCost {diff --git a/crates/openhuman-core/src/agent/hooks_scope.rs b/crates/openhuman-core/src/agent/hooks_scope.rsindex 8e5b752cdc..fce4e756f8 100644--- a/crates/openhuman-core/src/agent/hooks_scope.rs+++ b/crates/openhuman-core/src/agent/hooks_scope.rs@@ -18,7 +18,7 @@ tokio::task_local! {pub struct HookScope {tools: Vec<Arc<dyn ToolHook>>,post_turn: Vec<Arc<dyn PostTurnHook>>,- stop: Vec<Arc<dyn tinyagents_harness::middleware::StopHook<crate::agent::cost::TurnCost>>>,+ stop: Vec<Arc<dyn tinyagents_harness::middleware::StopHook<tinyagents_harness::cost::TurnCost>>>,}impl HookScope {@@ -35,7 +35,7 @@ impl HookScope {/// Append a usage observer/budget policy checked after each model call.pub fn push_stop(&mut self,- hook: Arc<dyn tinyagents_harness::middleware::StopHook<crate::agent::cost::TurnCost>>,+ hook: Arc<dyn tinyagents_harness::middleware::StopHook<tinyagents_harness::cost::TurnCost>>,) {self.stop.push(hook);}diff --git a/crates/openhuman-core/src/agent/progress.rs b/crates/openhuman-core/src/agent/progress.rsindex c0e23b6ed4..6e434fc0eb 100644--- a/crates/openhuman-core/src/agent/progress.rs+++ b/crates/openhuman-core/src/agent/progress.rs@@ -355,7 +355,7 @@ pub enum AgentProgress {/// `total_usd` prefers backend-reported `charged_amount_usd`/// (sum of authoritative figures) and falls back to a tier-based/// token-rate estimate for calls that didn't carry one — see- /// [`crate::agent::cost::TurnCost::total_usd`].+ /// [`tinyagents_harness::cost::TurnCost::total_usd`].TurnCostUpdated {/// Last model that contributed to this update.model: String,diff --git a/crates/openhuman-core/src/agent/session_host/tool_progress.rs b/crates/openhuman-core/src/agent/session_host/tool_progress.rsindex eb111a779f..73aa006c1a 100644--- a/crates/openhuman-core/src/agent/session_host/tool_progress.rs+++ b/crates/openhuman-core/src/agent/session_host/tool_progress.rs@@ -9,7 +9,7 @@use async_trait::async_trait;-use crate::agent::cost::TurnCost;+use tinyagents_harness::cost::TurnCost;use crate::agent::progress::AgentProgress;use crate::inference::provider::ProviderDelta;diff --git a/crates/openhuman-core/src/agent/stop_hooks.rs b/crates/openhuman-core/src/agent/stop_hooks.rsindex 5e0b8782fe..631a2f363e 100644--- a/crates/openhuman-core/src/agent/stop_hooks.rs+++ b/crates/openhuman-core/src/agent/stop_hooks.rs@@ -17,7 +17,7 @@//! The per-turn tool budget ([`with_tool_call_limit`]) also lives here, on a//! task-local.-pub use crate::agent::cost::TurnCost;+use tinyagents_harness::cost::TurnCost;use async_trait::async_trait;use std::sync::Arc;diff --git a/crates/openhuman-core/src/agent/stop_hooks_tests.rs b/crates/openhuman-core/src/agent/stop_hooks_tests.rsindex 887a0c146e..6f51721794 100644--- a/crates/openhuman-core/src/agent/stop_hooks_tests.rs+++ b/crates/openhuman-core/src/agent/stop_hooks_tests.rs@@ -56,7 +56,7 @@ async fn cancelled_scope_restores_the_callers_tool_limit() {#[tokio::test]async fn scoped_stop_hooks_are_visible_to_run_contexts() {- let hook: Arc<dyn StopHook<crate::agent::cost::TurnCost>> = Arc::new(BudgetStopHook::new(1.0));+ let hook: Arc<dyn StopHook<tinyagents_harness::cost::TurnCost>> = Arc::new(BudgetStopHook::new(1.0));assert!(current_stop_hooks().is_empty());with_stop_hooks(vec![Arc::clone(&hook)], async {let active = current_stop_hooks();diff --git a/crates/openhuman-core/src/agent/tinyagents/budget_stop_turn_tests.rs b/crates/openhuman-core/src/agent/tinyagents/budget_stop_turn_tests.rsindex 16db8fe4f4..4a71f23db6 100644--- a/crates/openhuman-core/src/agent/tinyagents/budget_stop_turn_tests.rs+++ b/crates/openhuman-core/src/agent/tinyagents/budget_stop_turn_tests.rs@@ -79,12 +79,12 @@ struct RecordingBudgetStopHook {}#[async_trait]-impl StopHook<crate::agent::cost::TurnCost> for RecordingBudgetStopHook {+impl StopHook<tinyagents_harness::cost::TurnCost> for RecordingBudgetStopHook {fn name(&self) -> &str {self.inner.name()}- async fn check(&self, state: &TurnState<'_, crate::agent::cost::TurnCost>) -> StopDecision {+ async fn check(&self, state: &TurnState<'_, tinyagents_harness::cost::TurnCost>) -> StopDecision {let decision = self.inner.check(state).await;if let StopDecision::Stop { reason } = &decision {*self.reason.lock().expect("stop reason lock") = Some(reason.clone());diff --git a/crates/openhuman-core/src/agent/tinyagents/harness_assembly.rs b/crates/openhuman-core/src/agent/tinyagents/harness_assembly.rsindex 741759c8a9..0a20baf60f 100644--- a/crates/openhuman-core/src/agent/tinyagents/harness_assembly.rs+++ b/crates/openhuman-core/src/agent/tinyagents/harness_assembly.rs@@ -65,7 +65,7 @@ pub(super) fn assemble_turn_harness(early_exit_tools: &[&str],context_mw: TurnContextMiddleware,stop_hooks: Vec<- Arc<dyn tinyagents_harness::middleware::StopHook<crate::agent::cost::TurnCost>>,+ Arc<dyn tinyagents_harness::middleware::StopHook<tinyagents_harness::cost::TurnCost>>,>,tool_policy: Option<ToolPolicyEnforcement>,required_capabilities: Option<CapabilitySet>,diff --git a/crates/openhuman-core/src/agent/tinyagents/host/run_context.rs b/crates/openhuman-core/src/agent/tinyagents/host/run_context.rsindex de9c1f17d0..263e40ab25 100644--- a/crates/openhuman-core/src/agent/tinyagents/host/run_context.rs+++ b/crates/openhuman-core/src/agent/tinyagents/host/run_context.rs@@ -282,7 +282,7 @@ pub struct OpenHumanRunContext {/// UI/event progress receiver for this turn tree.pub progress: Option<Sender<AgentProgress>>,/// Stop policies evaluated after each model call.- pub stop_hooks: Vec<Arc<dyn StopHook<crate::agent::cost::TurnCost>>>,+ pub stop_hooks: Vec<Arc<dyn StopHook<tinyagents_harness::cost::TurnCost>>>,/// Wall-clock deadline of the top-level turn this run belongs to, set/// only by the session turn entry point from the web backstop/// (`agent::turn_deadline`). The harness winds the run down and clampsdiff --git a/crates/openhuman-core/src/agent/tinyagents/stop_hooks.rs b/crates/openhuman-core/src/agent/tinyagents/stop_hooks.rsindex f59b227c0b..c62c11116c 100644--- a/crates/openhuman-core/src/agent/tinyagents/stop_hooks.rs+++ b/crates/openhuman-core/src/agent/tinyagents/stop_hooks.rs@@ -1,19 +1,10 @@//! Host accounting and result projection for the harness-owned stop chain.-use crate::agent::cost::TurnCost;use std::sync::Arc;-use tinyagents_harness::middleware::library::{StopHookMetrics, StopHookMiddleware};+use tinyagents_harness::cost::TurnCost;+use tinyagents_harness::middleware::library::StopHookMiddleware;use tinyagents_harness::middleware::StopHook;use tinyagents_harness::steering::SteeringHandle;-use tinyinference_llm::model::ModelResponse;--impl StopHookMetrics for TurnCost {- fn record_model(&mut self, model: &str, response: &ModelResponse) {- if let Some(usage) = super::model::usage_info_from_response(response) {- self.add_call(model, &usage);- }- }-}/// Install product policies and preserve the host-facing halt summary.pub(super) fn install(@@ -25,7 +16,9 @@ pub(super) fn install() {if handle.is_some() && !hooks.is_empty() {harness.push_middleware(Arc::new(- StopHookMiddleware::new(model, hooks).with_stop_observer(move |report| {+ StopHookMiddleware::new(model, hooks)+ .with_metrics_seed(crate::agent::cost::turn_cost())+ .with_stop_observer(move |report| {tracing::warn!(target: "stop_hooks",hook = %report.hook,diff --git a/crates/openhuman-embed/src/lib.rs b/crates/openhuman-embed/src/lib.rsindex 7ce3aafb48..7f75036d76 100644--- a/crates/openhuman-embed/src/lib.rs+++ b/crates/openhuman-embed/src/lib.rs@@ -219,7 +219,7 @@ pub mod seams {pub use openhuman_core::agent::hooks::{PostTurnHook, ToolHook};pub use openhuman_core::agent::hooks::{ToolHookContext, ToolHookDecision, TurnContext};pub use openhuman_core::agent::stop_hooks::BudgetStopHook;- pub use openhuman_core::agent::stop_hooks::TurnCost;+ pub use tinyagents_harness::cost::TurnCost;pub use tinyagents_harness::middleware::{StopDecision, StopHook};/// Product cost snapshot passed to harness-owned stop policies.pub type TurnState<'a> = tinyagents_harness::middleware::TurnState<'a, TurnCost>;diff --git a/vendor/tinyagents b/vendor/tinyagentsindex c7bce17370..e024364e8c 160000--- a/vendor/tinyagents+++ b/vendor/tinyagents@@ -1 +1 @@-Subproject commit c7bce17370b9d7425fec679cb331a93a5e362033+Subproject commit e024364e8c22c7b84b8637159924c30cc425846b