Rate limit how many HTTP requests can be made in a given period of seconds, minutes, hours, days, months, or years.
If the AI Model, AI Agent, or AI MCP Server handling the request has no authentication layer, the client IP address is used to identify clients.
Otherwise, the AI Consumer is used once an AI Auth Strategy has authenticated the request.
This Policy counts requests. To rate limit on LLM token usage or cost instead, use the AI Rate Limiting Advanced Policy, which reads the token data returned by the AI Model Provider.
The Rate Limiting Policy supports two rate limiting strategies: local and redis.
This is controlled by the config.policy parameter.
Strategy
Description
Pros
Cons
local
Counters are stored in-memory on the node.
Minimal performance impact.
Less accurate. Unless there’s a consistent-hashing load balancer in front of AI Gateway, it diverges when scaling the number of nodes.
redis
Counters are stored on a Redis server and shared across nodes.
Accurate1, shared across all nodes.
Needs a Redis installation. Bigger performance impact than a local strategy.
[1]: Only when the config.sync_rate option is set to -1 (synchronous behavior).
Two common use cases for rate limiting are:
Every transaction counts: The highest level of accuracy is needed. An example is a transaction with financial consequences.
Backend protection: Accuracy is not as relevant.
The requirement is only to protect backend services from overloading that’s caused either by specific users or by attacks.
In this scenario, because accuracy is important, the local strategy is not an option. Use redis.
If you use a very high sync frequency, redis is the only workable choice.
The sync frequency becomes higher when the sync_rate setting is a lower number. For example, a sync_rate of 0.1 is a much higher sync frequency (10 counter syncs per second) than a sync_rate of 1 (1 counter sync per second).
You can calculate what is considered a very high sync rate in your environment based on your topology, number of AI Policies, their sync rates, and tolerance for loose rate limits.
If accuracy is less important, choose the local strategy.
You might need to experiment a little before you get a setting that works for your scenario.
As AI Gateway scales to more nodes, more user requests are handled.
When the number of nodes scales down, the probability of false negatives increases.
Make sure to adjust your rate limits when scaling.
For example, if a user can make 100 requests every second, and you have an equally balanced 5-node AI Gateway deployment, you can set the local limit to 30 requests every second.
If you see too many false negatives, increase the limit.
To minimize inaccuracies, consider using a consistent-hashing load balancer in front of AI Gateway.
The load balancer ensures that a user is always directed to the same AI Gateway node, which reduces inaccuracies and prevents scaling problems.
If your AI Policy uses a Redis datastore, you can authenticate to it with a cloud Redis provider. This allows you to rotate credentials without relying on static passwords.
The following providers are supported:
AWS ElastiCache
Azure Managed Redis
Google Cloud Memorystore (with or without Valkey)
Each provider also supports an instance and cluster configuration.
You need:
A running Redis instance on an AWS ElastiCache instance for Valkey 7.2 or later or ElastiCache for Redis OSS version 7.0 or later
If limiting by IP address, it’s important to understand how AI Gateway determines the IP address of an incoming request.
The IP address is extracted from the request headers sent to AI Gateway by downstream clients. Typically, these headers are named X-Real-IP or X-Forwarded-For.
By default, AI Gateway uses the header name X-Real-IP to identify the client’s IP address. If your environment requires a different header, you can specify this by setting the real_ip_header Nginx property. Depending on your network setup, you may also need to configure the trusted_ips Nginx property to include the load balancer IP address. This ensures that AI Gateway correctly interprets the client’s IP address, even when the request passes through multiple network layers.
The headers RateLimit-Limit, RateLimit-Remaining, and RateLimit-Reset are based on the Internet-Draft RateLimit Header Fields for HTTP and may change in the future to respect specification updates.