fix(opencode): prevent negative cost when cache tokens exceed input#24572
fix(opencode): prevent negative cost when cache tokens exceed input#24572Felix-Hz wants to merge 4 commits into
Conversation
|
The following comment was made by an LLM, it may be inaccurate: Found a potential duplicate: PR #24336: fix(session): clamp token usage counts This PR appears to address the same root issue - clamping token usage counts to prevent negative values. It's highly likely this is either an existing fix for the same bug or a related attempt to solve the negative cost problem. |
I noticed #24336 also addresses this. My approach fixes the negative value at the token calculation level (with the |
Issue for this PR
Closes #22618
Type of change
What does this PR do?
I noticed a bug where the "$ spent" indicator in the TUI sidebar would decrease after a request. This was most visible when switching from a paid model (like Claude 3.5 Opus) to a free model (like MiniMax 2.5).
The root cause is in the
getUsagefunction insession.ts. It calculatesadjustedInputTokensby subtracting cached tokens from the reportedinputTokens:adjustedInputTokens = inputTokens - cacheReadTokens - cacheWriteTokensSome providers report
inputTokensas the total (including cache), while others report it as only the new tokens. In the latter case, subtracting the cache tokens results in a negative number. If the model being used has any cost associated with it (either through its own definition or a fallback), this negative value is multiplied by the price and added to the session total, effectively "refunding" the user and causing the spent amount to drop.I've updated the local
safehelper ingetUsageto ensure it always returns at least 0, preventing negative token counts and costs regardless of the provider's reporting format.How did you verify your code works?
packages/opencode/test/session/compaction.test.tsthat simulates a scenario wherecacheReadTokensexceedsinputTokens.bun test test/session/) and verified all 315 tests pass.bun run typecheckandoxlintto ensure the changes follow project standards and type safety.Screenshots / recordings
N/A (Logic fix for the "spent" display in the sidebar).
Checklist