* Check if time-relative early warning should be triggered for a rate limit type. * Fallback when server doesn't send surpassed-threshold header. * Returns ClaudeAILimits if thresholds are exceeded, null otherwise.
( headers: globalThis.Headers, config: EarlyWarningConfig, unifiedRateLimitFallbackAvailable: boolean, )
| 299 | * Returns ClaudeAILimits if thresholds are exceeded, null otherwise. |
| 300 | */ |
| 301 | function getTimeRelativeEarlyWarning( |
| 302 | headers: globalThis.Headers, |
| 303 | config: EarlyWarningConfig, |
| 304 | unifiedRateLimitFallbackAvailable: boolean, |
| 305 | ): ClaudeAILimits | null { |
| 306 | const { rateLimitType, claimAbbrev, windowSeconds, thresholds } = config |
| 307 | |
| 308 | const utilizationHeader = headers.get( |
| 309 | `anthropic-ratelimit-unified-${claimAbbrev}-utilization`, |
| 310 | ) |
| 311 | const resetHeader = headers.get( |
| 312 | `anthropic-ratelimit-unified-${claimAbbrev}-reset`, |
| 313 | ) |
| 314 | |
| 315 | if (utilizationHeader === null || resetHeader === null) { |
| 316 | return null |
| 317 | } |
| 318 | |
| 319 | const utilization = Number(utilizationHeader) |
| 320 | const resetsAt = Number(resetHeader) |
| 321 | const timeProgress = computeTimeProgress(resetsAt, windowSeconds) |
| 322 | |
| 323 | // Check if any threshold is exceeded: high usage early in the window |
| 324 | const shouldWarn = thresholds.some( |
| 325 | t => utilization >= t.utilization && timeProgress <= t.timePct, |
| 326 | ) |
| 327 | |
| 328 | if (!shouldWarn) { |
| 329 | return null |
| 330 | } |
| 331 | |
| 332 | return { |
| 333 | status: 'allowed_warning', |
| 334 | resetsAt, |
| 335 | rateLimitType, |
| 336 | utilization, |
| 337 | unifiedRateLimitFallbackAvailable, |
| 338 | isUsingOverage: false, |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | /** |
| 343 | * Get early warning limits using header-based detection with time-relative fallback. |
no test coverage detected