MCPcopy Create free account
hub / github.com/claude-code-best/claude-code / shouldRetry

Function shouldRetry

src/services/api/withRetry.ts:693–784  ·  view source on GitHub ↗
(error: APIError)

Source from the content-addressed store, hash-verified

691}
692
693function shouldRetry(error: APIError): boolean {
694 // Never retry mock errors - they're from /mock-limits command for testing
695 if (isMockRateLimitError(error)) {
696 return false
697 }
698
699 // Persistent mode: 429/529 always retryable, bypass subscriber gates and
700 // x-should-retry header.
701 if (isPersistentRetryEnabled() && isTransientCapacityError(error)) {
702 return true
703 }
704
705 // CCR mode: auth is via infrastructure-provided JWTs, so a 401/403 is a
706 // transient blip (auth service flap, network hiccup) rather than bad
707 // credentials. Bypass x-should-retry:false — the server assumes we'd retry
708 // the same bad key, but our key is fine.
709 if (
710 isEnvTruthy(process.env.CLAUDE_CODE_REMOTE) &&
711 (error.status === 401 || error.status === 403)
712 ) {
713 return true
714 }
715
716 // Check for overloaded errors first by examining the message content
717 // The SDK sometimes fails to properly pass the 529 status code during streaming,
718 // so we need to check the error message directly
719 if (error.message?.includes('"type":"overloaded_error"')) {
720 return true
721 }
722
723 // Check for max tokens context overflow errors that we can handle
724 if (parseMaxTokensContextOverflowError(error)) {
725 return true
726 }
727
728 // Note this is not a standard header.
729 const shouldRetryHeader = error.headers?.get('x-should-retry')
730
731 // If the server explicitly says whether or not to retry, obey.
732 // For Max and Pro users, should-retry is true, but in several hours, so we shouldn't.
733 // Enterprise users can retry because they typically use PAYG instead of rate limits.
734 if (
735 shouldRetryHeader === 'true' &&
736 (!isClaudeAISubscriber() || isEnterpriseSubscriber())
737 ) {
738 return true
739 }
740
741 // Ants can ignore x-should-retry: false for 5xx server errors only.
742 // For other status codes (401, 403, 400, 429, etc.), respect the header.
743 if (shouldRetryHeader === 'false') {
744 const is5xxError = error.status !== undefined && error.status >= 500
745 if (!(process.env.USER_TYPE === 'ant' && is5xxError)) {
746 return false
747 }
748 }
749
750 if (error instanceof APIConnectionError) {

Callers 1

withRetryFunction · 0.85

Calls 10

isMockRateLimitErrorFunction · 0.85
isPersistentRetryEnabledFunction · 0.85
isTransientCapacityErrorFunction · 0.85
isEnterpriseSubscriberFunction · 0.85
clearApiKeyHelperCacheFunction · 0.85
isOAuthTokenRevokedErrorFunction · 0.85
getMethod · 0.65
isEnvTruthyFunction · 0.50
isClaudeAISubscriberFunction · 0.50

Tested by

no test coverage detected