Skip to content

fix(core): estimate Hangul and CJK text by script - #61204

Closed
NA-DEGEN-GIRL wants to merge 1 commit into
anomalyco:devfrom
NA-DEGEN-GIRL:cjk-token-estimate
Closed

NA-DEGEN-GIRL wants to merge 1 commit into
anomalyco:devfrom
NA-DEGEN-GIRL:cjk-token-estimate

Conversation

@NA-DEGEN-GIRL

@NA-DEGEN-GIRL NA-DEGEN-GIRL commented Nov 2, 2026 •

Copy link
Copy Markdown

Issue for this PR

Closes #61202

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

Token.estimate now counts Hangul at 0.65 and other CJK characters at 0.6 tokens each; everything else keeps length / 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

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

Added by the opencode bot (AI-generated).

Related: #50574

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]>

@opencodejr opencodejr Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@linqiang-max

Copy link
Copy Markdown

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:

Text Samples length / 4 this PR
Chinese 11 0.34 0.82
Japanese 4 0.34 0.80
Korean 4 0.45 0.95
English prose 416 1.25 1.25
Python source 53 1.05 1.05
JSON 20 0.53 0.53
CSV 40 0.52 0.52
API keys, UUIDs, hashes 30 0.39 0.39

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

@github-actions

github-actions Bot commented Dec 2, 2026

Copy link
Copy Markdown
Contributor

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:

  • The PR was created more than 1 month ago
  • The PR had fewer than 2 positive reactions
  • Positive reactions are counted as thumbs-up, heart, celebration, or rocket reactions on the PR

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.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Token estimate undercounts Korean/Chinese/Japanese text

2 participants