MCPcopy
hub / github.com/codeaashu/claude-code / countSystemTokens

Function countSystemTokens

src/utils/analyzeContext.ts:272–318  ·  view source on GitHub ↗
(
  effectiveSystemPrompt: readonly string[],
)

Source from the content-addressed store, hash-verified

270}
271
272async function countSystemTokens(
273 effectiveSystemPrompt: readonly string[],
274): Promise<{
275 systemPromptTokens: number
276 systemPromptSections: SystemPromptSectionDetail[]
277}> {
278 // Get system context (gitStatus, etc.) which is always included
279 const systemContext = await getSystemContext()
280
281 // Build named entries: system prompt parts + system context values
282 // Skip empty strings and the global-cache boundary marker
283 const namedEntries: Array<{ name: string; content: string }> = [
284 ...effectiveSystemPrompt
285 .filter(
286 content =>
287 content.length > 0 && content !== SYSTEM_PROMPT_DYNAMIC_BOUNDARY,
288 )
289 .map(content => ({ name: extractSectionName(content), content })),
290 ...Object.entries(systemContext)
291 .filter(([, content]) => content.length > 0)
292 .map(([name, content]) => ({ name, content })),
293 ]
294
295 if (namedEntries.length < 1) {
296 return { systemPromptTokens: 0, systemPromptSections: [] }
297 }
298
299 const systemTokenCounts = await Promise.all(
300 namedEntries.map(({ content }) =>
301 countTokensWithFallback([{ role: 'user', content }], []),
302 ),
303 )
304
305 const systemPromptSections: SystemPromptSectionDetail[] = namedEntries.map(
306 (entry, i) => ({
307 name: entry.name,
308 tokens: systemTokenCounts[i] || 0,
309 }),
310 )
311
312 const systemPromptTokens = systemTokenCounts.reduce(
313 (sum: number, tokens) => sum + (tokens || 0),
314 0,
315 )
316
317 return { systemPromptTokens, systemPromptSections }
318}
319
320async function countMemoryFileTokens(): Promise<{
321 memoryFileDetails: MemoryFile[]

Callers 1

analyzeContextUsageFunction · 0.85

Calls 3

extractSectionNameFunction · 0.85
countTokensWithFallbackFunction · 0.85
entriesMethod · 0.80

Tested by

no test coverage detected