Skip to content

fix: report input_tokens in streaming responses for accurate context tracking#5

Open
andyzengmath wants to merge 1 commit into
samarth777:mainfrom
andyzengmath:fix/stream-token-reporting
Open

fix: report input_tokens in streaming responses for accurate context tracking#5
andyzengmath wants to merge 1 commit into
samarth777:mainfrom
andyzengmath:fix/stream-token-reporting

Conversation

@andyzengmath

Copy link
Copy Markdown

Problem

Claude Code's HUD always shows 0% context usage when using this proxy because:

  1. message_start was sent before extracting data.usage from the first chunk, so input_tokens was always 0
  2. message_delta events only reported output_tokens, never input_tokens
  3. prompt_tokens || inputTokens has a JS falsy-zero bug — if the API returns {prompt_tokens: 0}, the || operator treats it as falsy and keeps the stale value

Fix

  • Reorder usage extraction before sendStartIfNeeded() so message_start includes accurate input_tokens when available on the first chunk
  • Add input_tokens to all message_delta.usage events so Claude Code can update its running context window total
  • Use ?? (nullish coalescing) instead of || to correctly handle 0 as a valid token count

Testing

Verified that Claude Code's HUD now shows accurate, non-zero context percentage after this fix.

…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)
@andyzengmath

Copy link
Copy Markdown
Author

Note: this token-reporting fix is now also included in #4 (bundled with the Serper/cache/Opus-4.8 work). This PR remains available as the standalone version if you'd prefer to merge the token fix in isolation against main — whichever you prefer. If you merge #4 first, this can be closed.

2 similar comments
@andyzengmath

Copy link
Copy Markdown
Author

Note: this token-reporting fix is now also included in #4 (bundled with the Serper/cache/Opus-4.8 work). This PR remains available as the standalone version if you'd prefer to merge the token fix in isolation against main — whichever you prefer. If you merge #4 first, this can be closed.

@andyzengmath

Copy link
Copy Markdown
Author

Note: this token-reporting fix is now also included in #4 (bundled with the Serper/cache/Opus-4.8 work). This PR remains available as the standalone version if you'd prefer to merge the token fix in isolation against main — whichever you prefer. If you merge #4 first, this can be closed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant