MCPcopy Index your code
hub / github.com/codeaashu/claude-code / countSkillTokens

Function countSkillTokens

src/utils/analyzeContext.ts:554–614  ·  view source on GitHub ↗
(
  tools: Tools,
  getToolPermissionContext: () => Promise<ToolPermissionContext>,
  agentInfo: AgentDefinitionsResult | null,
)

Source from the content-addressed store, hash-verified

552}
553
554async function countSkillTokens(
555 tools: Tools,
556 getToolPermissionContext: () => Promise<ToolPermissionContext>,
557 agentInfo: AgentDefinitionsResult | null,
558): Promise<{
559 skillTokens: number
560 skillInfo: {
561 totalSkills: number
562 includedSkills: number
563 skillFrontmatter: SkillFrontmatter[]
564 }
565}> {
566 try {
567 const skills = await getLimitedSkillToolCommands(getCwd())
568
569 const slashCommandTool = findSkillTool(tools)
570 if (!slashCommandTool) {
571 return {
572 skillTokens: 0,
573 skillInfo: { totalSkills: 0, includedSkills: 0, skillFrontmatter: [] },
574 }
575 }
576
577 // NOTE: This counts the entire SlashCommandTool (which includes both commands AND skills).
578 // This is the same tool counted by countSlashCommandTokens(), but we track it separately
579 // here for display purposes. These tokens should NOT be added to context categories
580 // to avoid double-counting.
581 const skillTokens = await countToolDefinitionTokens(
582 [slashCommandTool],
583 getToolPermissionContext,
584 agentInfo,
585 )
586
587 // Calculate per-skill token estimates based on frontmatter only
588 // (name, description, whenToUse) since full content is only loaded on invocation
589 const skillFrontmatter: SkillFrontmatter[] = skills.map(skill => ({
590 name: getCommandName(skill),
591 source: (skill.type === 'prompt' ? skill.source : 'plugin') as
592 | SettingSource
593 | 'plugin',
594 tokens: estimateSkillFrontmatterTokens(skill),
595 }))
596
597 return {
598 skillTokens,
599 skillInfo: {
600 totalSkills: skills.length,
601 includedSkills: skills.length,
602 skillFrontmatter,
603 },
604 }
605 } catch (error) {
606 logError(toError(error))
607
608 // Return zero values rather than failing the entire context analysis
609 return {
610 skillTokens: 0,
611 skillInfo: { totalSkills: 0, includedSkills: 0, skillFrontmatter: [] },

Callers 1

analyzeContextUsageFunction · 0.85

Calls 8

getCwdFunction · 0.85
findSkillToolFunction · 0.85
toErrorFunction · 0.85
logErrorFunction · 0.70
getCommandNameFunction · 0.50

Tested by

no test coverage detected