Skip to content

commandBackend and APIs

Add Rate Limit

Adds rate limiting to a named route group using your current stack, with sane limits, proper 429 replies and a test.

npx workspacetemplates add add-rate-limit --tool claude-code

Writes 1 file to .claude/commands/

.claude/commands/add-rate-limit.md15 lines · 1.2 KBFile
---
description: Adds rate limiting to a named route group using your current stack, with sane limits, proper 429 replies and a test.
argument-hint: "[route group, e.g. /auth/*]"
---

Add rate limiting to this route group: $ARGUMENTS

1. Find how the app is served and whether a limiter exists already (express-rate-limit, slowapi, Rack::Attack, a gateway or CDN rule, Redis). Reuse it. Do not add a new store without asking.
2. Choose the key per route: IP for anonymous routes, user id or API key for authenticated ones, and both IP and account name for login. Mention proxy setups: read the real client IP only from a trusted forwarded header.
3. Propose limits and show the numbers before coding, for example 5 per minute for login, 60 per minute for general reads. A burst allowance is better than a hard cap.
4. Return 429 with a Retry-After header and the project's normal error body. Never reveal whether an account exists.
5. Check multi-instance behavior: an in-memory counter is wrong behind several servers, so say so.
6. Write a test that exceeds the limit, sees the 429, and checks that a different key is unaffected. Run the tests.

Report the limits, keys and any gap such as a shared store you did not set up.

What it does

Login, signup and search routes invite abuse long before anything else does. This command adds limits to the route group you name, using the middleware or gateway feature your stack already has.

It picks the limit key with care, since keying only on IP punishes shared offices and keying only on user misses anonymous abuse. A test proves the limit actually trips.

Example

you › /add-rate-limit /auth/*

agent › Reused express-rate-limit with the existing Redis store. POST /auth/login: 5 per minute per IP+email. POST /auth/reset: 3 per hour per email. 429 with Retry-After.
Test passes: sixth login attempt is rejected, another email is not.
Note: trust proxy is off, so I set it to 1 for your load balancer. Confirm.

Questions

Which limits does it choose?
It proposes numbers per route type and shows them before writing code, so you can raise or lower them.
Does it work behind a load balancer?
It checks how the client IP is forwarded and warns when counters live in process memory across several instances.
  • Add Caching

    CommandBackend and APIs

    Free
  • Add Endpoint

    CommandBackend and APIs

    Free
  • Add Health Check

    CommandBackend and APIs

    Free
  • Add Pagination

    CommandBackend and APIs

    Free