@@ -37,6 +37,19 @@ const DEFAULT_RETRY_CONFIG = {
3737
3838/**
3939 * Check if an error is retryable for LLM calls.
40+ *
41+ * @remarks
42+ * This function checks the error's HTTP status code, error code, and message to determine
43+ * whether it should be retried. The logic is:
44+ * - Do not retry on client errors (4xx) except 429 (rate limit)
45+ * - Do not retry on billing errors (402)
46+ * - Retry on rate limit (429)
47+ * - Retry on server errors (5xx)
48+ * - Retry on network errors (ETIMEDOUT, ESOCKETTIMEDOUT, ECONNRESET, ECONNABORTED)
49+ * - Retry on timeout errors (only when not a 4xx status code)
50+ *
51+ * @param err - The error to check, which may be an object with status, statusCode, code, or message properties.
52+ * @returns `true` if the error is retryable, `false` otherwise.
4053 */
4154export function isRetryableLlmError ( err : unknown ) : boolean {
4255 if ( ! err ) {
@@ -47,35 +60,41 @@ export function isRetryableLlmError(err: unknown): boolean {
4760 const status = errorObj . status ?? errorObj . statusCode ;
4861 const message = errorObj . message ?? "" ;
4962
50- // Retry on rate limit (429)
51- if ( status === 429 ) {
52- return true ;
63+ // Check status codes first (more reliable than message parsing)
64+ // Do not retry on client errors (400, 401, 403, 404, 408)
65+ if ( status === 400 || status === 401 || status === 403 || status === 404 || status === 408 ) {
66+ return false ;
5367 }
5468
55- // Retry on server errors (500, 502, 503, 504 )
56- if ( status === 500 || status === 502 || status === 503 || status === 504 ) {
57- return true ;
69+ // Do not retry on billing errors (402 )
70+ if ( status === 402 ) {
71+ return false ;
5872 }
5973
60- // Retry on network errors
61- const code = ( errorObj . code ?? "" ) . toUpperCase ( ) ;
62- if ( [ "ETIMEDOUT" , "ESOCKETTIMEDOUT" , "ECONNRESET" , "ECONNABORTED" ] . includes ( code ) ) {
74+ // Retry on rate limit (429)
75+ if ( status === 429 ) {
6376 return true ;
6477 }
6578
66- // Retry on timeout errors
67- if ( / t i m e o u t | t i m e d o u t | d e a d l i n e e x c e e d e d | d e a d l i n e _ e x c e e d e d / i . test ( message ) ) {
79+ // Retry on server errors (500, 502, 503, 504)
80+ if ( status === 500 || status === 502 || status === 503 || status === 504 ) {
6881 return true ;
6982 }
7083
71- // Do not retry on client errors (400, 401, 403, 404)
72- if ( status === 400 || status === 401 || status === 403 || status === 404 ) {
73- return false ;
74- }
84+ // Only check timeout message if there's no 4xx status code
85+ // (timeout messages in 4xx responses indicate client-side timeouts that shouldn't be retried)
86+ const is4xx = status !== undefined && status >= 400 && status < 500 ;
87+ if ( ! is4xx ) {
88+ // Retry on timeout errors (only for non-4xx status codes)
89+ if ( / t i m e o u t | t i m e d o u t | d e a d l i n e e x c e e d e d | d e a d l i n e _ e x c e e d e d / i. test ( message ) ) {
90+ return true ;
91+ }
7592
76- // Do not retry on billing errors (402)
77- if ( status === 402 ) {
78- return false ;
93+ // Retry on network errors
94+ const code = ( errorObj . code ?? "" ) . toUpperCase ( ) ;
95+ if ( [ "ETIMEDOUT" , "ESOCKETTIMEDOUT" , "ECONNRESET" , "ECONNABORTED" ] . includes ( code ) ) {
96+ return true ;
97+ }
7998 }
8099
81100 // Default: do not retry unknown errors
@@ -84,6 +103,16 @@ export function isRetryableLlmError(err: unknown): boolean {
84103
85104/**
86105 * Extract retry-after delay from error response headers or body.
106+ *
107+ * @remarks
108+ * This function checks the error's headers and message for retry delay hints:
109+ * - `Retry-After` header (RFC 7231, in seconds)
110+ * - `x-ratelimit-reset` header (Unix timestamp, converted to relative ms)
111+ * - `x-ratelimit-reset-after` header (seconds)
112+ * - Message patterns like "retry in 5s" or "reset after 100ms"
113+ *
114+ * @param err - The error to check, which may have headers and/or message properties.
115+ * @returns The retry delay in milliseconds, or `undefined` if no delay is found.
87116 */
88117export function extractRetryAfterMs ( err : unknown ) : number | undefined {
89118 if ( ! err || typeof err !== "object" ) {
@@ -127,7 +156,7 @@ export function extractRetryAfterMs(err: unknown): number | undefined {
127156 // Check error message for retry delay hints
128157 const message = errorObj . message ?? "" ;
129158 const retryInMatch = message . match (
130- / r e t r y \s + i n \s + ( [ 0 - 9 . ] + ) \s * ( m s | m i l l i s e c o n d s ? | s e c o n d s ? | s e c s ? | s ) / i,
159+ / r e t r y \s + i n \s + ( [ 0 - 9 . ] + ) \s * ( m s | m i l l i s e c o n d s ? | s e c o n d s ? | s e c s ? | s ) ? / i,
131160 ) ;
132161 if ( retryInMatch ?. [ 1 ] ) {
133162 const value = parseFloat ( retryInMatch [ 1 ] ) ;
@@ -138,7 +167,7 @@ export function extractRetryAfterMs(err: unknown): number | undefined {
138167 }
139168
140169 const resetAfterMatch = message . match (
141- / r e s e t \s + a f t e r \s + ( [ 0 - 9 . ] + ) \s * ( m s | m i l l i s e c o n d s ? | s e c o n d s ? | s e c s ? | s ) / i,
170+ / r e s e t \s + a f t e r \s + ( [ 0 - 9 . ] + ) \s * ( m s | m i l l i s e c o n d s ? | s e c o n d s ? | s e c s ? | s ) ? / i,
142171 ) ;
143172 if ( resetAfterMatch ?. [ 1 ] ) {
144173 const value = parseFloat ( resetAfterMatch [ 1 ] ) ;
0 commit comments