Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

AI Gateway Rate Limits & Token Quotas Guide

· 5 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

Setting token quotas and rate limits for teams through an AI gateway prevents unexpected costs, protects shared API keys from abuse, and keeps latency predictable. This guide walks through configuring per-team limits on a typical gateway like Kong, Tyk, or a cloud provider's API management layer.

What You'll Need

  • Admin access to your AI gateway (self-hosted or cloud console).
  • API keys or team identifiers already created in the gateway.
  • Knowledge of your AI provider's pricing tiers (e.g., tokens per minute, requests per second).
  • Optional: a test script or tool like curl to verify limits.

Step-by-Step

  1. Identify the gateway's rate-limiting plugin or module. Most gateways have built-in rate limiting (e.g., Kong's rate-limiting plugin, Tyk's rate_limit middleware, or AWS API Gateway's usage plans). Open the admin dashboard or CLI.
  2. Define your quota policy. Decide the limits per team: requests per second (RPS), tokens per minute, and daily token cap. For example, a team might get 100 RPS, 50,000 tokens per minute, and 1 million tokens per day.
  3. Create a rate limit rule for the team. In the gateway, navigate to the route or service that proxies to your AI provider. Add a rate limit policy that applies to the team's API key or a header like X-Team-ID. Set the time window (e.g., 1 second, 1 minute, 1 day) and the max number of requests or tokens.
  4. Set token quotas at the provider level. If your AI provider (OpenAI, Anthropic) has its own token limits, configure those in the provider's dashboard as a hard cap. The gateway limits should be slightly lower to avoid hitting provider caps and getting 429 errors.
  5. Apply the policy to the team's key. In the gateway, associate the rate limit policy with the team's API key or consumer group. For example, in Kong, create a consumer for the team, then attach the rate-limiting plugin to that consumer.
  6. Test the limits. Send requests from the team's key to verify that the gateway returns 429 Too Many Requests when the limit is exceeded. Use a tool like curl with the key to hit the endpoint multiple times rapidly.
  7. Monitor and adjust. Check the gateway's analytics or logs to see if teams are hitting limits. Adjust the quotas based on actual usage patterns. Set up alerts for when a team reaches 80% of its quota.
  8. Document the policy. Share the limits with your team so they know what to expect. Include the exact numbers and the error handling guidance (e.g., retry with backoff).

Common Mistakes

  • Setting limits too high: If you set the gateway limit above the provider's limit, the provider will still throttle you, causing unexpected errors. Always set gateway limits at least 10% below provider caps.
  • Using only IP-based limits: IPs can change (especially with cloud teams), and multiple users behind a NAT can share an IP, causing false positives. Prefer key-based or header-based limits.
  • Ignoring burst vs. sustained limits: A single high burst can exhaust a daily quota quickly. Use separate limits for per-second and per-day to balance responsiveness and cost.
  • Not testing with the actual team key: Testing with an admin key may bypass limits, giving a false sense of security. Always test with the team's credentials.
  • Forgetting to update limits after provider changes: If your AI provider changes their pricing or limits, your gateway quotas become stale. Review them quarterly.

Tips

  • Use token-based limits for cost control: Request counts don't reflect cost (a prompt with 10k tokens costs more than one with 100). If your gateway supports token counting (e.g., via a plugin that parses the request body), use it.
  • Implement a retry-after header: When the gateway returns 429, include a Retry-After header so clients know when to retry. This reduces repeated failed requests.
  • Create separate limits for different endpoints: For example, a cheap embedding model can have a higher RPS than an expensive chat model.
  • Use consumer groups for tiered access: Instead of configuring limits per key, create groups (e.g., 'free-tier', 'pro') and apply limits to the group. This makes management easier as teams grow.
  • Log all 429s: Capture the key, endpoint, and timestamp for every rate-limit rejection. This helps you identify which teams need adjustments.

Troubleshooting

  • Limits not applying: Check that the plugin is enabled on the correct route or service. In Kong, run curl -X GET http://localhost:8001/routes/{route_id}/plugins to verify. Also, ensure the team's key is actually associated with the consumer you configured.
  • Getting 429 even when under the limit: This can happen if the provider's own rate limit is lower than yours. Check the provider's response headers (e.g., x-ratelimit-remaining) to see which limit is being hit. Also, check if there are other gateway-level limits (e.g., a global limit) that you forgot about.
  • Token counting not working: Some gateways require a plugin to parse the request body to count tokens. If you're using a streaming response, token counting may be inaccurate. Use a conservative estimate or switch to request-based limits if token counting is unreliable.
  • Teams complaining about intermittent failures: Check if the limit is per-key or per-IP. If per-IP, multiple team members may share an IP, causing one member to exhaust the limit for all. Switch to per-key limits.
  • Changes not taking effect: Some gateways cache plugin configurations. Reload the gateway or wait a few seconds. In Kong, you may need to restart the node or use the admin API to trigger a reload.

Frequently Asked Questions

Can I set a daily token quota per team? Yes, most gateways allow you to set a limit per hour or per day. If your gateway doesn't support token counting natively, you can use a custom plugin or approximate tokens by character count (e.g., 4 chars ≈ 1 token).

What's the difference between rate limit and quota? A rate limit controls the speed of requests (e.g., 10 requests per second). A quota controls the total usage over a period (e.g., 1 million tokens per day). Both are often used together.

How do I handle a team that needs a temporary increase? Most gateways allow you to override the limit for a specific consumer or key. You can temporarily increase the limit in the admin console, then revert it after the event.

What happens when a team hits the limit? The gateway returns a 429 status code with a message. You can customize the response body to include instructions (e.g., 'Contact admin for a quota increase').

Can I set different limits for different models? Yes, if your gateway routes to different models via different paths or headers, you can apply different rate limit policies to each route.

Do I need to set limits on both the gateway and the provider? It's recommended. The provider's limits are the hard cap, but the gateway lets you manage per-team quotas and provide a consistent error format.