Skip to content

Fix token usage / cost often being underreported #6122

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

chrarnoldus
Copy link

@chrarnoldus chrarnoldus commented Jul 23, 2025

Kilo Code PR: Kilo-Org/kilocode#1447

Description

Roo Code often underreports the total cost of a task, because the cost of some requests are missing. For example:

CleanShot 2025-07-23 at 14 03 33 CleanShot 2025-07-23 at 14 03 51

The first $0.00677 request is missing its cost in the UI, while requests 2-4 are not. Also the total cost (should be $0.0816) is off.

This is because a request is abandoned before the usage block has been processed when an LLM tries to use multiple tools (only one is allowed in the rules):

if (this.didAlreadyUseTool) {

(this code is inherited from Cline)

Test Procedure

Run tasks with a variety of providers and models and verify all requests have a cost reported. Kimi K2 seems to be especially affected. Sonnet 4 sometimes, Gemini 2.5 Pro not so much.

I did test this with OpenRouter and costs are reported more reliably than before. Also tested with Anthropic and Ollama for regressions.

Pre-Submission Checklist

  • Issue Linked: This PR is linked to an approved GitHub Issue (see "Related GitHub Issue" above).
  • Scope: My changes are focused on the linked issue (one major feature/fix per PR).
  • Self-Review: I have performed a thorough self-review of my code.
  • Testing: New and/or updated tests have been added to cover my changes (if applicable).
  • Documentation Impact: I have considered if my changes require documentation updates (see "Documentation Updates" section below).
  • Contribution Guidelines: I have read and agree to the Contributor Guidelines.

Screenshots / Videos

In this screenshot all costs are reported and the tally is correct ($0.0591):

CleanShot 2025-07-23 at 16 49 46 CleanShot 2025-07-23 at 17 01 30

Additional Notes

I'll add some comments to the code.

Get in Touch

Christiaan in shared Slack


Important

Fixes underreporting of token usage and cost in Task.ts by processing all request chunks and updating usage data even if requests are interrupted.

  • Behavior:
    • Fixes underreporting of token usage and cost in Task class in Task.ts by ensuring all request chunks are processed.
    • Adds drainStreamInBackgroundToFindAllUsage() to process remaining chunks and update usage data even if a request is interrupted.
  • Functions:
    • Modifies recursivelyMakeClineRequests() to handle incomplete requests by processing all chunks in the background.
    • Updates updateApiReqMsg() to include all token and cost data.
  • Misc:
    • Removes redundant TelemetryService call for capturing LLM completion in the main loop, now handled in the background task.

This description was created by Ellipsis for 60f0a03. You can customize this summary. It will automatically update as commits are pushed.

@chrarnoldus chrarnoldus requested review from mrubens, cte and jr as code owners July 23, 2025 15:07
@dosubot dosubot bot added size:M This PR changes 30-99 lines, ignoring generated files. bug Something isn't working labels Jul 23, 2025
// PREV: We need to let the request finish for openrouter to
// get generation details.
// UPDATE: It's better UX to interrupt the request at the
// cost of the API cost not being retrieved.
Copy link
Author

Choose a reason for hiding this comment

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

The comment calls out OpenRouter specifically, but I think if seen usage being misreported with other providers as well.

}
}
if (usageFound) {
updateApiReqMsg()
Copy link
Author

@chrarnoldus chrarnoldus Jul 23, 2025

Choose a reason for hiding this comment

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

Not sure if calling this is enough... I think it won't update the UI if the usage arrives after the task finished (you need to reopen the task).

),
})
} else {
console.warn(`Suspicious: request ${lastApiReqIndex} is complete, but no usage info was found.`)
Copy link
Author

Choose a reason for hiding this comment

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

I have seen this happen, but so far only with OpenRouter routing to BaseTen.

@hannesrudolph hannesrudolph added the Issue/PR - Triage New issue. Needs quick review to confirm validity and assign labels. label Jul 23, 2025
@daniel-lxs daniel-lxs moved this from Triage to PR [Needs Prelim Review] in Roo Code Roadmap Jul 24, 2025
@hannesrudolph hannesrudolph added PR - Needs Preliminary Review and removed Issue/PR - Triage New issue. Needs quick review to confirm validity and assign labels. labels Jul 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working PR - Needs Preliminary Review size:M This PR changes 30-99 lines, ignoring generated files.
Projects
Status: PR [Needs Prelim Review]
Development

Successfully merging this pull request may close these issues.

2 participants