DocsRuntimeinfrastructureSlidingWindowRateLimiter
Package: @hexos/runtime

In-memory sliding window rate limiter that tracks request counts per key over a time window.

Each key (typically a user ID or conversation ID) maintains its own request timestamp array. On each consume() call, timestamps outside the window are pruned and the request is either allowed (if under the limit) or rejected (with a retryAfterMs hint).

Used by AgentRuntime to enforce per-user and per-conversation request limits as configured by RateLimitConfig. The scope (user, conversation, or both) determines the key used for tracking.

class SlidingWindowRateLimiter {
    constructor(windowMs: number, maxRequests: number)
    consume(key: string, now:  = Date.now()) => RateLimitResult;
}

constructor

(windowMs: number, maxRequests: number) => SlidingWindowRateLimiter

consume

(key: string, now: = Date.now()) => RateLimitResult

Attempts to consume a rate limit token for the given key.

Prunes expired timestamps, checks the remaining quota, and either records the request (if allowed) or returns retry information (if the limit is exceeded).