Generated banner for Upstash Ratelimit JS
Generated logo for Upstash Ratelimit JS

Upstash Ratelimit JS

UpstashOfficialCreated by Upstash

  • Skill
  • Tool integration
  • Software development
  • MIT
  • No issues found

From the author

Rate limiting for serverless and edge apps with the @upstash/ratelimit TypeScript/JavaScript SDK backed by Upstash Redis. Use when adding a rate limiter or throttling to an API route, Next.js middleware, Vercel Edge, Cloudflare Workers, or any HTTP endpoint; returning 429 Too Many Requests; choosing between fixed window, sliding window, and token bucket algorithms; limiting per user, IP, API key, or tenant with prefixes and custom keys; protecting login, signup, form, or AI endpoints from abuse, bots, and brute force; using deny lists, ephemeral caching, analytics, timeouts, and multi-region rate limits; or estimating the Redis command cost of rate limiting. Also use when the user says rate limit, rate-limiting, throttle, quota, request limits, or traffic protection.
— the description by Upstash

AutoGineer summary

Written by AutoGineer from the skill’s own files, not by the author

The upstash-ratelimit-js skill provides guidance and documentation for integrating the @upstash/ratelimit SDK into TypeScript and JavaScript applications. It supports various rate-limiting algorithms, including fixed window, sliding window, and token bucket, to help developers manage traffic, prevent abuse, and enforce quotas across serverless and edge environments like Vercel Edge and Cloudflare Workers.

What it does

  • Provides implementation patterns for rate limiting API routes and middleware.
  • Supports multiple algorithms including fixed window, sliding window, and token bucket.
  • Enables traffic control based on user IDs, IP addresses, API keys, or tenant identifiers.
  • Offers guidance on protecting endpoints like login, signup, and AI services from abuse.
  • Includes documentation on Redis command costs and operational considerations.
  • Facilitates the use of advanced features like deny lists, analytics, and multi-region configurations.

When to use it

  • When you need to implement request throttling or quotas for your HTTP endpoints.
  • When protecting sensitive endpoints from brute force or bot abuse.
  • When building serverless or edge applications that require stateful rate limiting.
  • When you need to choose between different rate-limiting algorithms for specific traffic patterns.
  • When you need to estimate the Redis command costs associated with your rate-limiting strategy.

Advantages

  • Native support for serverless and edge runtime environments.
  • Flexible configuration for custom keys and request prefixes.
  • Comprehensive documentation on algorithms and traffic protection patterns.
  • Integration with Upstash Redis for distributed state management.

Usage examples

“How do I implement a sliding window rate limit for a user in my Next.js API route?”
The agent provides a code snippet using the Ratelimit class with the slidingWindow algorithm, showing how to check the success boolean to determine if a request should be throttled.
“What are the differences between the available rate-limiting algorithms?”
The agent explains the behavior of fixed window, sliding window, and token bucket algorithms as described in the algorithms.md file.

Install

  1. Download the skill as a ZIP:

    https://mp-staging.autogineer.com/agent-skills/upstash/upstash-ratelimit-js/download
  2. Unzip it into the cross-client skills folder of your project:

    mkdir -p .agents/skills && unzip upstash-ratelimit-js.zip -d .agents/skills

Documentation by Upstash

Rate Limit TS SDK

Bundled docs and source

Before writing or debugging SDK code, check node_modules/@upstash/ratelimit/docs/ for bundled docs and node_modules/@upstash/ratelimit/src/ for readable TypeScript source. Read and search the relevant files when present; otherwise use the references in this skill.

Quick Start

  • Install the SDK and connect to Redis.

  • Create a rate limiter and apply it to incoming operations.

Example:

import { Ratelimit } from "@upstash/ratelimit";
import { Redis } from "@upstash/redis";

const redis = new Redis({ url: "<url>", token: "<token>" });
const limiter = new Ratelimit({ redis, limiter: Ratelimit.slidingWindow(5, "10s") });

const { success } = await limiter.limit("user-id");
if (!success) {
  // throttled
}

Other Skill Files

  • algorithms.md: Describes all available rate‑limiting algorithms and how they behave.

  • pricing-cost.md: Explains pricing, Redis cost implications, and operational considerations.

  • features.md: Lists SDK features such as prefixes, custom keys, and behavioral options.

  • methods-getting-started.md: Full method reference for the SDK's API and getting started guide.

  • traffic-protection.md: Guidance on applying rate limiting for traffic shaping, abuse prevention, and protection patterns.

Files

pricing-cost.md

Pricing & Cost Considerations for Ratelimit Operations

This document explains how Redis command costs vary across Ratelimit algorithms, cache states, and optional features. Use it to reason about latency, throughput, and pricing impacts when designing systems with Upstash Ratelimit.


Overview

Redis command usage depends on:

  • Algorithm type (Fixed Window, Sliding Window, Token Bucket)

  • Algorithm state for a given identifier (first request, intermediate, rate‑limited)

  • Cache hit/miss in the runtime environment

  • Optional features (deny lists, analytics, dynamic limits, multi‑region replication)

A Global Upstash Redis setup multiplies write commands by (1 + readRegionCount) and adds 1 extra command when analytics is enabled.


Algorithm States

Each identifier (e.g., an IP or user ID) has an associated state:

  • First: No existing key; creates state and sets expiry

  • Intermediate: Key exists; normal operation

  • Rate‑Limited: Request blocked; may avoid Redis if cache contains an unexpired block timestamp

Cache hits allow skipping Redis entirely for rate‑limited requests.


Cache Behavior

  • Hit: Identifier found in in‑memory cache → request may be denied without Redis calls.

  • Miss: Cache empty or value does not indicate a block → algorithm consults Redis.

Only rate‑limited results populate the cache.


Command Costs by Operation

limit()

Costs depend on algorithm, cache state, and identifier state.

Fixed Window:

  • First: 3 commands (EVAL, INCR, PEXPIRE)

  • Intermediate: 2 commands (EVAL, INCR)

  • Rate‑limited miss: 2 commands (EVAL, INCR)

  • Rate‑limited hit: 0 commands

Sliding Window:

  • First: 5 commands (EVAL, GET, GET, INCR, PEXPIRE)

  • Intermediate: 4 commands (EVAL, GET, GET, INCR)

  • Rate‑limited miss: 3 commands (EVAL, GET, GET)

  • Rate‑limited hit: 0 commands

Token Bucket:

  • First/Intermediate: 4 commands (EVAL, HMGET, HSET, PEXPIRE)

  • Rate‑limited miss: 2 commands (EVAL, HMGET)

  • Rate‑limited hit: 0 commands

getRemaining()

Always deterministic, no cache effects:

  • Fixed Window: 2 commands (EVAL, GET)

  • Sliding Window: 3 commands (EVAL, GET, GET)

  • Token Bucket: 2 commands (EVAL, HMGET)

resetUsedTokens()

Starts with SCAN and deletes all matching keys:

  • Fixed Window: 3 commands (EVAL, SCAN, DEL)

  • Sliding Window: 4 commands (EVAL, SCAN, DEL, DEL)

  • Token Bucket: 3 commands (EVAL, SCAN, DEL)

blockUntilReady()

Same cost model as limit().


Optional Features Impact

Deny List

Adds 2 commands per limit() call:

  • One SMISMEMBER check

  • One TTL fetch for deny list validity

Auto‑IP deny list refresh uses 9 commands once per day (first limit call after 02:00 UTC).

A deny‑listed identifier is cached for a minute, skipping Redis for subsequent checks.

Analytics

Adds 1 Redis command per limit() via ZINCRBY.

Dynamic Limits

Adds 1 command to each limit() and getRemaining() call.

  • setDynamicLimit() = 1 command

  • getDynamicLimit() = 1 command

Multi‑Region

Effective cost becomes:

(1 + readRegionCount) * writeCommandCount + readCommandCount
+1 if analytics is enabled

Example Usage

import { Ratelimit } from "@upstash/ratelimit";
import { Redis } from "@upstash/redis";

const redis = new Redis({ url: process.env.UPSTASH_URL!, token: process.env.UPSTASH_TOKEN! });

const limiter = new Ratelimit({
  redis,
  limiter: Ratelimit.slidingWindow(10, "60 s"),
  analytics: true,            // adds +1 ZINCRBY per call
  enableProtection: true,     // adds +2 per call
  dynamicLimits: true         // adds +1 per call
});

async function handle(ip) {
  const { success, reset } = await limiter.limit(ip);
  if (!success) {
    return { status: 429, retryAfter: reset - Date.now() };
  }

  const remaining = await limiter.getRemaining(ip);
  return { status: 200, remaining };
}

This example shows all cost‑impacting features enabled. Sliding Window + deny list + analytics + dynamic limits can increase the base 4–5 command cost per limit().


Common Pitfalls

  • Ignoring cold starts: Serverless environments often start with empty caches → initial calls incur higher Redis usage.

  • Using Sliding Window when cost‑sensitive: It requires multiple GET operations, making it the most expensive algorithm.

  • Unexpected multi‑region amplification: Write commands scale with region count; cost can rise significantly.

  • Assuming deny list is free: Even when no identifier is blocked, two extra commands execute per request.


This file provides an operational reference for estimating Redis usage and optimizing Ratelimit performance and cost.

Requirements and compatibility

Works with: Claude Code, Claude apps, OpenAI Codex, GitHub Copilot, Cursor, Gemini CLI, Kiro, OpenCode, Goose, Amp, Roo Code, JetBrains Junie, Factory Droid, Devin (ex-Windsurf), Mistral Vibe, OpenHands, Letta

“Works with” means the tool reads the open Agent Skills format; a skill written for one tool may use features others lack.

Security

No issues found by our static scan· No issues found

A static scan is not a guarantee. Skills are third-party content: review a skill before you let an agent use it.

Attribution and licence

Created by
Upstash
Publisher
Upstash (Official)
Source
upstash/skills · skills/upstash-ratelimit-js · commit 9f9e3e5
Licence
MIT License (redistribution allowed)
  • LICENSE (added by AutoGineer from LICENSE)
  • ATTRIBUTION.md (written by AutoGineer)
Synced

AutoGineer is not affiliated with or endorsed by Upstash.

Download ZIP

The ZIP holds the skill’s files, its licence files and an ATTRIBUTION.md. Download upstash-ratelimit-js.zip