# Control AI Costs with Token-Based Rate Limiting

This guide covers how to implement token-based rate limiting for LLM traffic using the [`ai-rate-limiting`](https://docs.api7.ai/hub/ai-rate-limiting.md) plugin. You will learn how to set token budgets per route and per model instance.

## Overview[​](#overview "Direct link to Overview")

Traditional request-based rate limiting is insufficient for LLM traffic — a single request can consume anywhere from 10 to 100,000 tokens depending on the prompt and response. Token-based rate limiting lets you control costs by budgeting based on actual token consumption.

## Prerequisites[​](#prerequisites "Direct link to Prerequisites")

* Install [Docker](https://docs.docker.com/get-docker/).

* Install [cURL](https://curl.se/) to send requests to the services for validation.

* Have a running API7 Gateway instance.

* Create a [token from the Dashboard](https://docs.api7.ai/api7-gateway/reference/obtain-dashboard-token.md) and save it to an environment variable:

  ```
  export API_KEY=your-dashboard-token   # replace with your Dashboard token
  ```

* Replace `{gateway_group_id}` with your gateway group ID. Use `default` if you are following the quickstart.

* If you are following the Admin API examples, create or reuse a service in API7 Gateway. If you do not have one yet, follow [Create or Reuse a Service](https://docs.api7.ai/api7-gateway/ai-gateway/get-started.md#create-or-reuse-a-service), then save its ID to an environment variable:

  ```
  export SERVICE_ID=your-service-id         # replace with your service ID
  ```

## Token-Based vs. Request-Based Rate Limiting[​](#token-based-vs-request-based-rate-limiting "Direct link to Token-Based vs. Request-Based Rate Limiting")

| Aspect           | Request-Based                                     | Token-Based                                     |
| ---------------- | ------------------------------------------------- | ----------------------------------------------- |
| **Unit**         | Number of HTTP requests                           | Number of LLM tokens consumed                   |
| **Precision**    | Coarse — all requests treated equally             | Fine — limits match actual resource consumption |
| **Cost control** | Weak — a few verbose requests can exhaust budgets | Strong — directly tied to provider billing      |
| **Best for**     | Traditional APIs                                  | LLM traffic                                     |

## Configure Token Rate Limits[​](#configure-token-rate-limits "Direct link to Configure Token Rate Limits")

* Admin API
* ADC

```
curl -k "https://localhost:7443/apisix/admin/routes/ai-rate-limited?gateway_group_id={gateway_group_id}" -X PUT \

  -H "X-API-KEY: ${API_KEY}" \

  -H "Content-Type: application/json" \

  --data-binary @- <<EOF

  {

    "name": "ai-rate-limited",

    "service_id": "$SERVICE_ID",

    "paths": ["/ai/token-limit"],

    "plugins": {

      "ai-proxy": {

        "provider": "openai",

        "auth": { "header": { "Authorization": "Bearer $OPENAI_API_KEY" } },

        "options": { "model": "gpt-4o" }

      },

      "ai-rate-limiting": {

        "policy": "local",

        "limit_strategy": "total_tokens",

        "limit": 10000,

        "time_window": 3600

      }

    }

  }

EOF
```

❶ Count total tokens (prompt + completion). Other options: `"prompt_tokens"` or `"completion_tokens"`.

❷ Allow 10,000 tokens per time window.

❸ Reset the limit every 3,600 seconds (1 hour).

adc.yaml

```
services:

  - name: AI Rate Limited

    routes:

      - uris:

          - /ai/token-limit

        name: ai-rate-limited

        plugins:

          ai-proxy:

            provider: openai

            auth:

              header:

                Authorization: "Bearer ${OPENAI_API_KEY}"

            options:

              model: gpt-4o

          ai-rate-limiting:

            policy: local

            limit_strategy: total_tokens

            limit: 10000

            time_window: 3600

    labels:

      docs-example: ai-rate-limited
```

❶ Count total tokens (prompt + completion). Other options: `"prompt_tokens"` or `"completion_tokens"`.

❷ Allow 10,000 tokens per time window.

❸ Reset the limit every 3,600 seconds (1 hour).

Preview the changes owned by this example and confirm that the diff contains no unintended updates or deletions:

```
adc diff -f adc.yaml \

  --include-resource-type service \

  --label-selector docs-example=ai-rate-limited
```

Synchronize the reviewed configuration:

```
adc sync -f adc.yaml \

  --include-resource-type service \

  --label-selector docs-example=ai-rate-limited
```

When the limit is exceeded, the gateway returns HTTP 503 by default with rate limit headers:

* `X-AI-RateLimit-Limit-{name}` — The configured token limit.
* `X-AI-RateLimit-Remaining-{name}` — Tokens remaining in the current window.
* `X-AI-RateLimit-Reset-{name}` — Seconds until the window resets.

## Per-Instance Rate Limits (Multi-Model)[​](#per-instance-rate-limits-multi-model "Direct link to Per-Instance Rate Limits (Multi-Model)")

When using [`ai-proxy-multi`](https://docs.api7.ai/hub/ai-proxy-multi.md), set per-instance token budgets to limit expensive models more aggressively:

* Admin API
* ADC

```
curl -k "https://localhost:7443/apisix/admin/routes/ai-per-instance-limits?gateway_group_id={gateway_group_id}" -X PUT \

  -H "X-API-KEY: ${API_KEY}" \

  -H "Content-Type: application/json" \

  --data-binary @- <<EOF

  {

    "service_id": "$SERVICE_ID",

    "name": "ai-per-instance-limits",

    "paths": ["/ai/per-instance-limit"],

    "plugins": {

      "ai-proxy-multi": {

        "instances": [

          {

            "name": "gpt-4o-mini",

            "provider": "openai",

            "auth": { "header": { "Authorization": "Bearer $OPENAI_API_KEY" } },

            "options": { "model": "gpt-4o-mini" },

            "weight": 1

          },

          {

            "name": "gpt-4o",

            "provider": "openai",

            "auth": { "header": { "Authorization": "Bearer $OPENAI_API_KEY" } },

            "options": { "model": "gpt-4o" },

            "weight": 1

          }

        ]

      },

      "ai-rate-limiting": {

        "policy": "local",

        "limit_strategy": "total_tokens",

        "instances": [

          {

            "name": "gpt-4o",

            "limit": 5000,

            "time_window": 3600

          }

        ]

      }

    }

  }

EOF
```

❶ Apply the rate limit only to the `gpt-4o` instance with its own `limit` and `time_window`. Traffic to `gpt-4o-mini` is not affected by this limit.

adc.yaml

```
services:

  - name: AI Per-Instance Limits

    routes:

      - uris:

          - /ai/per-instance-limit

        name: ai-per-instance-limits

        plugins:

          ai-proxy-multi:

            instances:

              - name: gpt-4o-mini

                provider: openai

                auth:

                  header:

                    Authorization: "Bearer ${OPENAI_API_KEY}"

                options:

                  model: gpt-4o-mini

                weight: 1

              - name: gpt-4o

                provider: openai

                auth:

                  header:

                    Authorization: "Bearer ${OPENAI_API_KEY}"

                options:

                  model: gpt-4o

                weight: 1

          ai-rate-limiting:

            policy: local

            limit_strategy: total_tokens

            instances:

              - name: gpt-4o

                limit: 5000

                time_window: 3600

    labels:

      docs-example: ai-per-instance-limits
```

❶ Apply the rate limit only to the `gpt-4o` instance with its own `limit` and `time_window`. Traffic to `gpt-4o-mini` is not affected by this limit.

Preview the changes owned by this example and confirm that the diff contains no unintended updates or deletions:

```
adc diff -f adc.yaml \

  --include-resource-type service \

  --label-selector docs-example=ai-per-instance-limits
```

Synchronize the reviewed configuration:

```
adc sync -f adc.yaml \

  --include-resource-type service \

  --label-selector docs-example=ai-per-instance-limits
```

## Scaling with Redis[​](#scaling-with-redis "Direct link to Scaling with Redis")

For multi-instance gateway deployments, use Redis to share rate limit counters across Data Plane nodes:

* Admin API
* ADC

```
curl -k "https://localhost:7443/apisix/admin/routes/ai-rate-redis?gateway_group_id={gateway_group_id}" -X PUT \

  -H "X-API-KEY: ${API_KEY}" \

  -H "Content-Type: application/json" \

  --data-binary @- <<EOF

  {

    "name": "ai-rate-redis",

    "service_id": "$SERVICE_ID",

    "paths": ["/ai/redis-limit"],

    "plugins": {

      "ai-proxy": {

        "provider": "openai",

        "auth": { "header": { "Authorization": "Bearer $OPENAI_API_KEY" } },

        "options": { "model": "gpt-4o" }

      },

      "ai-rate-limiting": {

        "limit_strategy": "total_tokens",

        "limit": 100000,

        "time_window": 3600,

        "policy": "redis",

        "redis_host": "redis.example.com",

        "redis_port": 6379,

        "allow_degradation": true

      }

    }

  }

EOF
```

❶ Set the policy to `redis` for shared counters. Other options: `"redis-cluster"`, `"redis-sentinel"`.

❷ Configure the Redis connection.

❸ When set to `true`, the gateway continues serving requests if Redis is unavailable (fail-open).

adc.yaml

```
services:

  - name: AI Rate Redis

    routes:

      - uris:

          - /ai/redis-limit

        name: ai-rate-redis

        plugins:

          ai-proxy:

            provider: openai

            auth:

              header:

                Authorization: "Bearer ${OPENAI_API_KEY}"

            options:

              model: gpt-4o

          ai-rate-limiting:

            limit_strategy: total_tokens

            limit: 100000

            time_window: 3600

            policy: redis

            redis_host: redis.example.com

            redis_port: 6379

            allow_degradation: true

    labels:

      docs-example: ai-rate-redis
```

❶ Set the policy to `redis` for shared counters. Other options: `"redis-cluster"`, `"redis-sentinel"`.

❷ Configure the Redis connection.

❸ When set to `true`, the gateway continues serving requests if Redis is unavailable (fail-open).

Preview the changes owned by this example and confirm that the diff contains no unintended updates or deletions:

```
adc diff -f adc.yaml \

  --include-resource-type service \

  --label-selector docs-example=ai-rate-redis
```

Synchronize the reviewed configuration:

```
adc sync -f adc.yaml \

  --include-resource-type service \

  --label-selector docs-example=ai-rate-redis
```

## Verify[​](#verify "Direct link to Verify")

Send requests to the Redis-backed route until its token limit is reached. The other examples use `/ai/token-limit` and `/ai/per-instance-limit`.

```
curl "http://127.0.0.1:9080/ai/redis-limit" -X POST \

  -H "Content-Type: application/json" \

  -d '{

    "messages": [

      { "role": "user", "content": "Write a 500-word essay about API gateways." }

    ]

  }'
```

After exceeding the token limit, the gateway returns:

```
HTTP/1.1 503 Service Unavailable
```

## Next Steps[​](#next-steps "Direct link to Next Steps")

* [AI Observability and Cost Tracking](https://docs.api7.ai/api7-gateway/ai-gateway/use-cases/ai-observability-and-cost-tracking.md) — Monitor token consumption and build cost dashboards.
* [Multi-LLM Routing and Fallback](https://docs.api7.ai/api7-gateway/ai-gateway/use-cases/multi-llm-routing-and-fallback.md) — Combine rate limiting with multi-model routing.
* For the full configuration reference, see [`ai-rate-limiting`](https://docs.api7.ai/hub/ai-rate-limiting.md).
