Skip to content

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

chore: files changed src/openhuman/inference/provider/crate_openai.rs,src/openhuman/inference/provid

Steven Enamakel9d40c91119fc
diff --git a/src/openhuman/inference/provider/crate_anthropic.rs b/src/openhuman/inference/provider/crate_anthropic.rs
new file mode 100644
index 0000000000..a29d4d768c
--- /dev/null
+++ b/src/openhuman/inference/provider/crate_anthropic.rs
@@ -0,0 +1,59 @@
+//! Crate-native Anthropic Messages API client construction.
+//!
+//! A `cloud_providers` entry with `auth_style = "anthropic"` used to be served
+//! by the crate `OpenAiModel` in its Anthropic-auth flavour — Chat Completions
+//! against Anthropic's OpenAI-compatibility endpoint. That endpoint documents
+//! prompt caching as **unsupported** and reports `prompt_tokens_details` as
+//! always empty, so every turn on a BYOK Claude key re-billed the entire system
+//! prompt, tool catalogue, and conversation at full price, and the host's
+//! `[cache]` diagnostics could never show a hit.
+//!
+//! This module is the boundary where that entry becomes the crate's native
+//! [`AnthropicModel`] instead: the Messages API with `cache_control`
+//! breakpoints on the tool set, the system prompt, and the tail of the
+//! conversation (see the crate adapter's docs for the placement rule), native
+//! `tool_use` / `tool_result` blocks, signed thinking replay, and SSE streaming.
+//! The harness's `PromptCacheSegmentMiddleware` already declares the stable
+//! prefix on every request; this is what turns those declarations into wire
+//! markers on the one provider that needs them explicitly.
+
+use std::sync::Arc;
+
+use tinyinference::model::ChatModel;
+use tinyinference::providers::anthropic::AnthropicModel;
+
+/// The resolved config for one Anthropic Messages API provider.
+pub(crate) struct CrateAnthropicConfig<'a> {
+ /// Base URL (`https://api.anthropic.com/v1` for the hosted API; a
+ /// Messages-compatible proxy otherwise). `/messages` is appended unless
+ /// the URL already ends in it.
+ pub endpoint: &'a str,
+ /// API credential, sent as `x-api-key`.
+ pub api_key: &'a str,
+ /// Default model id baked onto the client (a per-call `ModelRequest.model`
+ /// still overrides it).
+ pub model: &'a str,
+ /// Fixed temperature override for every call, when set (the `@<temp>`
+ /// provider-string suffix).
+ pub temperature_override: Option<f64>,
+}
+
+/// Build a crate-native [`AnthropicModel`] (`ChatModel`) for the given config.
+pub(crate) fn build_crate_anthropic_model(
+ config: CrateAnthropicConfig<'_>,
+) -> Arc<dyn ChatModel<()>> {
+ log::debug!(
+ "[providers][chat-factory] building native Anthropic Messages client endpoint={} model={} temperature_override={:?}",
+ config.endpoint,
+ config.model,
+ config.temperature_override
+ );
+ let model = AnthropicModel::with_base_url(config.api_key, config.endpoint)
+ .with_model(config.model)
+ .with_temperature_override(config.temperature_override);
+ Arc::new(model)
+}
+
+#[cfg(test)]
+#[path = "crate_anthropic_tests.rs"]
+mod tests;
diff --git a/src/openhuman/inference/provider/crate_anthropic_tests.rs b/src/openhuman/inference/provider/crate_anthropic_tests.rs
new file mode 100644
index 0000000000..1f0b71196d
--- /dev/null
+++ b/src/openhuman/inference/provider/crate_anthropic_tests.rs
@@ -0,0 +1,21 @@
+use super::*;
+
+#[test]
+fn builds_a_native_anthropic_model_with_the_configured_profile() {
+ let model = build_crate_anthropic_model(CrateAnthropicConfig {
+ endpoint: "https://api.anthropic.com/v1",
+ api_key: "sk-ant-secret",
+ model: "claude-sonnet-4-6",
+ temperature_override: Some(0.2),
+ });
+ let profile = model.profile().expect("anthropic models expose a profile");
+ assert_eq!(profile.provider.as_deref(), Some("anthropic"));
+ assert_eq!(profile.model.as_deref(), Some("claude-sonnet-4-6"));
+ assert!(profile.tool_calling, "the native adapter speaks tool_use");
+ assert!(profile.streaming, "the native adapter streams SSE");
+ // The identity the response cache scopes on names the endpoint + model and
+ // never the credential.
+ let identity = model.cache_identity().expect("anthropic models identify themselves");
+ assert!(identity.contains("api.anthropic.com"));
+ assert!(!identity.contains("sk-ant-secret"));
+}
diff --git a/src/openhuman/inference/provider/crate_openai.rs b/src/openhuman/inference/provider/crate_openai.rs
index 09291c37a0..b4bd560a23 100644
--- a/src/openhuman/inference/provider/crate_openai.rs
+++ b/src/openhuman/inference/provider/crate_openai.rs
@@ -90,6 +90,23 @@ pub(crate) struct CrateOpenAiConfig<'a> {
/// `User-Agent` header override (e.g. the Codex CLI UA). Maps to
/// `OpenAiModel::with_user_agent`.
pub user_agent: Option<&'a str>,
+ /// Emit explicit `cache_control` breakpoints on the last system and user
+ /// messages. Only relays that forward them to an opt-in prompt cache
+ /// (OpenRouter → Anthropic / Gemini) should set this; hosted OpenAI
+ /// rejects unknown content-part fields. Maps to
+ /// `OpenAiModel::with_explicit_cache_control`.
+ pub explicit_cache_control: bool,
+}
+
+/// Whether `endpoint` is OpenRouter, the one relay in the catalog that
+/// documents forwarding `cache_control` markers to its upstream providers.
+pub(crate) fn endpoint_is_openrouter(endpoint: &str) -> bool {
+ endpoint
+ .trim()
+ .to_ascii_lowercase()
+ .split("://")
+ .last()
+ .is_some_and(|rest| rest.starts_with("openrouter.ai/"))
}
/// Build a crate-native `OpenAiModel` (`ChatModel`) for the given OpenAI-compatible
@@ -134,6 +151,13 @@ pub(crate) fn build_crate_openai_model(config: CrateOpenAiConfig<'_>) -> Arc<dyn
if let Some(user_agent) = config.user_agent {
model = model.with_user_agent(user_agent);
}
+ if config.explicit_cache_control {
+ log::debug!(
+ "[providers][chat-factory] provider={} explicit cache_control breakpoints enabled",
+ config.provider_name
+ );
+ model = model.with_explicit_cache_control(true);
+ }
if config.responses_api_primary {
model = model.with_responses_api_primary();
}
diff --git a/src/openhuman/inference/provider/factory_part_04.rs b/src/openhuman/inference/provider/factory_part_04.rs
index fac969c7af..d1bcefe6bf 100644
--- a/src/openhuman/inference/provider/factory_part_04.rs
+++ b/src/openhuman/inference/provider/factory_part_04.rs
@@ -390,7 +390,38 @@ fn try_create_cloud_slug_chat_model_from_string_with_native_tools(
let mut responses_omit_max_output_tokens = false;
let auth = match entry.auth_style {
- AuthStyle::Anthropic => CompatAuthStyle::Anthropic,
+ AuthStyle::Anthropic => {
+ // Anthropic's OpenAI-compatibility endpoint does not support prompt
+ // caching (and reports `prompt_tokens_details` as always empty), so
+ // a BYOK Claude key served through Chat Completions re-billed the
+ // whole prefix on every call. Native tool calling is the normal
+ // case and routes to the crate's Messages API adapter, which
+ // places `cache_control` breakpoints. Text mode (prompt-guided
+ // tools) is only implemented on the Chat Completions adapter, so
+ // that rare case keeps the compat path.
+ if native_tool_calling {
+ crate::openhuman::security::egress::emit_external_transfer(
+ crate::openhuman::security::egress::EgressDescriptor::inference(
+ &slug,
+ &effective_model,
+ true,
+ ),
+ );
+ let chat = super::crate_anthropic::build_crate_anthropic_model(
+ super::crate_anthropic::CrateAnthropicConfig {
+ endpoint: endpoint.as_str(),
+ api_key: key.as_str(),
+ model: effective_model.as_str(),
+ temperature_override,
+ },
+ );
+ return Some(Ok((chat, effective_model)));
+ }
+ log::debug!(
+ "[providers][chat-factory] slug={slug} auth_style=anthropic native_tools=false → OpenAI-compatible text-mode path (no prompt caching)"
+ );
+ CompatAuthStyle::Anthropic
+ }
AuthStyle::None => CompatAuthStyle::None,
AuthStyle::OpenhumanJwt => {
#[cfg(not(test))]
@@ -467,6 +498,11 @@ fn try_create_cloud_slug_chat_model_from_string_with_native_tools(
responses_omit_max_output_tokens,
extra_query_params: extra_query_params.as_slice(),
user_agent: user_agent.as_deref(),
+ // OpenRouter forwards explicit `cache_control` markers to Anthropic
+ // and Gemini, which cache nothing through a Chat Completions relay
+ // without them; hosted OpenAI rejects unknown part fields, so the
+ // flag is keyed on the relay, not on by default.
+ explicit_cache_control: super::crate_openai::endpoint_is_openrouter(&endpoint),
});
Some(Ok((chat, effective_model)))
}
diff --git a/src/openhuman/inference/provider/mod.rs b/src/openhuman/inference/provider/mod.rs
index 8e28581906..356572ec28 100644
--- a/src/openhuman/inference/provider/mod.rs
+++ b/src/openhuman/inference/provider/mod.rs
@@ -11,6 +11,8 @@ pub mod chat_template;
pub mod claude_agent_sdk;
pub mod claude_code;
pub mod config_rejection;
+/// Crate-native Anthropic Messages API client construction (prompt caching).
+pub mod crate_anthropic;
/// Crate-native OpenAI-compatible client construction (issue #4727, Motion B).
pub mod crate_openai;
pub mod error_classify;