@@ -308,7 +308,10 @@ export abstract class APIClient {
308308 return null ;
309309 }
310310
311- buildRequest < Req > ( options : FinalRequestOptions < Req > ) : { req : RequestInit ; url : string ; timeout : number } {
311+ buildRequest < Req > (
312+ options : FinalRequestOptions < Req > ,
313+ { retryCount = 0 } : { retryCount ?: number } = { } ,
314+ ) : { req : RequestInit ; url : string ; timeout : number } {
312315 const { method, path, query, headers : headers = { } } = options ;
313316
314317 const body =
@@ -340,7 +343,7 @@ export abstract class APIClient {
340343 headers [ this . idempotencyHeader ] = options . idempotencyKey ;
341344 }
342345
343- const reqHeaders = this . buildHeaders ( { options, headers, contentLength } ) ;
346+ const reqHeaders = this . buildHeaders ( { options, headers, contentLength, retryCount } ) ;
344347
345348 const req : RequestInit = {
346349 method,
@@ -359,10 +362,12 @@ export abstract class APIClient {
359362 options,
360363 headers,
361364 contentLength,
365+ retryCount,
362366 } : {
363367 options : FinalRequestOptions ;
364368 headers : Record < string , string | null | undefined > ;
365369 contentLength : string | null | undefined ;
370+ retryCount : number ;
366371 } ) : Record < string , string > {
367372 const reqHeaders : Record < string , string > = { } ;
368373 if ( contentLength ) {
@@ -378,6 +383,8 @@ export abstract class APIClient {
378383 delete reqHeaders [ 'content-type' ] ;
379384 }
380385
386+ reqHeaders [ 'x-stainless-retry-count' ] = String ( retryCount ) ;
387+
381388 this . validateHeaders ( reqHeaders , headers ) ;
382389
383390 return reqHeaders ;
@@ -429,13 +436,14 @@ export abstract class APIClient {
429436 retriesRemaining : number | null ,
430437 ) : Promise < APIResponseProps > {
431438 const options = await optionsInput ;
439+ const maxRetries = options . maxRetries ?? this . maxRetries ;
432440 if ( retriesRemaining == null ) {
433- retriesRemaining = options . maxRetries ?? this . maxRetries ;
441+ retriesRemaining = maxRetries ;
434442 }
435443
436444 await this . prepareOptions ( options ) ;
437445
438- const { req, url, timeout } = this . buildRequest ( options ) ;
446+ const { req, url, timeout } = this . buildRequest ( options , { retryCount : maxRetries - retriesRemaining } ) ;
439447
440448 await this . prepareRequest ( req , { url, options } ) ;
441449
0 commit comments