rate-limit
Installation
SKILL.md
Rate limiting with Upstash
Rate limits are one layer of abuse and cost control. Design policy before wiring middleware.
Workflow
- Detect Next.js/runtime/version, deployed proxy/CDN, auth/API-key model, installed Upstash versions, protected routes, operation cost, existing quotas, and whether middleware/proxy already touches the request.
- Define a policy table per route class: identifier, algorithm/window/burst, cost weight, user tier, response behavior, and store-outage behavior. Ask only when the threat/cost tradeoff cannot be inferred.
- Prefer authenticated user, API key, or tenant+user identifiers. Use IP only for anonymous traffic and only from a deployment-provided trusted source. Never trust arbitrary
x-forwarded-forfrom the public internet; configure trusted proxy depth/platform headers. Do not collapse unknown clients to127.0.0.1. - Namespace by environment and route/policy. Do not apply blanket middleware and a per-route limiter to the same request unless deliberate layered limits use different keys.
- Configure explicit Upstash timeout and logging/metrics. Choose outage behavior by endpoint:
- low-risk availability paths may fail open with degraded telemetry;
- authentication, high-cost generation, or abuse-sensitive paths may use a local emergency limit, queue, credit reservation, or fail closed with a clear temporary response.
- Return
429with a non-negativeRetry-Afterand consistent rate-limit headers. Add headers to successful responses where useful. Do not emit misleading zero limits during fail-open degradation. - For weighted work, consume tokens based on server-calculated batch/operation cost. Combine rate limits with body-size limits, concurrency controls, idempotency, budgets/credits, and provider quotas where relevant.
- Preserve async work such as analytics synchronization using the runtime’s
waitUntilmechanism when the SDK exposes a pending promise.