Skip to content

Preserve provider-reported workflow costs#594

Merged
brynary merged 4 commits into
mainfrom
preserve-provider-costs
Jul 23, 2026
Merged

Preserve provider-reported workflow costs#594
brynary merged 4 commits into
mainfrom
preserve-provider-costs

Conversation

@brynary

@brynary brynary commented Jul 23, 2026

Copy link
Copy Markdown
Member

Summary

  • Thread provider-reported response costs (e.g. OpenRouter usage.cost) through the agent session, workflow events, and stage billing so authoritative provider costs override catalog estimates
  • Add regression coverage: unit tests for cost accumulation and override behavior, plus an end-to-end workflow test asserting the provider-reported cost is persisted on the agent stage
  • Consolidate cost handling in fabro-model: with_reported_cost methods on BilledModelUsage/BilledTokenCounts and UsdMicros::accumulate replace a _with_cost wrapper function and a cost-fold idiom duplicated across crates

Test plan

  • cargo nextest run -p fabro-model -p fabro-agent -p fabro-workflow — 1817 passed
  • cargo +nightly-2026-04-14 clippy --workspace --all-targets -- -D warnings — clean
  • cargo +nightly-2026-04-14 fmt --check --all — clean

🤖 Generated with Claude Code

brynary and others added 3 commits July 23, 2026 06:59
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>
Copilot AI review requested due to automatic review settings July 23, 2026 11:24

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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;
}
}
}
Copilot AI review requested due to automatic review settings July 23, 2026 11:53

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 15 out of 15 changed files in this pull request and generated no new comments.

@brynary
brynary merged commit 580ee15 into main Jul 23, 2026
14 checks passed
@brynary
brynary deleted the preserve-provider-costs branch July 23, 2026 12:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants