Preserve provider-reported workflow costs#594
Merged
Conversation
Replace the billed_model_usage_from_llm_with_cost wrapper with a with_reported_cost method on BilledModelUsage and BilledTokenCounts, and centralize the optional-cost fold as UsdMicros::accumulate so fabro-agent and fabro-workflow share one implementation. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Contributor
There was a problem hiding this comment.
Pull request overview
This PR threads provider-reported LLM response costs (notably OpenRouter usage.cost) through the agent session, workflow event conversion, and stage billing so that authoritative provider costs can be persisted and used instead of (or alongside) catalog-derived estimates.
Changes:
- Add cost fields (
cost_usd,cost_source) to agent events and persist provenance on run events (AgentMessageProps.cost_source). - Accumulate per-response reported costs across multi-turn agent inputs and apply them to stage-level billing via
with_reported_cost(...). - Consolidate billing helpers in
fabro-model(UsdMicros::from_usd,UsdMicros::accumulate,with_reported_cost) and add regression + integration test coverage for override behavior.
Reviewed changes
Copilot reviewed 14 out of 14 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| lib/crates/fabro-workflow/tests/it/integration.rs | Adds E2E workflow test ensuring OpenRouter stream usage.cost persists onto the agent stage usage total. |
| lib/crates/fabro-workflow/src/outcome.rs | Adds unit coverage that provider-reported cost overrides catalog estimate on billed usage. |
| lib/crates/fabro-workflow/src/handler/llm/api.rs | Accumulates reported costs across retries/tool loops and stamps them onto stage billing; updates tests to assert totals. |
| lib/crates/fabro-workflow/src/event/convert.rs | Preserves per-response cost + provenance when converting agent events into stored run events. |
| lib/crates/fabro-types/src/run_event/mod.rs | Updates run-event tests for the new cost_source field on agent message props. |
| lib/crates/fabro-types/src/run_event/agent.rs | Adds AgentMessageProps.cost_source to expose cost provenance to consumers. |
| lib/crates/fabro-store/src/run_state.rs | Updates store-side test fixtures to include cost_source. |
| lib/crates/fabro-server/src/server/tests.rs | Updates server-side test fixtures to include cost_usd and cost_source on agent events. |
| lib/crates/fabro-server/src/server/handler/pair.rs | Updates handler tests for the new cost_source field on agent message props. |
| lib/crates/fabro-server/src/demo/mod.rs | Updates demo fixtures to include cost_source on agent message props. |
| lib/crates/fabro-model/src/billing.rs | Introduces UsdMicros helpers (from_usd, accumulate) and with_reported_cost overrides; adds unit test for accumulation semantics. |
| lib/crates/fabro-cli/src/commands/run/run_progress/mod.rs | Updates CLI progress test fixture to include cost fields on agent events. |
| lib/crates/fabro-agent/src/types.rs | Extends AgentEvent::AssistantMessage with cost_usd and cost_source plus serialization tests. |
| lib/crates/fabro-agent/src/session.rs | Tracks and sums per-response cost for each processed input (last_input_cost) and emits cost/provenance on assistant messages; adds regression test. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+60
to
+65
| pub fn accumulate(total: &mut Option<Self>, cost: Option<Self>) { | ||
| if let Some(cost) = cost { | ||
| *total.get_or_insert_default() += cost; | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
usage.cost) through the agent session, workflow events, and stage billing so authoritative provider costs override catalog estimateswith_reported_costmethods onBilledModelUsage/BilledTokenCountsandUsdMicros::accumulatereplace a_with_costwrapper function and a cost-fold idiom duplicated across cratesTest plan
cargo nextest run -p fabro-model -p fabro-agent -p fabro-workflow— 1817 passedcargo +nightly-2026-04-14 clippy --workspace --all-targets -- -D warnings— cleancargo +nightly-2026-04-14 fmt --check --all— clean🤖 Generated with Claude Code