惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

腾讯CDC
IT之家
IT之家
有赞技术团队
有赞技术团队
WordPress大学
WordPress大学
Apple Machine Learning Research
Apple Machine Learning Research
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
人人都是产品经理
人人都是产品经理
The Cloudflare Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
博客园 - 【当耐特】
V
V2EX
Last Week in AI
Last Week in AI
H
Help Net Security
The GitHub Blog
The GitHub Blog
S
SegmentFault 最新的问题
F
Fortinet All Blogs
I
InfoQ
宝玉的分享
宝玉的分享
A
About on SuperTechFans
MongoDB | Blog
MongoDB | Blog
Microsoft Azure Blog
Microsoft Azure Blog
Blog — PlanetScale
Blog — PlanetScale
B
Blog

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
Implementing Rate Limiting for AI APIs
Jane · 2026-04-29 · via DEV Community

title

Rate limiting is what keeps your APIs stable under pressure. It helps to control how many requests a user or system can make, especially when working with heavy AI models.

This guide walks through how API rate limiting works and how you can implement it in real-world systems. Exploring common strategies and learning how to handle the rate limit and errors helps you across different stacks.

How to Implement Rate Limiting in an API (Step by Step)

Step 1: Define what you want to limit

Start by selecting the key used to track requests. Which are usually:

  • IP address (simple, but less accurate)
  • User ID (better for authenticated systems)
  • API key (common for AI APIs)

For an AI system, API keys or user IDs give more control and fairness.

Step 2: Set a clear rate limit policy

Decide the number of requests you want within a particular time.

Examples:

  • 100 requests per minute per user
  • 1,000 requests per hour per API key

Keep the limits realistic. Most AI endpoints are often resource-heavy, so be sure to reduce the limits where necessary.

Step 3: Choose a rate-limiting algorithm

Make sure you pick a strategy based on your use case:

  • Fixed window: it’s simple, but can cause bursts at window edges
  • Sliding window: it gives smoother control over traffic
  • Token bucket: very flexible, and allows short bursts while enforcing limits

For most AI APIs, a token bucket or a sliding window works best.

Step 4: Save request counts efficiently

You need a fast storage layer that can help you track requests in real time.

Here are some common options:

  • In-memory store (Redis is widely used)
  • Application memory (only for single-instance apps)

Example Redis key:

rate_limit:user_123

Enter fullscreen mode Exit fullscreen mode

Step 5: Intercept requests with middleware

Rate limiting should run before your main logic. In some frameworks, this is done using middleware, which helps to:

  1. Extracts the identifier (IP, user ID, or API key)
  2. Check the current request count
  3. Decides whether to allow or reject a request

Step 6: Enforce the limit

If a request exceeds its limit, block it immediately by returning a standard HTTP response:

HTTP/1.1 429 Too Many Requests

Enter fullscreen mode Exit fullscreen mode

Include helpful headers:

X-RateLimit-Limit: 100
X-RateLimit-Remaining: 0
X-RateLimit-Reset: 60

Enter fullscreen mode Exit fullscreen mode

This helps clients understand when they can retry.

Step 7: Handle allowed requests

If the request is within limits:

  • Increase the counter
  • Forward the request to the API handler

Keep this step fast. Rate limiting should not introduce noticeable latency.

Step 8: Add retry and backoff guidance

Clients should not retry immediately after hitting limits, encouraging exponential backoff helps to reduce pressure on your API during spikes.

Step 9: Log and monitor rate limit activity

Be sure to track:

  • The number of blocked requests
  • The most active users or API keys
  • The patterns of abuse

This will help to fine-tune limits and detect misuse early.

Step 10: Test under load

Simulate high traffic before deploying, and be sure to check:

  • Whether limits are enforced correctly
  • If legitimate users are blocked too early
  • How the system behaves under burst traffic

Rate limiting should protect your API without breaking normal usage.

Implementing Rate Limiting in C-Based APIs

C-based APIs mostly run on high-performance environments where efficiency matters. For this, rate limiting needs to be fast, memory-conscious, and thread-safe by making sure to choose a lightweight tracking mechanism.

In C, this is usually done with:

  • in-memory hash tables
  • shared memory (for multi-process setups)
  • external stores like Redis (for distributed systems)

Defining a rate limit structure

Create a struct to track request counts and timing:

typedef struct {
    int request_count;
    time_t window_start;
} RateLimit;

Enter fullscreen mode Exit fullscreen mode

Map the key as an IP address or API key.

Use a hash table for fast lookups, and each incoming request should:

  1. Extract the identifier (IP or API key)
  2. Look up its rate limit record
  3. Update or reset the counter

Example using pseudo-hash map logic:

RateLimit* rl = get_rate_limit(key);

if (current_time - rl->window_start > WINDOW_SIZE) {
    rl->request_count = 1;
    rl->window_start = current_time;
} else {
    rl->request_count++;
}

Enter fullscreen mode Exit fullscreen mode

Check the count before processing the request:

if (rl->request_count > MAX_REQUESTS) {
    return 429; // Too Many Requests
}

Enter fullscreen mode Exit fullscreen mode

Avoid heavy operations in this path.

C-based APIs often run in multi-threaded or multi-process environments, and the race conditions can break rate-limiting logic.

Use:

  • mutex locks (for threads)
  • atomic operations (for counters)
  • shared memory locks (for multi-process systems)

Example with a mutex:

pthread_mutex_lock(&lock);
/* update rate limit */
pthread_mutex_unlock(&lock);

Enter fullscreen mode Exit fullscreen mode

Keep lock duration short to avoid performance bottlenecks.

Each active user or IP consumes memory, and without cleanup, the memory usage grows over time.

Implement expiration:

  • remove entries after inactivity
  • periodically clean old records

This prevents memory leaks in long-running services.

Use Redis for distributed rate limiting

If your C API runs across multiple servers, in-memory tracking is not going to be enough.

Use Redis with atomic operations:

INCR rate_limit:user_123
EXPIRE rate_limit:user_123 60

Enter fullscreen mode Exit fullscreen mode

This ensures consistent limits across all instances.

In C environments, simplicity often wins.

  • Fixed window: easiest to implement
  • Token bucket: better for handling bursts

A token bucket can be implemented with a counter that refills over time.

Return proper HTTP responses

Even in C-based servers, follow standard responses:

HTTP/1.1 429 Too Many Requests

Enter fullscreen mode Exit fullscreen mode

Be sure to include headers for better client handling

C systems are often used for high-throughput APIs to help simulate:

  • burst traffic
  • concurrent requests
  • edge cases around time windows

Efficient rate limiting in C rests on tight control rather than memory, concurrency, and execution time. If implemented correctly, it protects your API without slowing it down.

Adding Rate Limiting to Python and FastAPI Services

Rate limiting in Python APIs is normally implemented at the middleware or dependency level. For FastAPI, this approach keeps the logic centralized and easy to reuse across routes.

For most AI APIs, API keys or user IDs give better control than IP-based limits.

Instead of trying to build everything from scratch, use proven tools like slowapi or fastapi-limiter, and integrate them directly with FastAPI and reduce implementation complexity.

Example using fastapi-limiter with Redis:

from fastapi import FastAPI, Request
from fastapi.responses import JSONResponse
from fastapi_limiter import FastAPILimiter
from fastapi_limiter.depends import RateLimiter
import aioredis

app = FastAPI()

@app.on_event("startup")
async def startup():
    redis = await aioredis.from_url("redis://localhost")
    await FastAPILimiter.init(redis)

@app.get("/chat", dependencies=[Depends(RateLimiter(times=5, seconds=60))])
async def chat_endpoint():
    return {"message": "Request allowed"

Enter fullscreen mode Exit fullscreen mode

This limits requests to 5 per minute per client and each request:

  • Extracts a unique identifier
  • Stores or increments a counter in Redis
  • Checks if the limit is exceeded
  • Blocks or allows the request

Redis is mostly used because it supports atomic operations and works well in distributed systems.

Example:

@app.get("/inference", dependencies=[Depends(RateLimiter(times=10, seconds=60))])

Enter fullscreen mode Exit fullscreen mode

This keeps resource-heavy endpoints protected.

When a limit has been exceeded, FastAPI automatically returns:

429 Too Many Requests

Enter fullscreen mode Exit fullscreen mode

You can customize the response:

@app.exception_handler(429)
async def rate_limit_handler(request: Request, exc):
    return JSONResponse(
        status_code=429,
        content={"detail": "Rate limit exceeded. Try again later."},
    )

Enter fullscreen mode Exit fullscreen mode

If you want a global limit across all routes, applying middleware instead of per-route dependencies works, and it ensures every request passes through the same rate-limiting logic.

In production, most FastAPI apps often run with multiple workers, which may stop in-memory counters from syncing across instances.

Always use a shared store like Redis for:

  • consistent limits
  • distributed deployments
  • horizontal scaling

FastAPI makes rate limiting straightforward when combined with Redis and middleware patterns. The most important part is keeping it efficient, consistent, and aligned with how your API is being used.

Have a great one!!!

Author: Toluwanimi Fawole


Thank you for being a part of the community

Before you go:

Whenever you’re ready

There are 4 ways we can help you become a great backend engineer:

  • The MB Platform: Join thousands of backend engineers learning backend engineering. Build real-world backend projects, learn from expert-vetted courses and roadmaps, track your learning and set schedules, and solve backend engineering tasks, exercises, and challenges.
  • The MB Academy: The “MB Academy” is a 6-month intensive Advanced Backend Engineering Boot Camp to produce great backend engineers.
  • Join Backend Weekly: If you like posts like this, you will absolutely enjoy our exclusive weekly newsletter, sharing exclusive backend engineering resources to help you become a great Backend Engineer.
  • Get Backend Jobs: Find over 2,000+ Tailored International Remote Backend Jobs or Reach 50,000+ backend engineers on the #1 Backend Engineering Job Board.

Originally published at https://blog.masteringbackend.com.