MCPcopy Create free account
hub / github.com/MoonshotAI/kimi-code / estimateTokens

Function estimateTokens

apps/vis/server/src/lib/context-projector.ts:639–650  ·  view source on GitHub ↗

Replicates agent-core's per-char token weighting exactly, over the same * `text` + `think` parts its gate counts. agent-core * (`packages/agent-core/src/utils/tokens.ts`) sums per-part estimates, each * `estimateTokens(s) = Math.ceil(asciiCount / 4) + nonAsciiCount` (ASCII ~4 * chars/token,

(text: string)

Source from the content-addressed store, hash-verified

637 * contribute 0. Matching it ensures Chinese-heavy tool results blank at the
638 * same gate as the agent. */
639function estimateTokens(text: string): number {
640 let asciiCount = 0;
641 let nonAsciiCount = 0;
642 for (const char of text) {
643 if (char.codePointAt(0)! <= 127) {
644 asciiCount++;
645 } else {
646 nonAsciiCount++;
647 }
648 }
649 return Math.ceil(asciiCount / 4) + nonAsciiCount;
650}
651
652function estimateContentTokens(content: readonly ContentPart[]): number {
653 let total = 0;

Callers 1

estimateContentTokensFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected