-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
base: main
Are you sure you want to change the base?
Conversation
// 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. |
There was a problem hiding this comment.
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() |
There was a problem hiding this comment.
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.`) |
There was a problem hiding this comment.
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.
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:
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):
Roo-Code/src/core/task/Task.ts
Line 1430 in 714fafd
(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
Screenshots / Videos
In this screenshot all costs are reported and the tally is correct ($0.0591):
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.Task
class inTask.ts
by ensuring all request chunks are processed.drainStreamInBackgroundToFindAllUsage()
to process remaining chunks and update usage data even if a request is interrupted.recursivelyMakeClineRequests()
to handle incomplete requests by processing all chunks in the background.updateApiReqMsg()
to include all token and cost data.TelemetryService
call for capturing LLM completion in the main loop, now handled in the background task.This description was created by
for 60f0a03. You can customize this summary. It will automatically update as commits are pushed.