Repository navigation
fix(core): estimate Hangul and CJK text by script - #61204
NA-DEGEN-GIRL wants to merge 1 commit into
Conversation
Token.estimate divides the length by four, but a Hangul syllable or CJK character is one UTF-16 unit and costs about 0.6-0.8 tokens, so Korean, Chinese and Japanese text was estimated at roughly 30-40% of its size. Compaction uses the estimate for the unsent tail, for histories without a provider count, and for the output limit, so CJK sessions could compact late and overflow. Count Hangul at 0.65 and other CJK at 0.6 tokens per character; other text keeps length / 4. Co-Authored-By: Claude Opus 5.5 <[email protected]>
There was a problem hiding this comment.
Looks good. On the base, Token.estimate gave about a third of the real token count for Korean and Chinese text; with this change the counts are in the expected range. The new token tests fail on the base and pass here. The compaction and session runner tests and tsc also pass.
The public API is unchanged: Token.estimate is internal to packages/core and only compaction uses it.
I have no blocking concerns. The two extra passes over the text cost a few milliseconds even on multi-megabyte strings.
Related: #50574
|
Ran this against o200k_base on a set of texts I had already counted, using the regexes and rounding from this PR. Estimate divided by the real count, 1.00 is exact:
CJK lands where the comment says it should, a bit under. The part I'd question is "as length / 4 is for Latin text": it is about 25% over on English prose but about half on JSON and CSV, and 0.39 on ID-like strings, because those are mostly punctuation, digits and short pieces. Tool results are often JSON, and the text since the last provider count is what gets estimated, so a big JSON tool result could be off by far more than the 15% reserve, CJK or not. Around 2.1 characters per token is where JSON and CSV sit on o200k, if a cheap rule for that is wanted later. These are OpenAI o200k numbers only; Claude and Gemini tokenize differently. The Japanese and Korean samples are tiny. Samples and script, in case they are useful: https://github.com/aicostbudget/tokenizer-ratios |
|
Automated PR Cleanup Thank you for contributing to opencode. Due to the high volume of PRs from users and AI agents, we periodically close older PRs using automated criteria so maintainers can focus review time on the most active and community-supported contributions. This PR was closed because it matched the following cleanup criteria:
PRs created within the last month are not affected by this cleanup. If you believe this PR was closed incorrectly, or if you are still actively working on it, please leave a comment explaining why it should be reopened. A maintainer can review and reopen it if appropriate. Thanks again for taking the time to contribute. |
Issue for this PR
Closes #61202
Type of change
What does this PR do?
Token.estimatenow counts Hangul at 0.65 and other CJK characters at 0.6 tokens each; everything else keepslength / 4. Korean, Chinese and Japanese text was estimated at about a third of its size, so compaction could start too late.How did you verify your code works?
Added cases to
test/util/token.test.ts(they fail before the change). Ran the compaction and session runner tests, typecheck and lint.Checklist
Added by the opencode bot (AI-generated).
Related: #50574