(error: APIError)
| 737 | } |
| 738 | |
| 739 | function shouldRetry(error: APIError): boolean { |
| 740 | // Never retry mock errors - they're from /mock-limits command for testing |
| 741 | if (isMockRateLimitError(error)) { |
| 742 | return false |
| 743 | } |
| 744 | |
| 745 | // Persistent mode: 429/529 always retryable, bypass subscriber gates and |
| 746 | // x-should-retry header. |
| 747 | if (isPersistentRetryEnabled() && isTransientCapacityError(error)) { |
| 748 | return true |
| 749 | } |
| 750 | |
| 751 | // CCR mode: auth is via infrastructure-provided JWTs, so a 401/403 is a |
| 752 | // transient blip (auth service flap, network hiccup) rather than bad |
| 753 | // credentials. Bypass x-should-retry:false — the server assumes we'd retry |
| 754 | // the same bad key, but our key is fine. |
| 755 | if ( |
| 756 | isEnvTruthy(process.env.CLAUDE_CODE_REMOTE) && |
| 757 | (error.status === 401 || error.status === 403) |
| 758 | ) { |
| 759 | return true |
| 760 | } |
| 761 | |
| 762 | // Check for overloaded errors first by examining the message content |
| 763 | // The SDK sometimes fails to properly pass the 529 status code during streaming, |
| 764 | // so we need to check the error message directly |
| 765 | if (error.message?.includes('"type":"overloaded_error"')) { |
| 766 | return true |
| 767 | } |
| 768 | |
| 769 | // Check for max tokens context overflow errors that we can handle |
| 770 | if (parseMaxTokensContextOverflowError(error)) { |
| 771 | return true |
| 772 | } |
| 773 | |
| 774 | // Note this is not a standard header. |
| 775 | const shouldRetryHeader = error.headers?.get('x-should-retry') |
| 776 | |
| 777 | // If the server explicitly says whether or not to retry, obey. |
| 778 | // For oauth-backed first-party sessions, should-retry:true often means "wait |
| 779 | // for the plan window reset", not "retry immediately". Enterprise sessions |
| 780 | // are the exception because they typically use PAYG instead of rate limits. |
| 781 | if ( |
| 782 | shouldRetryHeader === 'true' && |
| 783 | (!isCurrentOauthBackedFirstPartySession() || |
| 784 | isCurrentEnterpriseOauthSession()) |
| 785 | ) { |
| 786 | return true |
| 787 | } |
| 788 | |
| 789 | // Ants can ignore x-should-retry: false for 5xx server errors only. |
| 790 | // For other status codes (401, 403, 400, 429, etc.), respect the header. |
| 791 | if (shouldRetryHeader === 'false') { |
| 792 | const is5xxError = error.status !== undefined && error.status >= 500 |
| 793 | if (!(isInternalBuild() && is5xxError)) { |
| 794 | return false |
| 795 | } |
| 796 | } |
no test coverage detected