Api rate limiting

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

Api rate limiting is an AI agent skill from the GitHub repository aj-geddes/useful-ai-prompts. Implement API rate limiting strategies using token bucket, sliding window, and fixed window algorithms. Use when protecting APIs from abuse, managing traffic, or implementing tiered rate limits.

How to use this skill

Skill file

API Rate Limiting

Table of Contents

Overview

Protect APIs from abuse and manage traffic using various rate limiting algorithms with per-user, per-IP, and per-endpoint strategies.

When to Use

Quick Start

Minimal working example:

// Token Bucket Rate Limiter
class TokenBucket {
  constructor(capacity, refillRate) {
    this.capacity = capacity;
    this.tokens = capacity;
    this.refillRate = refillRate; // tokens per second
    this.lastRefillTime = Date.now();
  }

  refill() {
    const now = Date.now();
    const timePassed = (now - this.lastRefillTime) / 1000;
    const tokensToAdd = timePassed * this.refillRate;

    this.tokens = Math.min(this.capacity, this.tokens + tokensToAdd);
    this.lastRefillTime = now;
  }

  consume(tokens = 1) {
    this.refill();

    if (this.tokens >= tokens) {
      this.tokens -= tokens;
      return true;
    }
// ... (see reference guides for full implementation)

Reference Guides

Detailed implementations in the references/ directory:

| Guide | Contents |

|---|---|

| Token Bucket Algorithm | Token Bucket Algorithm |

| Sliding Window Algorithm | Sliding Window Algorithm |

| Redis-Based Rate Limiting | Redis-Based Rate Limiting |

| Tiered Rate Limiting | Tiered Rate Limiting |

| Python Rate Limiting (Flask) | Python Rate Limiting (Flask) |

| Response Headers | Response Headers |

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.