API Rate Limiting

API Rate Limiting

Guides an AI coding agent to implement API rate limiting with token bucket, sliding window and fixed window algorithms, including tiered limits and Redis.

Skill files9 files

references/

scripts/

templates/

SKILL.md100 lines
1
---
2
name: api-rate-limiting
3
description: >
4
Implement API rate limiting strategies using token bucket, sliding window, and
5
fixed window algorithms. Use when protecting APIs from abuse, managing
6
traffic, or implementing tiered rate limits.
7
---
8
9
# API Rate Limiting
10
11
## Table of Contents
12
13
- [Overview](#overview)
14
- [When to Use](#when-to-use)
15
- [Quick Start](#quick-start)
16
- [Reference Guides](#reference-guides)
17
- [Best Practices](#best-practices)
18
19
## Overview
20
21
Protect APIs from abuse and manage traffic using various rate limiting algorithms with per-user, per-IP, and per-endpoint strategies.
22
23
## When to Use
24
25
- Protecting APIs from brute force attacks
26
- Managing traffic spikes
27
- Implementing tiered service plans
28
- Preventing DoS attacks
29
- Fairness in resource allocation
30
- Enforcing quotas and usage limits
31
32
## Quick Start
33
34
Minimal working example:
35
36
```javascript
37
// Token Bucket Rate Limiter
38
class TokenBucket {
39
constructor(capacity, refillRate) {
40
this.capacity = capacity;
41
this.tokens = capacity;
42
this.refillRate = refillRate; // tokens per second
43
this.lastRefillTime = Date.now();
44
}
45
46
refill() {
47
const now = Date.now();
48
const timePassed = (now - this.lastRefillTime) / 1000;
49
const tokensToAdd = timePassed * this.refillRate;
50
51
this.tokens = Math.min(this.capacity, this.tokens + tokensToAdd);
52
this.lastRefillTime = now;
53
}
54
55
consume(tokens = 1) {
56
this.refill();
57
58
if (this.tokens >= tokens) {
59
this.tokens -= tokens;
60
return true;
61
}
62
// ... (see reference guides for full implementation)
63
```
64
65
## Reference Guides
66
67
Detailed implementations in the `references/` directory:
68
69
| Guide | Contents |
70
|---|---|
71
| [Token Bucket Algorithm](references/token-bucket-algorithm.md) | Token Bucket Algorithm |
72
| [Sliding Window Algorithm](references/sliding-window-algorithm.md) | Sliding Window Algorithm |
73
| [Redis-Based Rate Limiting](references/redis-based-rate-limiting.md) | Redis-Based Rate Limiting |
74
| [Tiered Rate Limiting](references/tiered-rate-limiting.md) | Tiered Rate Limiting |
75
| [Python Rate Limiting (Flask)](references/python-rate-limiting-flask.md) | Python Rate Limiting (Flask) |
76
| [Response Headers](references/response-headers.md) | Response Headers |
77
78
## Best Practices
79
80
### ✅ DO
81
82
- Include rate limit headers in responses
83
- Use Redis for distributed rate limiting
84
- Implement tiered limits for different user plans
85
- Set appropriate window sizes and limits
86
- Monitor rate limit metrics
87
- Provide clear retry guidance
88
- Document rate limits in API docs
89
- Test under high load
90
91
### ❌ DON'T
92
93
- Use in-memory storage in production
94
- Set limits too restrictively
95
- Forget to include Retry-After header
96
- Ignore distributed scenarios
97
- Make rate limits public (security)
98
- Use simple counters for distributed systems
99
- Forget cleanup of old data
100

© AJ Geddes · MIT · source · posted by @OpenSourceBuffGuy on promptparty.com

@OpenSourceBuffGuy

I find openly licensed prompts and post them here.

0 followers · 14,621 prompts

More by @OpenSourceBuffGuy

See all
Sea turtle carrying floating film studio island prompt image

Sea turtle carrying floating film studio island

Draws a giant sea turtle carrying a floating island film studio through a starry sky, where fairies polish rough film strips into golden film.

image-prompt, nano-banana, 3d-render, cartoon, fantasy-world, creatures, animals, whimsical, dreamy, cinematic, dramatic-lighting, vibrant-colors, bokeh, miniature, widescreen, night-scene, illustration, concept-art, surreal, storytelling, clay-3d

Desert buggy escapes drones past industrial outpost prompt image

Desert buggy escapes drones past industrial outpost

Builds a 15-second desert chase video where a racer in an armored buggy escapes drones past an industrial outpost, using five reference images with set jobs.

video-prompt, hailuo, action, multi-shot, timed-beats, camera-movement, reference-image, character-consistency, negative-prompt, cinematic, sci-fi, cars, landscape, aerial-view, tracking-shot, photorealistic, sound-effects, structured-output, long-form, widescreen

1 view
Cozy Pixel Art Farm Island Village prompt image

Cozy Pixel Art Farm Island Village

Draws a cozy 16-bit pixel-art farming village across six small islands joined by bridges, with exact counts for buildings, animals and crops.

image-prompt, gpt-image, pixel-art, isometric, game-assets, concept-art, maps, worldbuilding, cozy, vibrant-colors, ocean, landscape, wide-shot, widescreen, game-development, video-games, structured-output, whimsical, summer, high-angle, aerial-view, animals, negative-prompt, cityscape

1 view

Reviews

Loading reviews…

Discussion

Loading comments…