Repository navigation
fix: use a realistic fallback divisor for collector token estimates - #6672
Merged
timothycarambat merged 2 commits intoOct 9, 2026
Conversation
shatfield4
approved these changes
Oct 8, 2026
shatfield4
left a comment
Collaborator
There was a problem hiding this comment.
Text above 5120 chars gets an estimated token count of length / DIVISOR. Real text averages about 4 chars per token, so /8 under-counted long docs by about half, and they passed the context window check when they shouldn't have. /4 is close to real counts and only overestimates, which is the safe direction. I removed the test since it only held for its sample text.
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.
Pull Request Type
Relevant Issues
resolves #6668
Description
TikTokenTokenizer.DIVISORincollector/utils/tokenizer/index.jsgoes from 8 to 4. The "too long" guard is untouched (it still protects the CPU); only the fallback estimate it returns changes.With the old divisor of 8,
token_count_estimatedropped sharply at exactly 5120 chars (e.g. 1003 -> 640 tokens for English prose), so the estimate was not monotonic and under-counted roughly 2x for typical English. With 4 the same input gives 1280, close to the real count and no longer a cliff.Tests: added
collector/__tests__/utils/tokenizer/index.test.js:ceil(length / 4)once the guard tripsTwo of these fail on the old divisor of 8. The full collector suite passes (14 suites, 410 tests);
eslintandprettier --checkare clean on the changed files.Additional Information
CJK / JSON / whitespace-heavy text above the guard is still under-counted by a fixed divisor; that is out of scope here (related only: #6142).
Developer Validations
yarn lintfrom the root of the repo & committed changes