@@ -83,10 +83,12 @@ export class LimitsPresenter extends BasePresenter {
8383 public async call ( {
8484 organizationId,
8585 projectId,
86+ environmentId,
8687 environmentApiKey,
8788 } : {
8889 organizationId : string ;
8990 projectId : string ;
91+ environmentId : string ;
9092 environmentApiKey : string ;
9193 } ) : Promise < LimitsResult > {
9294 // Get organization with all limit-related fields
@@ -159,9 +161,9 @@ export class LimitsPresenter extends BasePresenter {
159161 environmentApiKey ,
160162 apiRateLimitConfig
161163 ) ;
162- const batchRateLimitTokens = await getRateLimitRemainingTokens (
163- "batch" ,
164- environmentApiKey ,
164+ // Batch rate limiter uses environment ID directly (not hashed) with a different key prefix
165+ const batchRateLimitTokens = await getBatchRateLimitRemainingTokens (
166+ environmentId ,
165167 batchRateLimitConfig
166168 ) ;
167169
@@ -419,3 +421,36 @@ async function getRateLimitRemainingTokens(
419421 return null ;
420422 }
421423}
424+
425+ /**
426+ * Query the current remaining tokens for the batch rate limiter.
427+ * The batch rate limiter uses environment ID directly (not hashed) and has a different key prefix.
428+ */
429+ async function getBatchRateLimitRemainingTokens (
430+ environmentId : string ,
431+ config : RateLimiterConfig
432+ ) : Promise < number | null > {
433+ try {
434+ // Create a Ratelimit instance with the same configuration as the batch rate limiter
435+ const limiter = createLimiterFromConfig ( config ) ;
436+ const ratelimit = new Ratelimit ( {
437+ redis : rateLimitRedisClient ,
438+ limiter,
439+ ephemeralCache : new Map ( ) ,
440+ analytics : false ,
441+ // The batch rate limiter uses "ratelimit:batch" as keyPrefix in RateLimiter,
442+ // which adds another "ratelimit:" prefix, resulting in "ratelimit:ratelimit:batch"
443+ prefix : `ratelimit:ratelimit:batch` ,
444+ } ) ;
445+
446+ // Batch rate limiter uses environment ID directly (not hashed)
447+ const remaining = await ratelimit . getRemaining ( environmentId ) ;
448+ return remaining ;
449+ } catch ( error ) {
450+ logger . warn ( "Failed to get batch rate limit remaining tokens" , {
451+ environmentId,
452+ error : error instanceof Error ? error . message : String ( error ) ,
453+ } ) ;
454+ return null ;
455+ }
456+ }
0 commit comments