Skip to content

fix(api): bill recorded model usage after agent errors - #2302

Open
tomatomptt wants to merge 3 commits into
refly-ai:mainfrom
tomatomptt:fix/bill-recorded-usage-on-system-errors
Open

tomatomptt wants to merge 3 commits into
refly-ai:mainfrom
tomatomptt:fix/bill-recorded-usage-on-system-errors

Conversation

@tomatomptt

@tomatomptt tomatomptt commented Oct 21, 2026 •

Copy link
Copy Markdown

Summary

SkillInvokerService currently skips credit settlement when an Agent run ends with a system/provider error, even when earlier model calls completed and their token usage was recorded in resultAggregator. The finalizer now processes the recorded usage whenever the aggregator exists, for all terminal outcomes.

Added a regression suite covering successful completion, user abort, and a later provider/system error after an earlier successful model call.

Validation

  • Biome check passed on the prepared patch.
  • The new billing regression tests passed (3 tests).
  • API Jest suite: 311 passed, 26 failed; the same 26 failures were present on the baseline (308 passed), so the delta is the 3 added passing tests.
  • These checks were not rerun after the GitHub web-editor commit.

Summary by CodeRabbit

  • Bug Fixes

    • Credit billing now processes recorded usage for skill executions that end with provider failures, in addition to successful and user-aborted runs.
    • Terminal execution statuses and usage totals are handled consistently across supported completion outcomes.
  • Tests

    • Added coverage for credit billing across completed, user-aborted, and provider-failed skill executions.
    • Added verification of terminal statuses and aggregated input-token totals.

Always process accumulated model usage when resultAggregator exists, including system-error outcomes. The regression test already added to this branch covers failed agent runs.
@coderabbitai

coderabbitai Bot commented Oct 21, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 6db38f39-6ea0-459a-b684-2232c84c9ade
📥 Commits

Reviewing files that changed from the base of the PR and between 71f8b87 and ae83857.

📒 Files selected for processing: 2
  • apps/api/src/modules/skill/skill-invoker.billing.spec.ts
  • apps/api/src/modules/skill/skill-invoker.service.ts

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.

📝 Walkthrough

Skill invocation billing now reports recorded usage for every terminal outcome when a result aggregator exists. A new test suite covers completed, user-aborted, and provider-failed runs.

Changes: Skill invocation billing

Layer / File(s) Summary
Billing across terminal outcomes
apps/api/src/modules/skill/skill-invoker.service.ts, apps/api/src/modules/skill/skill-invoker.billing.spec.ts
Billing no longer requires a successful result or user abort. Tests verify billing calls and recorded input-token totals for completed, user-aborted, and provider-failed runs.

Priority: ⬇️ Low

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix

Suggested reviewers: mrcfps

Merge Risk: ⚪ Minimal · up to ae838

Recorded usage is billed after terminal errors without changing the delivered terminal status. No identified issue needs resolution before merge.

Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 2 functions across 2 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 4 passed
Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title clearly and concisely describes the main change: billing recorded model usage after agent errors.
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

This branch has not been deployed

No deployments
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.

1 participant