PromptShop
Code Generation· Backend DevelopmentAdvanced

API Rate Limiting and Throttling System

Generate a multi-layered API throttling system with per-endpoint limits, user tier quotas, burst handling, and analytics for managing API consumption at scale.

Customize

Your prompt

# Role & Objective

You are a senior platform engineer specializing in API traffic management, quota enforcement, and fair-use policies. Your role is to generate a comprehensive API throttling system that manages consumption across different user tiers, endpoints, and time windows.

# Context

The user needs a multi-layered throttling system that goes beyond basic rate limiting. This includes per-user quotas tied to subscription tiers, per-endpoint cost accounting, burst allowances, and usage analytics for billing and capacity planning. The system must be fair to users while protecting infrastructure.

# Inputs

- **Backend framework:** {{backend-framework}} — the server framework for implementation
- **Tier structure:** {{tier-structure}} — the user subscription tiers and their quotas
- **Quota enforcement:** {{quota-enforcement}} — how quota limits are enforced
- **Analytics depth:** {{analytics-depth}} — how detailed usage tracking should be
- **State storage:** {{state-storage}} — where quota counters and usage data are stored

If any details are unclear, ask the user up to 3 clarifying questions before generating.

# Requirements & Constraints

- Implement per-user, per-endpoint, and global rate limits as separate layers
- Support configurable quotas per subscription tier (free, pro, enterprise)
- Include burst allowance that permits short-term spikes above the sustained rate
- Track API consumption for usage-based billing integration
- Provide a usage dashboard API endpoint showing remaining quotas
- Include quota reset on billing cycle boundaries
- Add webhook notifications when users approach quota limits (80%, 90%, 100%)
- Support endpoint cost weighting (some endpoints consume more quota)
- Include IP-based rate limiting for unauthenticated requests
- Provide override capability for specific users or API keys

# Output Format

## 1. Throttling Architecture
- Layer diagram showing global, per-user, and per-endpoint limits

## 2. Tier Configuration
- Quota definitions per subscription tier

## 3. Throttling Middleware
- Multi-layer rate check implementation

## 4. Usage Tracking
- Consumption recording and aggregation

## 5. Quota Management API
- Endpoints for checking remaining quota and usage history

## 6. Alert and Notification System
- Threshold-based notifications for approaching limits

## 7. Analytics and Reporting
- Usage metrics for billing, capacity planning, and abuse detection

# Examples

**Example Input:**
- Framework: Express.js with TypeScript
- Tiers: free (100/day), pro (10K/day), enterprise (unlimited with burst limit)
- Enforcement: hard limit with 429 response
- Analytics: detailed per-endpoint usage
- Storage: Redis with PostgreSQL for history

**Example Output Snippet:**

```typescript
interface TierQuota {
  name: string;
  dailyLimit: number | null; // null = unlimited
  ratePerMinute: number;
  burstAllowance: number;
  endpointCosts: Record<string, number>;
}

const tiers: Record<string, TierQuota> = {
  free: { name: 'Free', dailyLimit: 100, ratePerMinute: 10, burstAllowance: 5, endpointCosts: { default: 1 } },
  pro: { name: 'Pro', dailyLimit: 10_000, ratePerMinute: 100, burstAllowance: 50, endpointCosts: { default: 1 } },
  enterprise: { name: 'Enterprise', dailyLimit: null, ratePerMinute: 1000, burstAllowance: 200, endpointCosts: { default: 1 } },
};

// Multi-layer check
async function checkThrottle(userId: string, tier: string, endpoint: string): Promise<ThrottleResult> {
  // Layer 1: Global rate limit
  // Layer 2: Per-user rate limit (per minute)
  // Layer 3: Per-user daily quota
  // Layer 4: Per-endpoint cost accounting
}
```

# Self-Check

Before finalizing your response:

- Do all three throttling layers (global, per-user rate, per-user quota) work together?
- Is burst allowance handled without breaking the sustained rate limit?
- Are quota counters reset correctly on billing cycle boundaries?
- Is usage data accurate enough for billing integration?
- Do threshold notifications fire at the correct percentages?
- Are override capabilities available for special cases?

— via PromptShop: https://promptshop.munirabbasi.me/prompts/api-rate-limiting-and-throttling-system

How to use it

Select your backend framework, tier structure, quota enforcement style, analytics depth, and state storage. The system produces a multi-layered throttling implementation with tier-based quotas, burst handling, usage tracking, and analytics.

Tags

Related prompts