MCPcopy Create free account
hub / github.com/Noumena-Network/code / countBuiltInToolTokens

Function countBuiltInToolTokens

src/utils/analyzeContext.ts:364–516  ·  view source on GitHub ↗
(
  tools: Tools,
  getToolPermissionContext: () => Promise<ToolPermissionContext>,
  agentInfo: AgentDefinitionsResult | null,
  model?: string,
  messages?: Message[],
)

Source from the content-addressed store, hash-verified

362}
363
364async function countBuiltInToolTokens(
365 tools: Tools,
366 getToolPermissionContext: () => Promise<ToolPermissionContext>,
367 agentInfo: AgentDefinitionsResult | null,
368 model?: string,
369 messages?: Message[],
370): Promise<{
371 builtInToolTokens: number
372 deferredBuiltinDetails: DeferredBuiltinTool[]
373 deferredBuiltinTokens: number
374 systemToolDetails: SystemToolDetail[]
375}> {
376 const builtInTools = tools.filter(tool => !tool.isMcp)
377 if (builtInTools.length < 1) {
378 return {
379 builtInToolTokens: 0,
380 deferredBuiltinDetails: [],
381 deferredBuiltinTokens: 0,
382 systemToolDetails: [],
383 }
384 }
385
386 // Check if tool search is enabled
387 const { isToolSearchEnabled } = await import('./toolSearch.js')
388 const { isDeferredTool } = await import('../tools/ToolSearchTool/prompt.js')
389 const isDeferred = await isToolSearchEnabled(
390 model ?? '',
391 tools,
392 getToolPermissionContext,
393 agentInfo?.activeAgents ?? [],
394 'analyzeBuiltIn',
395 )
396
397 // Separate always-loaded and deferred builtin tools using dynamic isDeferredTool check
398 const alwaysLoadedTools = builtInTools.filter(t => !isDeferredTool(t))
399 const deferredBuiltinTools = builtInTools.filter(t => isDeferredTool(t))
400
401 // Count always-loaded tools
402 const alwaysLoadedTokens =
403 alwaysLoadedTools.length > 0
404 ? await countToolDefinitionTokens(
405 alwaysLoadedTools,
406 getToolPermissionContext,
407 agentInfo,
408 model,
409 )
410 : 0
411
412 // Build per-tool breakdown for always-loaded tools (ant-only, proportional
413 // split of the bulk count based on rough schema size estimation). Excludes
414 // SkillTool since its tokens are shown in the separate Skills category.
415 let systemToolDetails: SystemToolDetail[] = []
416 if (isInternalBuild()) {
417 const toolsForBreakdown = alwaysLoadedTools.filter(
418 t => !toolMatchesName(t, SKILL_TOOL_NAME),
419 )
420 if (toolsForBreakdown.length > 0) {
421 const estimates = toolsForBreakdown.map(t =>

Callers 1

analyzeContextUsageFunction · 0.85

Calls 11

isToolSearchEnabledFunction · 0.85
isDeferredToolFunction · 0.85
isInternalBuildFunction · 0.85
toolMatchesNameFunction · 0.85
jsonStringifyFunction · 0.85
maxMethod · 0.80
entriesMethod · 0.80
hasMethod · 0.45
addMethod · 0.45

Tested by

no test coverage detected