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

Function estimateTokens

packages/agent-core/src/utils/tokens.ts:26–37  ·  view source on GitHub ↗
(text: string)

Source from the content-addressed store, hash-verified

24 * monotonic between LLM round-trips without paying for a tokenizer.
25 */
26export function estimateTokens(text: string): number {
27 let asciiCount = 0;
28 let nonAsciiCount = 0;
29 for (const char of text) {
30 if (char.codePointAt(0)! <= 127) {
31 asciiCount++;
32 } else {
33 nonAsciiCount++;
34 }
35 }
36 return Math.ceil(asciiCount / 4) + nonAsciiCount;
37}
38
39export function estimateTokensForMessages(messages: readonly Message[]): number {
40 let total = 0;

Callers 7

handoff.test.tsFile · 0.90
full.test.tsFile · 0.90
applyCompactionMethod · 0.90
estimateRequestTokensMethod · 0.90
estimateTokensForToolsFunction · 0.70
estimateTokensForMessageFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected