Skip to main content

aggregate_usage_per_role

Function aggregate_usage_per_role 

Source
pub fn aggregate_usage_per_role(
    turns: &[TurnRecord],
    pm_model: &str,
    engineer_model: &str,
) -> (TokenUsage, f64)
Expand description

Sum every transcript turn’s usage, pricing EACH turn against its OWN role’s resolved model rather than blending the whole transcript under one model (#1475 bug 1 fix).

Why: The PM’s AgentOutput.usage only covers the PM’s own turns — the engineer’s usage is lost when its output flows back as a tool-result string. The transcript recorder, by contrast, captures the provider-reported usage of every PM and engineer turn, so summing those turns is the faithful run total. Pricing the WHOLE total against a single (PM) model — the original #1475 bug — silently mis-prices every engineer turn whenever the engineer routes to a different, often more expensive, model: directly undercuts the #1035 cost-comparison use case this run exists to support. This is the SAME fix task::executor applied for the daemon path (#2056); this is now the ONE shared implementation both paths call, so they can never independently drift. What: Field-wise sums each turn’s usage into a single TokenUsage (unchanged). Per turn, PREFERS the turn’s own usage.cost_usd — OpenRouter’s authoritative, already cache-discount-aware cost (response-side cache-usage fix) — when present; only falls back to the static per-token recompute (priced per role: pm_model for role == "pm", engineer_model otherwise) when the turn carries no authoritative cost. Sums the per-turn costs. Returns (total_usage, cost). Test: run_task::tests::usage_and_cost_aggregate_end_to_end, report::tests::aggregate_usage_per_role_prices_each_role_separately, report::tests::aggregate_usage_per_role_prefers_authoritative_cost.