Course contentsShow
← AI Engineering
Lesson 989 of 1,88624. Building AI APIsFree lesson

Per-User and Per-Key Rate Limits

Implementing differentiated rate limits based on user tiers, API keys, and subscription levels.

Per-User and Per-Key Rate Limits

You'll learn how to implement differentiated rate limits that vary based on user identity, API key tier, and subscription level rather than applying blanket restrictions.

Why Differentiate Rate Limits?

Not all API consumers are equal. Your free-tier users might get 10 requests per minute, while enterprise customers pay for 1,000. Similarly, a read-only analytics key shouldn't have the same quota as a production application key. Per-user and per-key rate limiting lets you enforce these business rules programmatically.

Core Implementation Pattern

Think of it like a nightclub with different wristbands: VIP, regular, and guest passes each grant different access levels. Your rate limiter checks the "wristband" (API key or user ID) and applies the corresponding limit.

The typical approach:

  1. Extract identity from the request (API key, user ID from authentication)
  2. Look up the tier (free, pro, enterprise) from your database or configuration
  3. Apply tier-specific limits using your rate limiter with a compound key like {tier}:{user_id}

For example, you might store rate limit windows per unique identifier: rate_limit:enterprise:user_12345 gets a different counter than rate_limit:free:user_67890.

Common Differentiation Dimensions

  • Subscription tiers: Free (10/min), Pro (100/min), Enterprise (unlimited)
  • Key types: Development keys (lower limits), production keys (higher limits)
  • Endpoint sensitivity: Expensive LLM operations vs. simple lookups
  • User reputation: Trusted users get higher limits; new accounts start restricted

You'll typically combine rate limiting fundamentals (covered in lesson 988) with authentication data (lessons 985-987) to retrieve the user's tier and apply the correct policy.

Key Takeaway: Per-user and per-key rate limiting enforces business logic by treating different API consumers differently based on their identity, subscription level, or key type, enabling monetization and fair resource allocation.