Command Palette

Search for a command to run...

UnylyUnyly
Назад к скиллам

rate-limiter

БесплатноБез исполняемых скриптовНе проверен

Designs and implements API rate limiting middleware and abuse protection. Use when you need to add request throttling, per-user quotas, IP-based blocking…

TerminalSkillsот TerminalSkills★ 221 звезда на GitHub

Об этом скилле

Rate Limiter

Overview

This skill enables AI agents to design, implement, and configure production-grade rate limiting for APIs. It covers algorithm selection, middleware generation, Redis-backed distributed counting, abuse pattern detection, and proper HTTP response headers.

Instructions

1. Assess the API Surface

Before writing any code, analyze the target application:

  • List all public endpoints and their HTTP methods
  • Classify endpoints by sensitivity: authentication (highest), write operations (high), read operations (medium), static/health (low)
  • Identify existing middleware stack and framework (Express, Fastify, Django, Gin, etc.)
  • Check if Redis or another shared store is available for distributed rate limiting

2. Choose the Right Algorithm

Select based on the use case:

Algorithm Best For Trade-off
Fixed Window Simple per-minute caps Burst at window edges
Sliding Window Log Precise per-user limits Higher memory per key
Sliding Window Counter Balance of accuracy and memory Slight approximation
Token Bucket APIs with burst allowance More complex to tune
Leaky Bucket Smooth output rate Delays rather than rejects

Default recommendation: Sliding Window Counter — it handles 95% of use cases with good accuracy and reasonable memory usage.

3. Implement Layered Limits

Always implement at least two layers:

Layer 1 — Global IP limit: Catches volumetric abuse before authentication. Typical: 100-300 req/min per IP.

Layer 2 — Endpoint-specific limits: Different limits per endpoint category. Auth endpoints get the strictest limits (3-10 req/min).

Layer 3 — Authenticated user quotas (if applicable): Daily or hourly caps per API key or user ID. Return quota status in response headers.

4. Response Headers

Send these headers on EVERY response (not just 429s). X-RateLimit-* is a widespread convention, not a standard:

X-RateLimit-Limit: 60
X-RateLimit-Remaining: 45
X-RateLimit-Reset: 1708012800     (epoch seconds, as in the example below)
Retry-After: 30                   (only on 429 responses)

The IETF draft draft-ietf-httpapi-ratelimit-headers (version 11, May 2026, still a draft, not an RFC) defines RateLimit-Policy and RateLimit instead, with the reset given as seconds from now. Choose one scheme and document it. A precise Retry-After is the number of seconds until the oldest counted request leaves the window; the example sends the full window length, which is a safe overestimate.

5. Abuse Detection Patterns

Beyond simple counting, detect:

  • Credential stuffing: Many unique usernames from one IP on auth endpoints
  • Scraping: Sequential access patterns on listing endpoints
  • Header anomalies: Missing User-Agent, rotating headers on rapid requests
  • Distributed attacks: Same user-agent/fingerprint across many IPs

6. Graceful Degradation

Always implement a fallback when Redis is unavailable:

  • Fall back to in-memory rate limiting per process
  • Log the Redis failure prominently
  • Do NOT fail open (allowing unlimited requests) unless explicitly configured

7. Avoid Common Pitfalls

  • Trust proxy configuration: Always verify how X-Forwarded-For is parsed. Behind a load balancer, the client IP may not be req.ip
  • Shared NAT: Don't set per-IP limits too low — corporate networks may have thousands of users behind one IP. Minimum 100 req/min for non-auth endpoints
  • Key design: Use composite keys (ip:endpoint, userId:endpoint) not just IP alone
  • Time sync: Use Redis server time (TIME command) not local clock for distributed setups

Examples

Example 1: Express middleware with Redis sliding window

Prompt: "Add rate limiting to my Express API. Auth endpoints should allow 5 requests per minute per IP. All other endpoints 60 per minute per authenticated user."

Output: The agent creates src/middleware/rateLimiter.ts:

import { Redis } from 'ioredis';
import { Request, Response, NextFunction } from 'express';

const redis = new Redis(process.env.REDIS_URL);

interface RateLimitConfig {
  windowMs: number;
  max: number;
  keyGenerator: (req: Request) => string;
}

const configs: Record<string, RateLimitConfig> = {
  auth: {
    windowMs: 60_000,
    max: 5,
    keyGenerator: (req) => `rl:auth:${req.ip}`,
  },
  default: {
    windowMs: 60_000,
    max: 60,
    keyGenerator: (req) => `rl:api:${(req as any).userId ?? req.ip}`,
  },
};

export function rateLimit(category: keyof typeof configs) {
  const config = configs[category];
  return async (req: Request, res: Response, next: NextFunction) => {
    const key = config.keyGenerator(req);
    // Read the Redis server clock so every app instance counts against the same window
    const [sec, usec] = await redis.time();
    const now = Number(sec) * 1000 + Math.floor(Number(usec) / 1000);
    const windowStart = now - config.window
Полный SKILL.md на GitHub

Установить rate-limiter в Claude Code и Claude Desktop

Зарегайся, чтобы установить скилл

Создай бесплатный аккаунт, чтобы открыть команду установки и сохранить скилл в библиотеку.

  • Открой команду установки в одну строку
  • Сохраняй скиллы в синхронизируемую библиотеку
  • Уведомления, когда скиллы обновляются
Зарегаться бесплатноУ меня уже есть аккаунт

Разрешённые инструменты

Инструменты, которые скиллу разрешено вызывать.

Без ограничений — скилл может использовать любой инструмент.

Вложенные файлы

_scores.json

FAQ

Что делает скилл rate-limiter?

Designs and implements API rate limiting middleware and abuse protection. Use when you need to add request throttling, per-user quotas, IP-based blocking, sliding window or token bucket algorithms, Redis-backed distributed counters, or 429 response handling. Trigger words: rate limit, throttle, API abuse, too many requests, request quota, DDoS protection, brute force prevention, credential stuffing defense.

Как установить скилл rate-limiter?

Скопируй папку скилла в ~/.claude/skills (вкладка Claude Code выше делает это одной командой), либо поставь как плагин.

Скилл rate-limiter запускает скрипты?

Нет, скилл состоит только из инструкций (SKILL.md), без исполняемых скриптов.

Похожие скиллы

Сравнить rate-limiter с