fix(session): clamp token usage counts#24336
Conversation
|
The following comment was made by an LLM, it may be inaccurate: Based on my search, I found one potentially related PR: PR #24232: fix(session): honor noCacheTokens in usage accounting This PR is related because it also deals with session token usage accounting. Since PR #24336 addresses issues with cache read/write tokens and negative input token counts in The other PRs that appeared in results (#22077, #14743, etc.) are related to token accumulation and cache configuration but don't appear to be duplicates of this specific fix. |
|
#24232 is related and touches the same
If #24232 merges first, I can rebase this and keep only the remaining defensive clamp behavior that is not covered there, or close this if maintainers consider #24232 sufficient. |
|
Automated PR Cleanup Thank you for contributing to opencode. Due to the high volume of PRs from users and AI agents, we periodically close older PRs using automated criteria so maintainers can focus review time on the most active and community-supported contributions. This PR was closed because it matched the following cleanup criteria:
PRs created within the last month are not affected by this cleanup. If you believe this PR was closed incorrectly, or if you are still actively working on it, please leave a comment explaining why it should be reopened. A maintainer can review and reopen it if appropriate. Thanks again for taking the time to contribute. |
Issue for this PR
Closes #15293
Type of change
What does this PR do?
Provider usage payloads can report cache read/write tokens larger than the normalized input token count.
Session.getUsage()subtracted those values directly, which could persist negativetokens.inputvalues and produce misleading analytics/cost data. This clamps raw and derived token counters to zero while preserving cache read/write accounting.How did you verify your code works?
bun test test/session/compaction.test.ts -t "SessionNs.getUsage"bun typecheckfrompackages/opencodebun turbo typecheckScreenshots / recordings
N/A, non-UI change.
Checklist