fix(task): record token usage in async billing logs#5841
Conversation
WalkthroughAdds PromptTokens and CompletionTokens fields to RecordTaskBillingLogParams, persists them on Log records, and computes TokenUsed from their sum. Introduces RecalculateTaskQuotaWithUsage to carry token counts through settlement; RecalculateTaskQuota and RecalculateTaskQuotaByTokens now delegate to it. ChangesTask Billing Token Usage Tracking
Estimated code review effort: 2 (Simple) | ~10 minutes Sequence Diagram(s)sequenceDiagram
participant Caller
participant RecalculateTaskQuota
participant RecalculateTaskQuotaWithUsage
participant RecordTaskBillingLog
participant LogStore
Caller->>RecalculateTaskQuota: request settlement
RecalculateTaskQuota->>RecalculateTaskQuotaWithUsage: delegate (promptTokens, completionTokens)
RecalculateTaskQuotaWithUsage->>RecordTaskBillingLog: record(params incl. token counts)
RecordTaskBillingLog->>LogStore: persist Log with PromptTokens, CompletionTokens
RecordTaskBillingLog->>LogStore: compute TokenUsed = PromptTokens + CompletionTokens
Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
service/task_billing.go (1)
192-253: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winAdd test coverage for the new token-usage propagation.
The referenced test file doesn't appear to assert
PromptTokens/CompletionTokensvalues on the resultingLogrecord forRecalculateTaskQuotaWithUsage/RecalculateTaskQuotaByTokens. Since the PR's core motivation is fixing zero-token display for these settlement logs, a test verifyinglog.PromptTokens/log.CompletionTokensare populated as expected would guard against regressions.Also applies to: 258-308
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@service/task_billing.go` around lines 192 - 253, Add test coverage around RecalculateTaskQuotaWithUsage and RecalculateTaskQuotaByTokens to assert token usage is propagated into the billing log. In the relevant test(s), inspect the Log record created by RecordTaskBillingLog and verify PromptTokens and CompletionTokens are populated with the expected values rather than left at zero, using the existing RecalculateTaskQuotaWithUsage helper and any shared billing-log assertions to locate the record. Ensure the test covers the settlement path where quota is recalculated so this regression is caught in future changes.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@service/task_billing.go`:
- Around line 192-253: Add test coverage around RecalculateTaskQuotaWithUsage
and RecalculateTaskQuotaByTokens to assert token usage is propagated into the
billing log. In the relevant test(s), inspect the Log record created by
RecordTaskBillingLog and verify PromptTokens and CompletionTokens are populated
with the expected values rather than left at zero, using the existing
RecalculateTaskQuotaWithUsage helper and any shared billing-log assertions to
locate the record. Ensure the test covers the settlement path where quota is
recalculated so this regression is caught in future changes.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: cf31de21-93bb-4ad4-9407-8389588a1c81
📒 Files selected for processing (2)
model/log.goservice/task_billing.go
Summary
Why
Async task settlement can recalculate quota from upstream
total_tokens, but the follow-up consume/refund log only stored the token count in the human-readable content string. As a result, the admin log token column and token-based exports showed zero tokens for these settlement logs even though billing used the token count correctly.Test
Summary by CodeRabbit
New Features
Bug Fixes