MCPcopy Create free account
hub / github.com/ChatLab/ChatLab / countTokens

Function countTokens

packages/node-runtime/src/ai/tokenizer.ts:67–73  ·  view source on GitHub ↗
(text: string)

Source from the content-addressed store, hash-verified

65 * encoder 已初始化时使用精确计数,否则降级到字符估算。
66 */
67export function countTokens(text: string): number {
68 if (!text) return 0
69 if (encoder) {
70 return encoder.encode(text).length
71 }
72 return estimateTokens(text)
73}
74
75/**
76 * 计算消息列表的总 token 数(含 systemPrompt)。

Callers 3

forceTruncateFunction · 0.90
countMessagesTokensFunction · 0.85

Calls 1

estimateTokensFunction · 0.70

Tested by

no test coverage detected