AiRateLimiter

class AiRateLimiter(val name: String, maxRequests: Int = DEFAULT_MAX_REQUESTS, windowMs: Long = DEFAULT_WINDOW_MS, clock: () -> Long = System::currentTimeMillis)(source)

Thread-safe sliding-window rate limiter for AI API calls.

Allows up to maxRequests calls within each windowMs period. Once the limit is reached, subsequent calls are rejected until the window resets.

Parameters

name

Human-readable label used in log messages

maxRequests

Maximum number of requests allowed per window

windowMs

Window duration in milliseconds

Constructors

Link copied to clipboard
constructor(name: String, maxRequests: Int = DEFAULT_MAX_REQUESTS, windowMs: Long = DEFAULT_WINDOW_MS, clock: () -> Long = System::currentTimeMillis)

Properties

Link copied to clipboard

Functions

Link copied to clipboard

Number of requests consumed in the current window.

Link copied to clipboard
fun remaining(): Int

Number of requests remaining in the current window.

Link copied to clipboard
fun reset()

Manually resets the window and counter.

Link copied to clipboard

Attempts to acquire a permit. Returns true if the call is allowed, false if the rate limit has been reached.