Skip to content

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

refactor(tinyagents): use the harness ModelBudget directly

Steven Enamakel33b044f286e6
diff --git a/crates/openhuman-core/src/agent/tinyagents/budget.rs b/crates/openhuman-core/src/agent/tinyagents/budget.rs
index 908a1e6a83..cd82c5a954 100644
--- a/crates/openhuman-core/src/agent/tinyagents/budget.rs
+++ b/crates/openhuman-core/src/agent/tinyagents/budget.rs
@@ -3,8 +3,6 @@
use std::sync::Arc;
-use tinyinference_llm::model::ChatModel;
-
/// Product recursion ceiling shared by native and MCP delegation.
pub(crate) const MAX_SPAWN_DEPTH: usize = 3;
pub use tinyagents_harness::cost::{
@@ -15,56 +13,6 @@ pub use tinyinference_llm::model::budget::{
Budget, BudgetExceeded, BudgetSnapshot, CallBudget, Spend, SpendLimits,
};
-/// OpenHuman's source-compatible carrier for the shared ledger and physical
-/// call reservation bounds. The owner harness performs the wrapping engine.
-#[derive(Debug, Clone)]
-pub struct ModelBudget {
- /// Run/turn ledger. Children of a turn share it.
- pub ledger: Budget,
- /// Per-call input, output and charge upper bounds for all allowed routes.
- pub call: CallBudget,
-}
-
-impl ModelBudget {
- /// Wait for live reservations when settled spend still affords admission.
- /// Clones and child turns inherit this opt-in; internal retries stay fail-fast.
- pub fn wait_for_capacity(mut self) -> Self {
- self.ledger = self.ledger.wait_for_capacity();
- self
- }
-
- /// Wrap the model with TinyAgents' shared physical-call budget engine.
- pub(crate) fn wrap(&self, model: Arc<dyn ChatModel<()>>) -> Arc<dyn ChatModel<()>> {
- self.wrap_with_observer(model, None)
- }
-
- /// Wrap the model with budget accounting and an optional physical observer.
- pub(crate) fn wrap_with_observer(
- &self,
- model: Arc<dyn ChatModel<()>>,
- observer: Option<Arc<dyn tinyagents_harness::observability::PhysicalDispatchObserver>>,
- ) -> Arc<dyn ChatModel<()>> {
- tinyagents_harness::cost::ModelBudget::new(self.ledger.clone(), self.call)
- .wrap_model_with_observer(model, observer)
- }
-
- pub(crate) fn control(
- model: Arc<dyn ChatModel<()>>,
- budget: Option<&Self>,
- admission: Option<Arc<dyn PhysicalDispatchAdmission>>,
- ) -> Arc<dyn ChatModel<()>> {
- let monetary_budget = budget.map(|budget| {
- tinyagents_harness::cost::ModelBudget::new(budget.ledger.clone(), budget.call)
- });
- Arc::new(tinyagents_harness::cost::ControlledChatModel::new(
- model,
- DispatchModelOptions::new()
- .monetary_budget(monetary_budget)
- .admission(admission),
- ))
- }
-}
-
/// Effective host depth ceiling, never wider than product policy.
pub(crate) fn depth(context: &super::host::OpenHumanRunContext) -> usize {
context
@@ -93,11 +41,27 @@ impl crate::inference::turn_models::TurnModels {
if budget.is_none() && admission.is_none() {
return self;
}
- self.primary = ModelBudget::control(self.primary, budget, admission.clone());
- self.summarizer = ModelBudget::control(self.summarizer, budget, admission.clone());
+ self.primary = control_model(self.primary, budget, admission.clone());
+ self.summarizer = control_model(self.summarizer, budget, admission.clone());
for (_, route) in &mut self.routes {
- *route = ModelBudget::control(route.clone(), budget, admission.clone());
+ *route = control_model(route.clone(), budget, admission.clone());
}
self
}
}
+
+fn control_model(
+ model: Arc<dyn tinyinference_llm::model::ChatModel<()>>,
+ budget: Option<&tinyagents_harness::cost::ModelBudget>,
+ admission: Option<Arc<dyn PhysicalDispatchAdmission>>,
+) -> Arc<dyn tinyinference_llm::model::ChatModel<()>> {
+ let monetary_budget = budget.map(|budget| {
+ tinyagents_harness::cost::ModelBudget::new(budget.ledger.clone(), budget.call)
+ });
+ Arc::new(tinyagents_harness::cost::ControlledChatModel::new(
+ model,
+ DispatchModelOptions::new()
+ .monetary_budget(monetary_budget)
+ .admission(admission),
+ ))
+}
diff --git a/crates/openhuman-core/src/agent/tinyagents/host/run_context.rs b/crates/openhuman-core/src/agent/tinyagents/host/run_context.rs
index 1449ee4e86..924f042e7b 100644
--- a/crates/openhuman-core/src/agent/tinyagents/host/run_context.rs
+++ b/crates/openhuman-core/src/agent/tinyagents/host/run_context.rs
@@ -163,7 +163,7 @@ pub use dispatch_control::{decide_dispatch, DispatchDecision, DispatchInputs, Tu
#[derive(Clone)]
pub struct OpenHumanRunContext {
/// Explicit shared model-call budget inherited by synchronous children.
- pub model_budget: Option<super::super::budget::ModelBudget>,
+ pub model_budget: Option<tinyagents_harness::cost::ModelBudget>,
/// Host admission policy shared by this run and its synchronous descendants.
pub dispatch_admission: Option<Arc<dyn super::super::budget::PhysicalDispatchAdmission>>,
/// The response format and output cap this root turn asks of every model
diff --git a/crates/openhuman-core/src/agent/tinyagents/host/run_context_tests.rs b/crates/openhuman-core/src/agent/tinyagents/host/run_context_tests.rs
index 607dd1feaf..0b1abd978f 100644
--- a/crates/openhuman-core/src/agent/tinyagents/host/run_context_tests.rs
+++ b/crates/openhuman-core/src/agent/tinyagents/host/run_context_tests.rs
@@ -386,7 +386,7 @@ fn attach_parent_keeps_the_snapshot_sink_when_the_run_has_none() {
#[test]
fn child_inherits_shared_model_budget_and_narrowed_depth_limit() {
use {
- crate::agent::tinyagents::budget::ModelBudget, tinyinference_llm::model::budget::Budget,
+ tinyagents_harness::cost::ModelBudget, tinyinference_llm::model::budget::Budget,
tinyinference_llm::model::budget::CallBudget, tinyinference_llm::model::budget::Spend,
tinyinference_llm::model::budget::SpendLimits,
};
diff --git a/crates/openhuman-core/src/agent/tinyagents/payload_summarizer.rs b/crates/openhuman-core/src/agent/tinyagents/payload_summarizer.rs
index ce212b6148..40139420d4 100644
--- a/crates/openhuman-core/src/agent/tinyagents/payload_summarizer.rs
+++ b/crates/openhuman-core/src/agent/tinyagents/payload_summarizer.rs
@@ -147,11 +147,18 @@ impl HarnessPayloadSummarizer<OpenHumanRunContext> for SubagentPayloadSummarizer
let provider_model = if child_context.data.model_budget.is_some()
|| child_context.data.dispatch_admission.is_some()
{
- super::budget::ModelBudget::control(
+ let monetary_budget = child_context.data.model_budget.as_ref().map(|budget| {
+ tinyagents_harness::cost::ModelBudget::new(
+ budget.ledger.clone(),
+ budget.call,
+ )
+ });
+ Arc::new(tinyagents_harness::cost::ControlledChatModel::new(
provider_model,
- child_context.data.model_budget.as_ref(),
- child_context.data.dispatch_admission.clone(),
- )
+ tinyagents_harness::cost::DispatchModelOptions::new()
+ .monetary_budget(monetary_budget)
+ .admission(child_context.data.dispatch_admission.clone()),
+ ))
} else {
provider_model
};
diff --git a/crates/openhuman-core/src/agent/turn_env.rs b/crates/openhuman-core/src/agent/turn_env.rs
index 9b1a0a1a42..584b53c807 100644
--- a/crates/openhuman-core/src/agent/turn_env.rs
+++ b/crates/openhuman-core/src/agent/turn_env.rs
@@ -18,7 +18,7 @@ use std::sync::Arc;
use tinyagents_harness::cancel::CancellationToken;
use tinyagents_harness::turn_deadline::TurnDeadline;
-use crate::agent::tinyagents::budget::ModelBudget;
+use tinyagents_harness::cost::ModelBudget;
use crate::agent::tinyagents::host::OpenHumanRunContext;
/// Per-turn values an entry point hands to the session it runs.
diff --git a/crates/openhuman-core/src/inference/host_runtime/ops/complete_once.rs b/crates/openhuman-core/src/inference/host_runtime/ops/complete_once.rs
index 1da51b7e6b..f440f78bfe 100644
--- a/crates/openhuman-core/src/inference/host_runtime/ops/complete_once.rs
+++ b/crates/openhuman-core/src/inference/host_runtime/ops/complete_once.rs
@@ -60,7 +60,7 @@ impl std::fmt::Debug for CompletionEndpoint {
pub async fn complete_once(
endpoint: &CompletionEndpoint,
request: ModelRequest,
- budget: Option<&crate::agent::tinyagents::budget::ModelBudget>,
+ budget: Option<&tinyagents_harness::cost::ModelBudget>,
) -> Result<ModelResponse, String> {
complete_once_observed(endpoint, request, budget, None).await
}
@@ -74,7 +74,7 @@ pub async fn complete_once(
pub async fn complete_once_observed(
endpoint: &CompletionEndpoint,
request: ModelRequest,
- budget: Option<&crate::agent::tinyagents::budget::ModelBudget>,
+ budget: Option<&tinyagents_harness::cost::ModelBudget>,
observer: Option<
std::sync::Arc<dyn tinyagents_harness::observability::PhysicalDispatchObserver>,
>,
@@ -124,7 +124,7 @@ pub fn validate_complete_once_request(request: &ModelRequest) -> Result<(), Stri
pub async fn complete_once_controlled(
endpoint: &CompletionEndpoint,
request: ModelRequest,
- budget: Option<&crate::agent::tinyagents::budget::ModelBudget>,
+ budget: Option<&tinyagents_harness::cost::ModelBudget>,
observer: Option<
std::sync::Arc<dyn tinyagents_harness::observability::PhysicalDispatchObserver>,
>,
diff --git a/crates/openhuman-embed/src/budget.rs b/crates/openhuman-embed/src/budget.rs
index 089b59c16b..621b051836 100644
--- a/crates/openhuman-embed/src/budget.rs
+++ b/crates/openhuman-embed/src/budget.rs
@@ -18,7 +18,7 @@
//! Budgeted calls currently accept text only. Input bounds use serialized-byte
//! counts conservatively; multimodal requests fail before dispatch. Choose
//! bounds that include model framing and the maximum price on allowed routes.
-pub use openhuman_core::agent::tinyagents::budget::ModelBudget;
+pub use tinyagents_harness::cost::ModelBudget;
pub use openhuman_core::agent::tinyagents::budget::{
ControlledDispatchError, DispatchModelOptions, DispatchRefusal, DispatchRefusalReason,
PhysicalDispatchAdmission, PhysicalDispatchPermit,