Skip to content

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

feat(agent): track cost provenance in turn usage

Steven Enamakelcb3c7abf60f8
diff --git a/crates/openhuman-core/src/agent/session_host/codec.rs b/crates/openhuman-core/src/agent/session_host/codec.rs
index 7efac1d31d..80f9cca607 100644
--- a/crates/openhuman-core/src/agent/session_host/codec.rs
+++ b/crates/openhuman-core/src/agent/session_host/codec.rs
@@ -142,7 +142,7 @@ impl TranscriptCodec<OpenHumanRunContext> for OpenHumanTranscriptCodec {
if sidecar.input_tokens == 0
&& sidecar.output_tokens == 0
&& sidecar.cached_input_tokens == 0
- && sidecar.cost_usd == 0.0
+ && sidecar.cost.known_usd == 0.0
&& route.is_none()
{
return Ok(None);
@@ -161,7 +161,12 @@ impl TranscriptCodec<OpenHumanRunContext> for OpenHumanTranscriptCodec {
output: sidecar.output_tokens,
cached_input: sidecar.cached_input_tokens,
context_window: sidecar.context_window,
- cost_usd: sidecar.cost_usd,
+ cost_usd: sidecar.cost.known_usd,
+ cost_source: Some(match sidecar.cost.source {
+ crate::agent::cost::CostSource::Charged => UsageCostSource::Charged,
+ crate::agent::cost::CostSource::Estimated => UsageCostSource::Estimated,
+ crate::agent::cost::CostSource::Unknown => UsageCostSource::Unknown,
+ }),
last_call_input: sidecar.last_call_input_tokens,
last_call_output: sidecar.last_call_output_tokens,
},
diff --git a/crates/openhuman-core/src/agent/session_host/runtime_session.rs b/crates/openhuman-core/src/agent/session_host/runtime_session.rs
index ac719d548a..87b5798009 100644
--- a/crates/openhuman-core/src/agent/session_host/runtime_session.rs
+++ b/crates/openhuman-core/src/agent/session_host/runtime_session.rs
@@ -807,17 +807,16 @@ pub(super) fn holistic_last_turn_usage(
let cached_input_tokens = tokens(sidecar.cached_input_tokens, |usage| {
usage.cached_input_tokens
});
- let cost_usd = sidecar
- .subagents
- .iter()
- .fold(sidecar.cost_usd, |total, entry| {
- total + entry.usage.charged_amount_usd
- });
+ let mut cost = sidecar.cost;
+ for entry in &sidecar.subagents {
+ cost.merge(entry.usage.cost());
+ }
crate::agent::tinyagents::host::LastTurnUsage {
input_tokens,
output_tokens,
cached_input_tokens,
- cost_usd,
+ cost_usd: cost.usd(),
+ cost_source: cost.source,
context_window: sidecar.context_window,
context_tokens: sidecar
.last_call_input_tokens
diff --git a/crates/openhuman-core/src/agent/subagent_host/types.rs b/crates/openhuman-core/src/agent/subagent_host/types.rs
index 147d93659b..1ab660e3cb 100644
--- a/crates/openhuman-core/src/agent/subagent_host/types.rs
+++ b/crates/openhuman-core/src/agent/subagent_host/types.rs
@@ -195,7 +195,22 @@ pub struct SubagentUsage {
pub input_tokens: u64,
pub output_tokens: u64,
pub cached_input_tokens: u64,
+ /// Sum of the run's known call costs; read with `cost_source`.
pub charged_amount_usd: f64,
+ /// How certain `charged_amount_usd` is. `Unknown` means some call had no
+ /// charge and no catalogued price, so the sum is incomplete.
+ #[serde(default)]
+ pub cost_source: crate::agent::cost::CostSource,
+}
+
+impl SubagentUsage {
+ /// The run's cost as a tally, for folding into a parent's.
+ pub fn cost(&self) -> crate::agent::cost::CostTally {
+ crate::agent::cost::CostTally {
+ known_usd: self.charged_amount_usd,
+ source: self.cost_source,
+ }
+ }
}
/// Which prompt-construction path the runner took for a sub-agent.
diff --git a/crates/openhuman-core/src/agent/tinyagents/host/run_context.rs b/crates/openhuman-core/src/agent/tinyagents/host/run_context.rs
index e8760c03fc..509a410b21 100644
--- a/crates/openhuman-core/src/agent/tinyagents/host/run_context.rs
+++ b/crates/openhuman-core/src/agent/tinyagents/host/run_context.rs
@@ -87,7 +87,12 @@ pub struct LastTurnUsage {
pub input_tokens: u64,
pub output_tokens: u64,
pub cached_input_tokens: u64,
- pub cost_usd: f64,
+ /// The turn's cost including children, or `None` when any call's cost is
+ /// unknown (no reported charge and no catalogued price). Never a guess.
+ pub cost_usd: Option<f64>,
+ /// Whether `cost_usd` is entirely provider-charged or includes a catalog
+ /// estimate (`Unknown` exactly when `cost_usd` is `None`).
+ pub cost_source: crate::agent::cost::CostSource,
pub context_window: u64,
/// Tokens the root agent's context held after the turn's final model call
/// (that call's input plus its reply): the numerator of the context-window
diff --git a/crates/openhuman-core/src/web_chat/presentation.rs b/crates/openhuman-core/src/web_chat/presentation.rs
index 207c2cc277..42ad246e2c 100644
--- a/crates/openhuman-core/src/web_chat/presentation.rs
+++ b/crates/openhuman-core/src/web_chat/presentation.rs
@@ -23,7 +23,7 @@ fn usage_payload(usage: Option<&LastTurnUsage>) -> Option<TurnUsagePayload> {
agent_id: s.agent_id.clone(),
input_tokens: s.usage.input_tokens,
output_tokens: s.usage.output_tokens,
- cost_usd: s.usage.charged_amount_usd,
+ cost_usd: s.usage.cost().usd(),
})
.collect();
Some(TurnUsagePayload {
@@ -31,6 +31,7 @@ fn usage_payload(usage: Option<&LastTurnUsage>) -> Option<TurnUsagePayload> {
output_tokens: usage.output_tokens,
cached_input_tokens: usage.cached_input_tokens,
cost_usd: usage.cost_usd,
+ cost_source: usage.cost_source,
context_window: usage.context_window,
context_tokens: usage.context_tokens,
subagents,