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

Function estimateTokensForMessage

packages/agent-core/src/utils/tokens.ts:57–73  ·  view source on GitHub ↗
(message: TokenEstimatableMessage)

Source from the content-addressed store, hash-verified

55}
56
57export function estimateTokensForMessage(message: TokenEstimatableMessage): number {
58 const cached = messageTokenEstimateCache.get(message);
59 if (cached !== undefined) {
60 return cached;
61 }
62
63 let total = estimateTokens(message.role);
64 total += estimateTokensForContentParts(message.content);
65 if (message.toolCalls !== undefined) {
66 for (const call of message.toolCalls) {
67 total += estimateTokens(call.name);
68 total += estimateTokens(JSON.stringify(call.arguments));
69 }
70 }
71 messageTokenEstimateCache.set(message, total);
72 return total;
73}
74
75export function estimateTokensForContentParts(parts: readonly ContentPart[]): number {
76 let total = 0;

Callers 6

tokens.test.tsFile · 0.90
handoff.test.tsFile · 0.90
selectRecentUserMessagesFunction · 0.90

Calls 4

estimateTokensFunction · 0.70
getMethod · 0.65
setMethod · 0.65

Tested by

no test coverage detected