fix: report input_tokens in streaming responses for accurate context tracking#5
Open
andyzengmath wants to merge 1 commit into
Open
fix: report input_tokens in streaming responses for accurate context tracking#5andyzengmath wants to merge 1 commit into
andyzengmath wants to merge 1 commit into
Conversation
…tracking - Reorder usage extraction before sendStartIfNeeded() so message_start includes input_tokens when available on first chunk - Add input_tokens to message_delta.usage events so Claude Code can track context window usage accurately - Fix falsy-zero bug: use ?? instead of || for prompt_tokens/completion_tokens (0 is a valid value that || incorrectly treats as falsy)
Author
2 similar comments
Author
Author
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.
Problem
Claude Code's HUD always shows 0% context usage when using this proxy because:
message_startwas sent before extractingdata.usagefrom the first chunk, soinput_tokenswas always 0message_deltaevents only reportedoutput_tokens, neverinput_tokensprompt_tokens || inputTokenshas a JS falsy-zero bug — if the API returns{prompt_tokens: 0}, the||operator treats it as falsy and keeps the stale valueFix
sendStartIfNeeded()somessage_startincludes accurateinput_tokenswhen available on the first chunkinput_tokensto allmessage_delta.usageevents so Claude Code can update its running context window total??(nullish coalescing) instead of||to correctly handle0as a valid token countTesting
Verified that Claude Code's HUD now shows accurate, non-zero context percentage after this fix.