Skip to content

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

feat(cost): distinguish estimated from provider-charged spend

Steven Enamakel052075265c8a
diff --git a/crates/openhuman-core/src/agent/tinyagents/observability/event_bridge.rs b/crates/openhuman-core/src/agent/tinyagents/observability/event_bridge.rs
index 159daaaf3c..0e80ae5630 100644
--- a/crates/openhuman-core/src/agent/tinyagents/observability/event_bridge.rs
+++ b/crates/openhuman-core/src/agent/tinyagents/observability/event_bridge.rs
@@ -368,11 +368,12 @@ impl OpenhumanEventBridge {
// estimate precedence, so credit-metered backends surface real billing
// rather than a token-rate estimate).
let estimate = Self::estimate_call_cost(&self.model, usage);
- let call_cost = carried
+ let provider_cost = carried
.as_ref()
.map(|u| u.charged_amount_usd)
- .filter(|c| c.is_finite() && *c > 0.0)
- .unwrap_or(estimate);
+ .filter(|c| c.is_finite() && *c > 0.0);
+ let cost_is_estimate = provider_cost.is_none();
+ let call_cost = provider_cost.unwrap_or(estimate);
// The context window + cache-creation/reasoning breakdown only exist on
// the carried provider usage (the crate `Usage` mapping drops them); fall
// back to the catalogue window and the crate token counts when absent.
@@ -441,8 +442,12 @@ impl OpenhumanEventBridge {
.with_context_window(context_window)
.with_cached_input_tokens(usage.cache_read_tokens)
.with_cache_creation_tokens(cache_creation_tokens)
- .with_reasoning_tokens(reasoning_tokens)
- .with_charged_usd(call_cost);
+ .with_reasoning_tokens(reasoning_tokens);
+ let usage_info = if cost_is_estimate {
+ usage_info.with_estimated_usd(call_cost)
+ } else {
+ usage_info.with_charged_usd(call_cost)
+ };
if reasoning_tokens > 0 || cache_creation_tokens > 0 {
log::debug!(
"[cost] recording reasoning/cache-creation tokens model={} reasoning_tokens={} cache_creation_tokens={}",
diff --git a/crates/openhuman-core/src/inference/provider/types.rs b/crates/openhuman-core/src/inference/provider/types.rs
index 93f16a7e0a..d224380b3d 100644
--- a/crates/openhuman-core/src/inference/provider/types.rs
+++ b/crates/openhuman-core/src/inference/provider/types.rs
@@ -18,6 +18,9 @@ pub struct BilledUsage {
/// Amount billed for this request in USD (from
/// `openhuman.billing.charged_amount_usd`). Zero when unavailable.
pub charged_amount_usd: f64,
+ /// `charged_amount_usd` is a local catalog estimate, not a provider charge
+ /// (see [`BilledUsage::with_estimated_usd`]).
+ pub cost_is_estimate: bool,
}
impl BilledUsage {
@@ -26,6 +29,7 @@ impl BilledUsage {
Self {
usage: Usage::new(input_tokens, output_tokens),
charged_amount_usd: 0.0,
+ cost_is_estimate: false,
}
}
@@ -66,6 +70,15 @@ impl BilledUsage {
/// Sets the provider-charged USD amount.
pub fn with_charged_usd(mut self, usd: f64) -> Self {
self.charged_amount_usd = usd;
+ self.cost_is_estimate = false;
+ self
+ }
+
+ /// Sets a locally estimated USD cost, kept apart from provider charges so
+ /// usage reports can split charged from estimated spend.
+ pub fn with_estimated_usd(mut self, usd: f64) -> Self {
+ self.charged_amount_usd = usd;
+ self.cost_is_estimate = true;
self
}
}
diff --git a/crates/openhuman-core/src/platform/cost/global.rs b/crates/openhuman-core/src/platform/cost/global.rs
index ef9553ed69..10d554f53a 100644
--- a/crates/openhuman-core/src/platform/cost/global.rs
+++ b/crates/openhuman-core/src/platform/cost/global.rs
@@ -183,7 +183,9 @@ pub(super) fn build_token_usage(model: &str, usage: &BilledUsage) -> Option<Toke
return None;
}
let total_tokens = usage.input_tokens.saturating_add(usage.output_tokens);
- let provider_charged = usage.charged_amount_usd.is_finite() && usage.charged_amount_usd > 0.0;
+ let provider_charged = !usage.cost_is_estimate
+ && usage.charged_amount_usd.is_finite()
+ && usage.charged_amount_usd > 0.0;
Some(TokenUsage {
model: model.to_string(),
input_tokens: usage.input_tokens,