Rate limiting implementation

From aj-geddes/useful-ai-prompts · View interactive version

Rate limiting implementation is an AI agent skill from the GitHub repository aj-geddes/useful-ai-prompts. Implement rate limiting, throttling, API quotas, and backpressure mechanisms to protect services from abuse and ensure fair resource usage. Use when building APIs, preventing DOS attacks, or managing system load.

How to use this skill

Skill file

Rate Limiting Implementation

Table of Contents

Overview

Implement rate limiting and throttling mechanisms to protect your services from abuse, ensure fair resource allocation, and maintain system stability under load.

When to Use

Quick Start

Minimal working example:

interface TokenBucketConfig {
  capacity: number;
  refillRate: number; // tokens per second
  refillInterval: number; // milliseconds
}

class TokenBucket {
  private tokens: number;
  private lastRefill: number;
  private readonly capacity: number;
  private readonly refillRate: number;
  private readonly refillInterval: number;
  private refillTimer?: NodeJS.Timeout;

  constructor(config: TokenBucketConfig) {
    this.capacity = config.capacity;
    this.tokens = config.capacity;
    this.refillRate = config.refillRate;
    this.refillInterval = config.refillInterval;
    this.lastRefill = Date.now();

    this.startRefill();
  }

  private startRefill(): void {
// ... (see reference guides for full implementation)

Reference Guides

Detailed implementations in the references/ directory:

| Guide | Contents |

|---|---|

| Token Bucket Algorithm (TypeScript) | Token Bucket Algorithm (TypeScript) |

| Redis-Based Distributed Rate Limiter | Redis-Based Distributed Rate Limiter |

| Express Middleware | Express Middleware |

| Sliding Window Algorithm (Python) | Sliding Window Algorithm (Python) |

| Tiered Rate Limiting | Tiered Rate Limiting |

| Adaptive Rate Limiting | Adaptive Rate Limiting |

Best Practices

✅ DO

❌ DON'T

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.