Skip to content

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

test(agent): pin the pricing a resolved route's generation carries

M3gA-Mindae54c88473c2
diff --git a/crates/openhuman-core/src/agent/tinyagents/turn_middleware.rs b/crates/openhuman-core/src/agent/tinyagents/turn_middleware.rs
index 64ce426c8a..eb1bf9df94 100644
--- a/crates/openhuman-core/src/agent/tinyagents/turn_middleware.rs
+++ b/crates/openhuman-core/src/agent/tinyagents/turn_middleware.rs
@@ -222,23 +222,7 @@ pub(super) fn install_turn_middleware(
.unwrap_or_else(|poisoned| poisoned.into_inner()) = Some(route);
},
)
- .with_pricing_lookup(|model| {
- if crate::agent::cost::is_free_model(model) {
- 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(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),
- }
- })
- }
- }),
+ .with_pricing_lookup(generation_pricing),
));
// Per-call capability gate (issue #4249, Workstream 02.1): when the turn has
@@ -688,6 +672,26 @@ fn record_stop_summary(summary: &HaltSummarySlot, iteration: u32, reason: &str)
));
}
+/// The per-token pricing a resolved route's generation is enriched with: a
+/// free model is priced at zero, a catalogued one at its list price, and an
+/// unknown one is left unpriced.
+fn generation_pricing(model: &str) -> Option<tinyagents_harness::events::GenerationPricing> {
+ if crate::agent::cost::is_free_model(model) {
+ return 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),
+ });
+ }
+ tinyagents_registry::pricing::lookup(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),
+ }
+ })
+}
+
#[cfg(test)]
#[path = "turn_middleware_tests.rs"]
mod tests;
diff --git a/crates/openhuman-core/src/agent/tinyagents/turn_middleware_tests.rs b/crates/openhuman-core/src/agent/tinyagents/turn_middleware_tests.rs
index 6d2ddcdabd..131461dfe9 100644
--- a/crates/openhuman-core/src/agent/tinyagents/turn_middleware_tests.rs
+++ b/crates/openhuman-core/src/agent/tinyagents/turn_middleware_tests.rs
@@ -20,3 +20,28 @@ fn a_poisoned_halt_summary_does_not_abort_stop_reporting() {
Some("Stopping after 1 model call(s): limit reached")
);
}
+
+#[test]
+fn a_resolved_routes_pricing_is_zero_for_free_models_and_the_list_price_otherwise() {
+ let free = generation_pricing("openrouter/some-model:free").expect("free models are priced");
+ assert_eq!(free.input_per_mtok_usd, Some(0.0));
+ assert_eq!(free.cached_input_per_mtok_usd, Some(0.0));
+ assert_eq!(free.output_per_mtok_usd, Some(0.0));
+
+ let listed = tinyagents_registry::pricing::KNOWN_MODEL_PRICING
+ .iter()
+ .find(|price| !crate::agent::cost::is_free_model(price.model_id))
+ .expect("precondition: the pricing catalog lists a paid model");
+ let priced = generation_pricing(listed.model_id).expect("a catalogued model is priced");
+ assert_eq!(priced.input_per_mtok_usd, Some(listed.input_per_mtok_usd));
+ assert_eq!(
+ priced.cached_input_per_mtok_usd,
+ Some(listed.cached_input_per_mtok_usd)
+ );
+ assert_eq!(priced.output_per_mtok_usd, Some(listed.output_per_mtok_usd));
+
+ assert!(
+ generation_pricing("no-such-model-anywhere").is_none(),
+ "an unknown model is left unpriced rather than given a default rate"
+ );
+}