fix(streaming): fix estimateTokenCount overestimation causing max_tokens: 1 (#83)#84
Merged
Merged
Conversation
…ens: 1 (#83) The word-count-based heuristic (words * 1.15) in estimateTokenCount() dramatically overestimates token counts for JSON-serialized chat messages (3-5x actual), because every structural JSON character ({, }, ", :, ,) is counted as a separate word. For conversations approaching the context window (~754K tokens on 1M window), this inflated safeOutputBudget to 1, sending max_tokens: 1 to the API — models generated exactly 1 token then stopped. Fix: - Replace words * 1.15 with charEstimate-based heuristic using OpenAI standard 1 token ~ 4 characters + 10% code buffer + CJK - Add MIN_OUTPUT_BUDGET = 4096 safety net to prevent collapse Closes #83
Closed
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.
Description
The word-count-based heuristic (
words * 1.15) inestimateTokenCount()dramatically overestimates token counts for JSON-serialized chat messages (3-5× actual), because every structural JSON character ({,},",:,,) is counted as a separate word.For conversations approaching the context window (~754K tokens on a 1M window), this inflated
safeOutputBudgetto 1, sendingmax_tokens: 1to the API — models generated exactly 1 token then stopped withfinishReason: length.Fix
estimateTokenCount()heuristic — replacedwords * 1.15with charEstimate-based: 1 token ≈ 4 characters (OpenAI standard) + 10% code buffer + CJK characters. Seedocs/issues/37-20260724-estimate-token-count-overflow.md.MIN_OUTPUT_BUDGET = 4096— safety net inmodelLimits()to preventsafeOutputBudgetcollapse under any estimation scenario.Verification
All 4 test scenarios pass:
npm run compile: ✅ no errorsExtension v0.4.3 installed locally: ✅
Related
Closes #83