Skip to content

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

Merge upstream/main into w5-usage; repin vendor to merged module mains

Steven Enamakel249979e66923
diff --cc crates/openhuman-core/src/agent/tinyagents/model_g1_usage_tests_tests.rs
index 837f55ad9e,7aa4a49667..3cc5037981
--- a/crates/openhuman-core/src/agent/tinyagents/model_g1_usage_tests_tests.rs
+++ b/crates/openhuman-core/src/agent/tinyagents/model_g1_usage_tests_tests.rs
@@@ -141,3 -136,80 +136,44 @@@ fn tool_request() -> ModelRequest
..Default::default()
}
}
+
-#[test]
-fn prompt_guided_response_uses_tinyagents_xml_parser() {
- let response = prompt_guided_text_response(
- r#"Checking.<tool_call>{"name":"lookup","arguments":{"id":7}}</tool_call>"#.to_string(),
- &tool_request(),
- );
-
- assert_eq!(response.text(), "Checking.");
- assert_eq!(response.message.tool_calls.len(), 1);
- assert!(
- !response.message.tool_calls[0].id.is_empty(),
- "the upstream parser assigns the tool-call ID"
- );
- assert_eq!(response.message.tool_calls[0].name, "lookup");
- assert_eq!(
- response.message.tool_calls[0].arguments,
- serde_json::json!({"id": 7})
- );
-}
-
-#[test]
-fn prompt_guided_response_keeps_legacy_pformat_fallback() {
- let response = prompt_guided_text_response(
- "<tool_call>lookup[0|7|1|needle]</tool_call>".to_string(),
- &tool_request(),
- );
-
- assert_eq!(response.text(), "");
- assert_eq!(response.message.tool_calls.len(), 1);
- assert_eq!(response.message.tool_calls[0].name, "lookup");
- assert_eq!(
- response.message.tool_calls[0].arguments,
- serde_json::json!({"id": 7, "query": "needle"})
- );
-}
-
+ /// Wire shape of the billing/context metadata stashed in `ModelResponse.raw`:
+ /// a literal-JSON pin so the key and field names cannot drift, and a response
+ /// written by the current release (with the old key set) still reconstructs.
+ #[test]
+ fn usage_meta_raw_wire_shape_is_stable_and_old_payloads_load() {
+ let chat = ChatResponse {
+ text: Some("hi".to_string()),
+ tool_calls: Vec::new(),
+ usage: Some(
+ BilledUsage::from_counts(100, 20)
+ .with_context_window(128_000)
+ .with_charged_usd(0.0123),
+ ),
+ reasoning_content: None,
+ };
+ let written = response_to_model_response(&chat, &empty_registry(), false);
+ assert_eq!(
+ written.raw,
+ Some(serde_json::json!({
+ "openhuman_usage_meta": {"charged_amount_usd": 0.0123, "context_window": 128000}
+ }))
+ );
+
+ let mut old = response_to_model_response(
+ &ChatResponse {
+ text: Some("x".into()),
+ usage: Some(BilledUsage::from_counts(7, 2)),
+ ..Default::default()
+ },
+ &empty_registry(),
+ false,
+ );
+ old.raw = Some(serde_json::json!({
+ "openhuman_usage_meta": {"charged_amount_usd": 0.5, "context_window": 32000}
+ }));
+ let recovered = usage_info_from_response(&old).expect("usage");
+ assert_eq!(recovered.charged_amount_usd, 0.5);
+ assert_eq!(recovered.context_window(), 32_000);
+ assert_eq!(recovered.input_tokens, 7);
+ }
diff --cc crates/openhuman-core/src/inference/provider/mod.rs
index bd4880bc70,fe51ecd1da..639f664c6e
--- a/crates/openhuman-core/src/inference/provider/mod.rs
+++ b/crates/openhuman-core/src/inference/provider/mod.rs
@@@ -16,7 -16,9 +16,9 @@@ pub mod ops
pub mod types;
#[allow(unused_imports)]
- pub use types::{ChatResponse, ProviderDelta, UsageInfo, AGENT_TURN_MAX_OUTPUT_TOKENS};
+ pub use types::{
- ChatRequest, ChatResponse, ProviderDelta, BilledUsage, AGENT_TURN_MAX_OUTPUT_TOKENS,
++ BilledUsage, ChatResponse, ProviderDelta, UsageInfo, AGENT_TURN_MAX_OUTPUT_TOKENS,
+ };
pub use error_code::{
backend_error_code_skips_sentry, body_flags_malformed, extract_backend_error_code,
diff --cc crates/openhuman-core/src/inference/provider/types.rs
index cb3c6f93f2,211f6458ea..6e262cdd27
--- a/crates/openhuman-core/src/inference/provider/types.rs
+++ b/crates/openhuman-core/src/inference/provider/types.rs
@@@ -1,9 -1,84 +1,86 @@@
-use crate::agent::messages::ChatMessage;
+/// Token usage returned by a provider. Defined in the contract crate because
+/// the extracted memory subsystem threads it out of summarisation runs; every
+/// existing `inference::provider::UsageInfo` path keeps naming this one type.
+pub use tinymemory_api::host::UsageInfo;
+ use tinyinference_llm::usage::Usage;
-use tinytools::ToolSpec;
use tinytools_agent::dialect::NativeToolCall;
+ /// Token usage returned by a provider: the vendor [`Usage`] token counts plus
+ /// the host-owned billing the vendor type deliberately does not carry.
+ ///
+ /// `usage` is the single token-count representation threaded everywhere
+ /// (cache reads/writes, reasoning tokens and the context window all have
+ /// homes on it). Only the provider-charged amount stays host data: it is kept
+ /// as an exact `f64` USD value (the vendor `ChargedAmount` is integer micro
+ /// units and would round the persisted cost rows). `Deref` exposes the
+ /// vendor fields (`input_tokens`, `output_tokens`, `cache_creation_tokens`,
+ /// `reasoning_tokens`, ...) directly.
+ #[derive(Debug, Clone, Copy, Default, PartialEq)]
+ pub struct BilledUsage {
+ /// Vendor token counts for the call.
+ pub usage: Usage,
+ /// Amount billed for this request in USD (from
+ /// `openhuman.billing.charged_amount_usd`). Zero when unavailable.
+ pub charged_amount_usd: f64,
+ }
+
+ impl BilledUsage {
+ /// Plain token counts with no cache breakdown, context window or charge.
+ pub fn from_counts(input_tokens: u64, output_tokens: u64) -> Self {
+ Self {
+ usage: Usage::new(input_tokens, output_tokens),
+ charged_amount_usd: 0.0,
+ }
+ }
+
+ /// Input tokens served from the provider prompt/KV cache.
+ pub fn cached_input_tokens(&self) -> u64 {
+ self.usage.cache_read_tokens
+ }
+
+ /// Model context window in tokens; `0` when unknown.
+ pub fn context_window(&self) -> u64 {
+ self.usage.context_window_tokens.unwrap_or(0)
+ }
+
+ /// Sets the cache-read token count.
+ pub fn with_cached_input_tokens(mut self, tokens: u64) -> Self {
+ self.usage.cache_read_tokens = tokens;
+ self
+ }
+
+ /// Sets the context window (`0` means unknown).
+ pub fn with_context_window(mut self, tokens: u64) -> Self {
+ self.usage.context_window_tokens = (tokens > 0).then_some(tokens);
+ self
+ }
+
+ /// Sets the cache-creation (write) token count.
+ pub fn with_cache_creation_tokens(mut self, tokens: u64) -> Self {
+ self.usage.cache_creation_tokens = tokens;
+ self
+ }
+
+ /// Sets the reasoning/thinking token count.
+ pub fn with_reasoning_tokens(mut self, tokens: u64) -> Self {
+ self.usage.reasoning_tokens = tokens;
+ self
+ }
+
+ /// Sets the provider-charged USD amount.
+ pub fn with_charged_usd(mut self, usd: f64) -> Self {
+ self.charged_amount_usd = usd;
+ self
+ }
+ }
+
+ impl std::ops::Deref for BilledUsage {
+ type Target = Usage;
+ fn deref(&self) -> &Usage {
+ &self.usage
+ }
+ }
+
/// An LLM response that may contain text, tool calls, or both.
#[derive(Debug, Clone, Default)]
pub struct ChatResponse {