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
feat(inference): send price-sorted routing to direct OpenRouter endpoints
Steven Enamakel09c29132bab6
diff --git a/crates/openhuman-core/src/inference/provider/factory/cloud_slug.rs b/crates/openhuman-core/src/inference/provider/factory/cloud_slug.rsindex dd6ade80fd..f92094290c 100644--- a/crates/openhuman-core/src/inference/provider/factory/cloud_slug.rs+++ b/crates/openhuman-core/src/inference/provider/factory/cloud_slug.rs@@ -412,7 +412,7 @@ pub(super) fn try_create_cloud_slug_chat_model_from_string_with_native_tools(extra_headers: extra_headers.as_slice(),native_tool_calling: Some(native_tool_calling),vision: None,- default_provider_options: None,+ default_provider_options: openrouter_default_provider_options(&endpoint),responses_api_primary,responses_omit_max_output_tokens,extra_query_params: extra_query_params.as_slice(),@@ -425,3 +425,29 @@ pub(super) fn try_create_cloud_slug_chat_model_from_string_with_native_tools(});Some(Ok((chat, effective_model)))}++/// The `provider.sort` OpenRouter routing preference sent on every request to+/// a direct (BYOK) OpenRouter endpoint.+pub(crate) const OPENROUTER_PROVIDER_SORT: &str = "price";++/// Baked request options for a direct OpenRouter endpoint, `None` for every+/// other OpenAI-compatible host.+///+/// OpenRouter's default routing is a health-aware load balance weighted toward+/// the cheaper endpoints of a model's pool, which spreads consecutive turns+/// across endpoints. DeepSeek- and Z.AI-family prefix caches live per endpoint,+/// so every hop re-pays the whole prefill. `provider.sort` orders the pool and+/// disables the balancing (not the fallback: `allow_fallbacks` stays on and+/// `order` is never set, so a request still moves down the sorted list when an+/// endpoint errors), and `price` is the stable choice — cheapest first, dearer+/// only on failure. Stickiness itself comes from the harness's derived+/// `prompt_cache_key` (`tap-<prefix fingerprint>`), which OpenRouter honours as+/// its sticky-routing key; measured on `z-ai/glm-5.3-flash` the same endpoint+/// served 5/5 calls with it. The hosted backend applies the same sort+/// server-side (tinyhumansai/backend#1370), so managed and direct routing agree.+/// https://openrouter.ai/docs/features/provider-routing+pub(crate) fn openrouter_default_provider_options(endpoint: &str) -> Option<serde_json::Value> {+ endpoint_is_openrouter(endpoint)+ .then(|| serde_json::json!({ "provider": { "sort": OPENROUTER_PROVIDER_SORT } }))+}+diff --git a/crates/openhuman-core/src/inference/provider/factory_crate_native_tests.rs b/crates/openhuman-core/src/inference/provider/factory_crate_native_tests.rsindex 8b7ddc4b83..8b298c6f5b 100644--- a/crates/openhuman-core/src/inference/provider/factory_crate_native_tests.rs+++ b/crates/openhuman-core/src/inference/provider/factory_crate_native_tests.rs@@ -1,6 +1,9 @@use super::*;-use crate::inference::provider::factory::cloud_slug::try_create_cloud_slug_chat_model_from_string_with_native_tools;+use crate::inference::provider::factory::cloud_slug::{+ openrouter_default_provider_options, try_create_cloud_slug_chat_model_from_string_with_native_tools,+ OPENROUTER_PROVIDER_SORT,+};#[test]fn enforce_local_only_inference_errors_on_external_when_local_only() {// Drive the live-policy-backed wrapper: install a LocalOnly policy, then@@ -748,3 +751,48 @@ async fn local_aliases_build_without_a_session_and_preserve_model_ids() {assert!(error.to_string().contains("empty model"), "{error}");assert!(!error.to_string().contains("SESSION_EXPIRED"), "{error}");}++#[test]+fn direct_openrouter_endpoints_get_price_sorted_routing_and_nothing_else() {+ // Direct BYOK OpenRouter: cheapest-first sort so consecutive turns stay on+ // one endpoint and its prefix cache hits. Only the sort — never `order` or+ // `allow_fallbacks: false`, which would strand a request on an outage, and+ // never a `max_price` (the hosted backend dropped its own for the same+ // reason in tinyhumansai/backend#1370).+ let options = openrouter_default_provider_options("https://openrouter.ai/api/v1")+ .expect("openrouter endpoint carries routing options");+ assert_eq!(OPENROUTER_PROVIDER_SORT, "price");+ assert_eq!(+ options,+ serde_json::json!({ "provider": { "sort": "price" } }),+ "exactly the sort and nothing else: {options}"+ );+ let provider = &options["provider"];+ for forbidden in ["order", "allow_fallbacks", "max_price", "only", "ignore"] {+ assert!(provider.get(forbidden).is_none(), "must not set provider.{forbidden}");+ }+ // Host matching is what keys it, with or without a path or trailing slash.+ assert!(openrouter_default_provider_options("https://openrouter.ai/api/v1/").is_some());+ assert!(openrouter_default_provider_options("https://OpenRouter.ai/api/v1").is_some());+}++#[test]+fn non_openrouter_openai_compatible_endpoints_get_no_baked_provider_options() {+ // `provider` is an OpenRouter-only body field; hosted OpenAI rejects+ // unknown top-level fields and local runners ignore them, so no other+ // OpenAI-compatible host may receive it.+ for endpoint in [+ "https://api.openai.com/v1",+ "https://api.deepseek.com/v1",+ "https://api.groq.com/openai/v1",+ "http://localhost:11434/v1",+ "https://openrouter.example.com/v1",+ "not a url",+ ] {+ assert!(+ openrouter_default_provider_options(endpoint).is_none(),+ "{endpoint} must not get OpenRouter routing options"+ );+ }+}+