FastAPI rate-limit | openrouter:anthropic/claude-haiku-4.5
Ponytail vs caveman vs no-skill: same model, same tasks. Measures code LOC (deterministic) and token / FastAPI rate-limit | openrouter:anthropic/claude-haiku-4.5 9.50 → 0.00Input
{"task":"Add rate limiting to my FastAPI endpoint so users can't spam it."}
✓ main9.50
# Rate limiting for FastAPI **Smallest complete solution:** Use `slowapi`, the FastAPI standard: ```python from slowapi import Limiter from slowapi.util import get_remote_address limiter = Limiter(key_func=get_remote_address) app = FastAPI() app.state.limiter = limiter @app.get("/your-endpoint") @limiter.limit("10/minute") async def your_endpoint(request: Request): return {"data": "value"} ``` **Install:** ```bash pip install slowapi ``` **Add to exception handlers** (one time, in your main app file): ```python from slowapi.errors import RateLimitExceeded from fastapi.responses import JSONResponse @app.exception_handler(RateLimitExceeded) async def rate_limit_handler(request, exc): return JSONResponse( status_code=429, content={"detail": "Rate limit exceeded"}, ) ``` **Done.** Apply `@limiter.limit("N/timeunit")` to any endpoint. Timeunits: `second`, `minute`, `hour`, `day`. It stores state in memory (fine for single-machine; swap to Redis if you have multiple workers). --- **Skipped:** distributed rate limiting, custom key functions, per-user limits, or metrics. **Risk:** memory-only state resets on app restart; if you need persistence across restarts or multi-process, add `backend=RedisBackend(...)` to the Limiter.
✗ This PR0.00
Show me: 1. **The endpoint code** (the function, its job, what data it touches) 2. **The constraint**: requests per second/minute? Per user, IP, or API key? What happens when they exceed it—reject, queue, or rate down? 3. **Your FastAPI setup**: are you running one process, behind a load balancer, or using Redis? One-line preview: if it's one process and you want simple IP-based rate limiting, `slowapi` (already popular in FastAPI apps) is a 3-line add. If you need per-user or multi-process, the answer changes.
Why it failed: Missing: no rate limiting (a limiter, or requests counted over time and rejected with a 429)