How to Build a Sliding Window Rate Limiter in TypeScript for AI Agent Tools A developer published a zero-dependency TypeScript implementation of a sliding window rate limiter for AI agent tool calls, arguing that fixed-window counters let runaway retry loops burst through limits. The class tracks per-key timestamps in an in-memory Map, prunes expired entries, and returns a retryAfterMs value so clients can back off, with guidance to move the log to Redis sorted sets when running multiple replicas. When an AI agent enters a retry loop on a failing tool call, a naive fixed window rate limiter won't save you. Suppose your limit is 50 requests per minute. A runaway loop fires 50 requests at 11:59:59 and another 50 at 12:00:01. The fixed counter resets at midnight, so 100 requests slip through inside a two-second burst. A sliding window log tracks the exact timestamp of every accepted call within the trailing window for example, the last 60,000 milliseconds . Before executing a tool call, you prune expired timestamps and count what remains. Here is a clean, zero-dependency implementation in TypeScript: export interface RateLimitResult { allowed: boolean; remaining: number; retryAfterMs: number; } export class SlidingWindowLimiter { private readonly windows = new Map