fix: CJK-aware token estimation in estimateTokens#47
Open
lishixiang0705 wants to merge 1 commit intoMartian-Engineering:mainfrom
Open
fix: CJK-aware token estimation in estimateTokens#47lishixiang0705 wants to merge 1 commit intoMartian-Engineering:mainfrom
lishixiang0705 wants to merge 1 commit intoMartian-Engineering:mainfrom
Conversation
The previous implementation (Math.ceil(text.length / 4)) assumes ~4 chars per token, which is accurate for English/ASCII text but severely underestimates token counts for CJK (Chinese/Japanese/Korean) content. In JavaScript, string.length returns UTF-16 code units, where each CJK character counts as 1. However, most LLM tokenizers (Claude, GPT, etc.) encode CJK characters at ~1.5 tokens per character, not 0.25. This causes a ~3x underestimation for Chinese-heavy conversations: - A message with 40% CJK + 53% ASCII (28,949 chars): - Old estimate: 7,238 tokens - Corrected estimate: 22,180 tokens (3.1x higher) - In production, LCM estimated context at 59k when actual API usage was 174k, causing compaction to trigger far too late and sessions to hit the 200k hard limit. The fix applies per-character weighting: - CJK Unified Ideographs (U+4E00–U+9FFF) and Extension A (U+3400–U+4DBF): 1.5 tokens/char - All other characters: 0.25 tokens/char (unchanged from before)
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.
Problem
The current
estimateTokensfunction usesMath.ceil(text.length / 4), which assumes ~4 characters per token. This is accurate for English/ASCII but severely underestimates CJK (Chinese/Japanese/Korean) content by ~3x.Root cause
In JavaScript,
string.lengthreturns UTF-16 code units where each CJK character = 1 unit. However, LLM tokenizers (Claude, GPT, etc.) encode CJK characters at ~1.5 tokens per character, not 0.25.Production impact
In a Chinese-heavy OpenClaw session:
Verified with real data — a message with 40% CJK + 53% ASCII (28,949 chars):
length/4)Fix
Apply per-character weighting in both
assembler.tsandcompaction.ts:Zero dependencies added. Backwards compatible — only affects estimation accuracy.