fix(llmcore): record Claude messages-API output_tokens instead of zero - #761
Open
Kailigithub wants to merge 1 commit into
Open
fix(llmcore): record Claude messages-API output_tokens instead of zero#761Kailigithub wants to merge 1 commit into
Kailigithub wants to merge 1 commit into
Conversation
_record_usage(usage, api_mode='messages') hard-coded out = 0, so STATS['out'] was never updated for Claude (Anthropic Messages API) sessions and the '[Output] tokens=...' log line never printed. Claude's usage dict emits output_tokens just like the OpenAI chat_completions and responses branches already do, so the messages branch should pick it up too. Read usage['output_tokens'] through the same _i(...) helper that coerces None and non-int values to 0, and add the missing [Output] log line so the three branches stay symmetric. Adds tests/test_llmcore_record_usage_messages.py with 5 cases covering happy path, missing key, null value, and regression checks on the chat_completions and responses branches. Verified by reverting llmcore.py to upstream/main: tests pass on the fix and the happy-path messages case fails on the unfixed code.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
llmcore._record_usage(usage, api_mode='messages')hard-codedout = 0. As a result:STATS['out']was never updated for Claude (Anthropic Messages API) sessions — the user-facing cost / running-stats display stayed stale.[Output] tokens=...log line never printed for Claude sessions, while the chat_completions and responses branches both emit it.This PR reads
usage['output_tokens']through the same_i(...)helper the other branches already use (which coercesNoneand non-int values to0) and adds the missing log line so the three branches stay symmetric.Diff
2 lines added, 1 changed. No behavior change on existing inputs except that Claude output_tokens are now counted.
Tests
tests/test_llmcore_record_usage_messages.pycovers:messageshappy path — STATS['out'] populated, [Output] line emitted.messageswithoutput_tokenskey absent — degrades to 0, no log line.messageswithoutput_tokens: null— _i coerces to 0 (consistent with the other branches).chat_completionsregression — untouched, still correct.responsesregression — untouched, still correct.The regression tests for the other branches are intentional:
_record_usageis shared, and the diff is small enough that a single typo could break all three modes. Keeping a 5-case suite means future refactors that touch one branch can't silently regress the others.Verification
This PR is the 'optimize' branch from a saturated-repo 3-bucket cron run; it pairs with #760 (fix(fsapp) #685) for bug-fix coverage on the same tick.