MCPcopy
hub / github.com/claude-code-best/claude-code / checkAutoThreshold

Function checkAutoThreshold

src/utils/searchExtraTools.ts:676–720  ·  view source on GitHub ↗

* Check whether deferred tools exceed the auto-threshold for enabling TST. * Tries exact token count first; falls back to character-based heuristic.

(
  tools: Tools,
  getToolPermissionContext: () => Promise<ToolPermissionContext>,
  agents: AgentDefinition[],
  model: string,
)

Source from the content-addressed store, hash-verified

674 * Tries exact token count first; falls back to character-based heuristic.
675 */
676async function checkAutoThreshold(
677 tools: Tools,
678 getToolPermissionContext: () => Promise<ToolPermissionContext>,
679 agents: AgentDefinition[],
680 model: string,
681): Promise<{
682 enabled: boolean
683 debugDescription: string
684 metrics: Record<string, number>
685}> {
686 // Try exact token count first (cached, one API call per toolset change)
687 const deferredToolTokens = await getDeferredToolTokenCount(
688 tools,
689 getToolPermissionContext,
690 agents,
691 model,
692 )
693
694 if (deferredToolTokens !== null) {
695 const threshold = getAutoSearchExtraToolsTokenThreshold(model)
696 return {
697 enabled: deferredToolTokens >= threshold,
698 debugDescription:
699 `${deferredToolTokens} tokens (threshold: ${threshold}, ` +
700 `${getAutoSearchExtraToolsPercentage()}% of context)`,
701 metrics: { deferredToolTokens, threshold },
702 }
703 }
704
705 // Fallback: character-based heuristic when token API is unavailable
706 const deferredToolDescriptionChars =
707 await calculateDeferredToolDescriptionChars(
708 tools,
709 getToolPermissionContext,
710 agents,
711 )
712 const charThreshold = getAutoSearchExtraToolsCharThreshold(model)
713 return {
714 enabled: deferredToolDescriptionChars >= charThreshold,
715 debugDescription:
716 `${deferredToolDescriptionChars} chars (threshold: ${charThreshold}, ` +
717 `${getAutoSearchExtraToolsPercentage()}% of context) (char fallback)`,
718 metrics: { deferredToolDescriptionChars, charThreshold },
719 }
720}

Callers 1

Tested by

no test coverage detected