Home · Developers · Guides · API Rate Limiting

APIs

API Rate Limiting

In-memory limiter and governed paths

The Nova Coder API supports enhanced rate limiting with configurable limits, whitelist/blacklist support, and graceful dependency handling.

Overview

Rate limiting protects the API from abuse and ensures fair resource allocation. The enhanced rate limiting system uses a token bucket algorithm with configurable limits and supports both in-memory and slowapi-based implementations.

Features

Token Bucket Algorithm

  • Configurable burst limits and refill rates
  • Automatic token replenishment over time
  • Temporary blocking for exceeded limits
  • Retry-after headers for blocked clients

Whitelist/Blacklist

  • Whitelist for unlimited access (e.g., internal services)
  • Blacklist for blocking abusive clients
  • Dynamic management of lists
  • Per-client statistics

Configuration

  • Environment-based configuration
  • Configurable rate limits and burst limits
  • TTL settings for rate limit windows
  • Enable/disable features as needed

Configuration

Environment Variables

  • API_RATE_LIMIT: Default rate limit (default: "100/minute")
  • API_BURST_LIMIT: Maximum burst size (default: 10)
  • API_RATE_LIMIT_TTL: Time-to-live for rate limit windows (default: 60 seconds)

Configuration Class

from novacoder.api.rate_limit import RateLimitConfig

config = RateLimitConfig(
    rate_limit="100/minute",
    burst_limit=10,
    ttl=60,
    enable_whitelist=True,
    enable_blacklist=True
)

Usage Examples

Basic Rate Limiting

from novacoder.api.rate_limit import in_memory_limiter, get_client_id

# Check if request is allowed
client_id = get_client_id(request)
allowed, info = in_memory_limiter.check_rate_limit(client_id)

if not allowed:
    return JSONResponse(
        {"error": "Rate limit exceeded", "retry_after": info.get("retry_after")},
        status_code=429
    )

Whitelist Management

from novacoder.api.rate_limit import in_memory_limiter

# Add client to whitelist
in_memory_limiter.add_to_whitelist("203.0.113.100")

# Remove from whitelist
in_memory_limiter.remove_from_whitelist("203.0.113.100")

# Check if whitelisted
if in_memory_limiter.is_whitelisted("203.0.113.100"):
    # Allow unlimited requests
    pass

Blacklist Management

from novacoder.api.rate_limit import in_memory_limiter

# Add client to blacklist
in_memory_limiter.add_to_blacklist("203.0.113.200")

# Remove from blacklist
in_memory_limiter.remove_from_blacklist("203.0.113.200")

# Check if blacklisted
if in_memory_limiter.is_blacklisted("203.0.113.200"):
    # Block all requests
    pass

Rate Limit Statistics

from novacoder.api.rate_limit import in_memory_limiter

# Get rate limiting statistics
stats = in_memory_limiter.get_stats()
print(f"Total clients: {stats['total_clients']}")
print(f"Whitelisted: {stats['whitelisted_clients']}")
print(f"Blacklisted: {stats['blacklisted_clients']}")
print(f"Blocked: {stats['blocked_clients']}")

Implementation

The rate limiting system is implemented in novacoder/api/rate_limit.py:

from novacoder.api.rate_limit import (
    RateLimitConfig,
    InMemoryRateLimiter,
    check_rate_limit_middleware
)

Dependency Handling

The system gracefully handles the slowapi dependency:

  • If slowapi is available, it uses the library for enhanced rate limiting
  • If not available, it falls back to the in-memory implementation
  • No breaking changes if the dependency is missing

Debugging Status

✅ Verified Working: All rate limiting components have been debugged and verified:

  • RateLimitConfig initializes correctly with all parameters
  • InMemoryRateLimiter implements token bucket algorithm correctly
  • Whitelist/blacklist management works correctly
  • Rate limit checking returns proper allowed/denied responses
  • Statistics tracking works correctly
  • Graceful handling of missing slowapi dependency
  • No import conflicts with existing systems

Best Practices

  1. Set Appropriate Limits: Configure limits based on your API capacity
  2. Use Whitelists: Whitelist trusted services that need higher limits
  3. Monitor Statistics: Regularly check rate limiting statistics
  4. Handle 429 Responses: Clients should respect retry-after headers
  5. Test Limits: Test rate limiting in staging before production

Source: wiki/api-rate-limiting.md · rendered 2026-11-10