AI-powered adaptive rate limiter with embedded monitoring dashboard for FastAPI
Project description
Adaptive Rate Limiter
An AI-powered adaptive rate limiter for FastAPI with a fully embedded real-time monitoring dashboard. It uses machine learning (Isolation Forest) combined with statistical traffic analysis to dynamically adjust rate limits per IP.
Features
- 🤖 AI + Statistical Decision Engine — Isolation Forest anomaly detection combined with entropy, burstiness, and interval analysis
- 📊 Embedded Dashboard — React SPA at
/limiter-dashboardwith live charts, no separate server needed - 🛑 Ban / Unban IPs — One-click manual IP control directly from the dashboard
- ⏱️ Auto-expiry — IPs automatically vanish from the dashboard after configurable idle time
- 🌐 Proxy-aware — Reads
X-Forwarded-For/X-Real-IPfor correct IP tracking behind Nginx/Apache - 📦 pip installable — Models and dashboard are bundled; works out-of-the-box after install
- 🔴 Redis-backed — All IP data persisted in Redis; survives server restarts
Requirements
- Python ≥ 3.8
- Redis (any recent version)
Installation
pip install adapt-rate-limiter
Quick Start
Option 1 — Integrate into your existing FastAPI app
from fastapi import FastAPI
from adapt_rate_limiter import AdaptiveRateLimitMiddleware, mount_rate_limiter
app = FastAPI()
# 1. Mount the dashboard + wire Redis setup
mount_rate_limiter(
app,
redis_host="localhost", # your Redis host
redis_port=6379,
window_size=60, # sliding window (seconds)
ip_ttl=300, # IPs expire after 5 min of inactivity
)
# 2. Add Global API Protection!
# This intercepts ALL requests globally and handles AI checks instantly.
app.add_middleware(AdaptiveRateLimitMiddleware)
# 3. Write your endpoints natively!
@app.get("/my-api") # Automatically Protected!
async def endpoint():
return {"message": "OK"}
Then visit http://yourserver/limiter-dashboard for the monitoring dashboard.
Option 2 — Run the standalone server
If you just want the rate limiter as a standalone server:
pip install adapt-rate-limiter uvicorn
uvicorn adapt_rate_limiter.main:app --host 0.0.0.0 --port 8000
Endpoints:
| URL | Description |
|---|---|
GET /api |
Demo rate-limited endpoint |
GET /dashboard-data |
JSON metrics for all tracked IPs |
GET /limiter-dashboard |
Monitoring dashboard UI |
POST /ban-ip |
Ban an IP {"ip": "1.2.3.4"} |
POST /unban-ip |
Unban an IP {"ip": "1.2.3.4"} |
Configuration
| Parameter | Default | Description |
|---|---|---|
redis_host |
"localhost" |
Redis server hostname |
redis_port |
6379 |
Redis server port |
window_size |
60 |
Sliding window in seconds |
ip_ttl |
600 |
Seconds to keep idle IPs in dashboard |
enable_dashboard |
True |
Set False to skip mounting the dashboard |
Environment Variables
You can also configure via environment variables (useful for Docker/k8s):
REDIS_HOST=redis.internal
REDIS_PORT=6379
IP_SUMMARY_TTL=300
Dashboard Features
- Requests/min timeline — Area chart of traffic over the last 10 minutes
- Action distribution — Donut chart (ALLOW / RESTRICT / BLOCK)
- Risk distribution — Donut chart (LOW / MEDIUM / HIGH)
- Top 5 IPs — Horizontal bar chart by request count
- IP table — Sortable, searchable, filterable by action and ban status
- Ban / Unban — Click the button in any IP row to instantly block or unblock
- Auto-refresh — Polls
/dashboard-dataevery second
Behind Nginx / Apache
Add the proxy headers configuration:
location / {
proxy_pass http://127.0.0.1:8000;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
}
The rate limiter automatically reads X-Forwarded-For and X-Real-IP to get the real client IP.
Building the Dashboard (for contributors)
The React dashboard source lives in dashboard-ui/. After making changes:
cd dashboard-ui
npm install
npm run build # outputs to adapt_rate_limiter/dashboard/static/
Then rebuild the Python package:
pip install build
python -m build
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file adapt_rate_limiter-1.0.1.tar.gz.
File metadata
- Download URL: adapt_rate_limiter-1.0.1.tar.gz
- Upload date:
- Size: 1.2 MB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0b9a16c2793dcef4be003b299dc945f44f0b457edf7277ec4d10c8bfb9091cd5
|
|
| MD5 |
28bc72f4db4883fff28b01d5c744147c
|
|
| BLAKE2b-256 |
06a992c5b3fa92eb6685cf72d23bc5006bc5422b6f52a4783ac72404e57d4ab3
|
File details
Details for the file adapt_rate_limiter-1.0.1-py3-none-any.whl.
File metadata
- Download URL: adapt_rate_limiter-1.0.1-py3-none-any.whl
- Upload date:
- Size: 1.3 MB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
29f1008d180abd08747f2f8f556519e6d28e058dad50d4eeb1a77bce1d12d46b
|
|
| MD5 |
48e41bb04dc8aff7add14a46aa647cce
|
|
| BLAKE2b-256 |
e17f1cfa5dcf35fb1b97d2960fcdcc88ba1dafb4f5defa7ae91a62e42a5c1f07
|