MCPcopy Index your code
hub / github.com/claude-code-best/claude-code / tokenCountWithEstimation

Function tokenCountWithEstimation

src/utils/tokens.ts:251–292  ·  view source on GitHub ↗
(messages: readonly Message[])

Source from the content-addressed store, hash-verified

249 * so every interleaved tool_result is included in the rough estimate.
250 */
251export function tokenCountWithEstimation(messages: readonly Message[]): number {
252 let i = messages.length - 1
253 while (i >= 0) {
254 const message = messages[i]
255 const usage = message ? getTokenUsage(message) : undefined
256 if (message && usage) {
257 // Walk back past any earlier sibling records split from the same API
258 // response (same message.id) so interleaved tool_results between them
259 // are included in the estimation slice.
260 const responseId = getAssistantMessageId(message)
261 if (responseId) {
262 let j = i - 1
263 while (j >= 0) {
264 const prior = messages[j]
265 const priorId = prior ? getAssistantMessageId(prior) : undefined
266 if (priorId === responseId) {
267 // Earlier split of the same API response — anchor here instead.
268 i = j
269 } else if (priorId !== undefined) {
270 // Hit a different API response — stop walking.
271 break
272 }
273 // priorId === undefined: a user/tool_result/attachment message,
274 // possibly interleaved between splits — keep walking.
275 j--
276 }
277 }
278 return (
279 getTokenCountFromUsage(usage) +
280 roughTokenCountEstimationForMessages(
281 messages.slice(i + 1) as Parameters<
282 typeof roughTokenCountEstimationForMessages
283 >[0],
284 )
285 )
286 }
287 i--
288 }
289 return roughTokenCountEstimationForMessages(
290 messages as Parameters<typeof roughTokenCountEstimationForMessages>[0],
291 )
292}

Callers 12

callFunction · 0.85
queryLoopFunction · 0.85
classifyYoloActionFunction · 0.85
runInProcessTeammateFunction · 0.85
compactConversationFunction · 0.85
shouldAutoCompactFunction · 0.85
shouldExtractMemoryFunction · 0.85
sessionMemory.tsFile · 0.85

Calls 4

getTokenUsageFunction · 0.85
getAssistantMessageIdFunction · 0.85
getTokenCountFromUsageFunction · 0.85

Tested by

no test coverage detected