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

Function calculateTokenWarningState

src/services/compact/autoCompact.ts:101–153  ·  view source on GitHub ↗
(
  tokenUsage: number,
  model: string,
)

Source from the content-addressed store, hash-verified

99}
100
101export function calculateTokenWarningState(
102 tokenUsage: number,
103 model: string,
104): {
105 percentLeft: number
106 isAboveWarningThreshold: boolean
107 isAboveErrorThreshold: boolean
108 isAboveAutoCompactThreshold: boolean
109 isAtBlockingLimit: boolean
110} {
111 const autoCompactThreshold = getAutoCompactThreshold(model)
112 const threshold = isAutoCompactEnabled()
113 ? autoCompactThreshold
114 : getEffectiveContextWindowSize(model)
115
116 const percentLeft = Math.max(
117 0,
118 Math.round(((threshold - tokenUsage) / threshold) * 100),
119 )
120
121 const warningThreshold = threshold - WARNING_THRESHOLD_BUFFER_TOKENS
122 const errorThreshold = threshold - ERROR_THRESHOLD_BUFFER_TOKENS
123
124 const isAboveWarningThreshold = tokenUsage >= warningThreshold
125 const isAboveErrorThreshold = tokenUsage >= errorThreshold
126
127 const isAboveAutoCompactThreshold =
128 isAutoCompactEnabled() && tokenUsage >= autoCompactThreshold
129
130 const actualContextWindow = getEffectiveContextWindowSize(model)
131 const defaultBlockingLimit =
132 actualContextWindow - MANUAL_COMPACT_BUFFER_TOKENS
133
134 // Allow override for testing
135 const blockingLimitOverride = process.env.CLAUDE_CODE_BLOCKING_LIMIT_OVERRIDE
136 const parsedOverride = blockingLimitOverride
137 ? parseInt(blockingLimitOverride, 10)
138 : NaN
139 const blockingLimit =
140 !isNaN(parsedOverride) && parsedOverride > 0
141 ? parsedOverride
142 : defaultBlockingLimit
143
144 const isAtBlockingLimit = tokenUsage >= blockingLimit
145
146 return {
147 percentLeft,
148 isAboveWarningThreshold,
149 isAboveErrorThreshold,
150 isAboveAutoCompactThreshold,
151 isAtBlockingLimit,
152 }
153}
154
155export function isAutoCompactEnabled(): boolean {
156 if (isEnvTruthy(process.env.DISABLE_COMPACT)) {

Callers 5

queryLoopFunction · 0.85
TokenWarningFunction · 0.85
NotificationsFunction · 0.85
shouldAutoCompactFunction · 0.85

Calls 4

getAutoCompactThresholdFunction · 0.85
isAutoCompactEnabledFunction · 0.85
maxMethod · 0.80

Tested by

no test coverage detected