Token 用量估算

🔑 Token Usage Estimator

Estimate token usage by the mix of Chinese, English and code in your text, and see how much tokenizers disagree.

📐 计算公式 / 原理
Token 估算(本页代码口径):tokens=中文字数÷1.5+英文词数÷0.75+代码 tokens(代码部分按输入值直接计入、不折算)。校验:无强校验,空值按 0 兜底。判据:无固定分级,中文约 1.5 字折 1 token、英文约 1 词折 1.33 token,混合文本按三项加总。
混合内容按三类分别折算:中文按字符、英文按词、代码按字符(切分最碎)。这是粗略预算工具,实际值随分词器与符号密度浮动 ±20%,用于判断是否接近上下文上限时应留出安全余量。

📐 计算公式与说明

口径判读(与上式互补):中文≈字数÷1.5、英文≈词数÷0.75 为 BPE 分词经验折算率,代码片段须按实际 token 数输入;估算结果四舍五入取整展示。校验:与上方一致,空值按 0 兜底。判据:无固定分级,英文单词常拆多个 token 故折算率>1,中文单字信息密度高故折算率<1。

不同分词器差异较大,仅供参考。

📚 深度解析:Token 用量估算

💡 常见使用场景

中英混排估算
一段 500 中文字+200 英文词:中文≈500×1.5=750 token,英文≈200×1.3=260 token,合计≈1010 token。实际因模型分词不同会有偏差。

❓ 常见问题(FAQ)

为什么中文 token 数不固定?
不同模型对中文的子词切分不同(按字/按词/混合),同一段中文 token 数可差数倍;本估算仅给量级,精确需对应分词器。
代码为什么更费 token?
代码含大量符号、缩进与标识符,信息密度高、子词切分更碎,同等字符数常比自然语言消耗更多 token。

Formula & Standard References

Item Formula / Note Source / Standard
Tokenisation subword tokenisation splits text into pieces whose density differs across scripts and code Sennrich et al. 2016, BPE
Estimation rule English averages about four characters per token while Chinese is near one per character tiktoken tokeniser reference
Purpose the estimate sizes the remaining context budget and the API cost tiktoken tokeniser reference