# Limits and rate limits

> How many requests a bot can make and how Klang tells it to slow down, the gateway’s limits, and the product limits of the beta, such as 25 bots per server and 100 servers per bot until it’s verified.

## REST rate limits

Klang limits requests per bot with buckets that refill a little at a time. Every response from a
limited route says where its bucket stands, in the headers libraries already read:

| Header                    | What it says                                                          |
| ------------------------- | --------------------------------------------------------------------- |
| `X-RateLimit-Limit`       | How many requests the bucket holds.                                   |
| `X-RateLimit-Remaining`   | How many are left right now.                                          |
| `X-RateLimit-Reset`       | When to look again, in Unix seconds with decimals.                    |
| `X-RateLimit-Reset-After` | The same, as seconds from now with decimals.                          |
| `X-RateLimit-Bucket`      | An opaque ID of the bucket. Routes that share a bucket share this ID. |

Because buckets refill gradually, `Reset-After` means two things: with requests left, how long
until the bucket is full; with none left, how long until the next request fits.

These are the limits per bot:

| What                                                                                                                 | Limit                             |
| -------------------------------------------------------------------------------------------------------------------- | --------------------------------- |
| Every request (global)                                                                                               | 50 per second                     |
| Sending messages                                                                                                     | 5 every 5 seconds per channel     |
| Editing and deleting messages                                                                                        | 10 every 10 seconds               |
| Adding reactions                                                                                                     | 10 every 10 seconds               |
| “Is typing”                                                                                                          | 5 every 10 seconds per channel    |
| Opening a new direct message                                                                                         | 10 every 10 minutes               |
| Listing a server’s members                                                                                           | 10 every 10 seconds               |
| Listing a channel’s messages                                                                                         | 10 every 5 seconds                |
| Getting one message or one channel                                                                                   | 50 every 10 seconds               |
| Getting a server, its channels or its roles, and `/users/@me/guilds`                                                 | 10 every 10 seconds               |
| Looking someone up (`/users/{user.id}`, `/guilds/{guild.id}/members/{user.id}`, `/guilds/{guild.id}/bans/{user.id}`) | 20 every 5 seconds                |
| Listing a server’s bans                                                                                              | 10 every 10 seconds               |
| Deleting messages in bulk                                                                                            | 5 every 10 seconds                |
| Changing nicknames                                                                                                   | 20 every 10 minutes per server    |
| Timing out and lifting timeouts                                                                                      | 30 every 10 minutes               |
| Reading commands and their permissions                                                                               | 20 every 10 seconds               |
| Creating, replacing, editing and deleting commands, global and per server together                                   | 20 every 20 seconds               |
| Follow-ups and edits with an interaction’s token                                                                     | 5 every 2 seconds per interaction |
| Changing, ordering and deleting channels and their overwrites                                                        | 20 every 10 seconds per server    |
| Creating, changing, ordering and deleting roles                                                                      | 20 every 10 seconds per server    |
| Creating channels (shared with the people in the server)                                                             | 20 every 10 minutes per server    |
| Creating invites (shared with the people in the server)                                                              | 10 every 10 minutes per server    |
| Looking up an invite by its code                                                                                     | 60 every minute                   |
| Listing invites                                                                                                      | 10 every 10 seconds               |
| Reading the audit log                                                                                                | 10 every 10 seconds               |
| Starting threads                                                                                                     | 10 every 10 minutes               |
| Changing, deleting, joining and leaving threads                                                                      | 30 every minute                   |
| Listing a channel’s archived threads                                                                                 | 30 every minute                   |
| Listing who voted in a poll                                                                                          | 30 every minute                   |
| Creating, changing and deleting emojis (shared with the people in the server)                                        | 30 every 10 minutes per server    |
| Creating, changing and deleting stickers (shared with the people in the server)                                      | 30 every 10 minutes per server    |
| Creating, changing and deleting AutoMod rules (shared with the people in the server)                                 | 30 every 10 minutes per server    |
| Listing a channel’s or a server’s webhooks, and reading one                                                          | 10 every 10 seconds               |
| Creating, changing and deleting webhooks                                                                             | 20 every 10 minutes               |

The reads’ limits go per bot, whichever server or channel you read: a bot in many servers shares
one bucket for each kind of read. Routes not in the table, such as the bot’s own user, its
application, `/gateway/bot`, kicking, banning and giving roles, only count for the global limit.
The global limit doesn’t send headers until you go over it. Answering an interaction doesn’t
count against the global limit: neither the callback nor the follow-ups and edits with the
interaction’s token, which have their own bucket per interaction. Requests without a valid token, such
as `GET /gateway`, have the same global limit per IP address. The
[bot API reference](https://docs.klang.gg/bots/reference/#rate-limits) says which bucket each route uses.

## When you go over

Klang answers `429 Too Many Requests`, with the wait in the body and in the headers:

```json
{
  "message": "You’re sending requests too quickly.",
  "retry_after": 1.25,
  "global": false
}
```

- `retry_after` is in seconds, with decimals. The `Retry-After` header has the same wait,
  rounded up to whole seconds.
- `X-RateLimit-Scope` is `user` for a route’s bucket and `global` for the global limit, which
  also sends `X-RateLimit-Global: true`. It’s `shared` when a bot already has two uploads of
  [files](https://docs.klang.gg/bots/files/) going: that one doesn’t count as an invalid request.

Libraries wait and retry on their own. If you write requests by hand, wait `retry_after` seconds
before the next one to the same bucket.

**Slow mode** applies to bots too. Writing again too soon in a channel with slow mode answers a
`429` with code `20016` and its own `retry_after`. A bot with **Manage messages** or **Manage
channels** there skips it.

**The command limits** answer `400`, not `429`, because waiting doesn’t help: `30032` when a scope
already has all the commands it can hold, and `30034` when 200 new commands were created in a
server today (UTC). See [Slash commands](https://docs.klang.gg/bots/commands/#limits).

## Invalid requests

Every response with status `401`, `403` or `429` counts against the IP address that made it.
After **10,000 in 10 minutes**, every request from that address gets a `429` for the next 10
minutes. A bot that keeps retrying with a reset token, or keeps reading a channel it can’t see,
reaches it sooner than it seems: stop on a `401`, and don’t retry a `403` without changing
something first.

## Gateway

| What                            | Limit                                                           | When you go over                                                                      |
| ------------------------------- | --------------------------------------------------------------- | ------------------------------------------------------------------------------------- |
| Frames you send, per connection | 120 every 60 seconds, heartbeats included                       | Closed with `4008`                                                                    |
| **Identify**, per app           | 1 every 5 seconds, and 1,000 per day (UTC)                      | Closed with `4008`                                                                    |
| Connections at once, per app    | 2                                                               | A third one closes the oldest with `4009`                                             |
| Presence updates, per session   | 5 every 20 seconds                                              | The extra ones are ignored                                                            |
| Shards                          | 1                                                               | Another `shard` closes with `4010`                                                    |
| Resume                          | The last 2,000 events and at most 5 MB, for about 3 minutes     | **Invalid Session**                                                                   |
| Resumes, per app                | 20 a minute                                                     | **Invalid Session**                                                                   |
| Failed logins, per IP address   | 1,000 identifies or resumes closed with `4004` every 10 minutes | That address is turned away for 10 minutes: `429` when connecting, `4008` on identify |

`GET /api/v10/gateway/bot` says how many identifies your app has left today in
`session_start_limit`, and when the count starts again. `max_concurrency` is always 1. There’s no
limit of connections per IP address: it goes per app. Only failed logins count per address, so a
bot with a reset token that restarts in a loop should stop on `4004` instead.

If a bot is away long enough for more than 2,000 events or 5 MB to pile up, it can’t resume and
gets **Invalid Session** with `false`: it identifies again, as libraries do on their own.

## Product limits

| What                                            | Limit                                                                                                         |
| ----------------------------------------------- | ------------------------------------------------------------------------------------------------------------- |
| Apps per developer                              | 10, during the beta                                                                                           |
| Bots per server                                 | 25, on every plan                                                                                             |
| Servers per bot                                 | 100; 2,500 once [verified](https://docs.klang.gg/bots/getting-started/#verify-your-bot)                                            |
| Webhooks                                        | 15 per channel and 50 per server: see [Webhooks](https://docs.klang.gg/bots/reference/webhooks/)                                   |
| A sticker’s file                                | 512 KB and 320 × 320 pixels: see [Create a sticker](https://docs.klang.gg/bots/reference/server-content/#create-a-sticker)         |
| Message text                                    | 4,000 characters                                                                                              |
| Embeds per message                              | 10, with the limits in [Embeds](https://docs.klang.gg/webhooks/embeds/)                                                            |
| Stickers per message                            | 1                                                                                                             |
| Files per message                               | 10, up to the server’s plan: see [Sending files](https://docs.klang.gg/bots/files/#limits)                                         |
| Messages per page                               | Up to 100 (50 by default)                                                                                     |
| Servers per page (`/users/@me/guilds`)          | Up to 200                                                                                                     |
| Members per page (`/guilds/{guild.id}/members`) | Up to 1,000 (1 by default)                                                                                    |
| Token resets                                    | 5 per day per app                                                                                             |
| Slash commands, per scope (global or a server)  | 100                                                                                                           |
| User and message commands, per scope            | 15 of each                                                                                                    |
| New commands per server and day (UTC), per app  | 200                                                                                                           |
| Options per command, choices per option         | 25                                                                                                            |
| First answer to an interaction                  | 3 seconds                                                                                                     |
| An interaction’s token                          | 15 minutes                                                                                                    |
| Autocomplete suggestions                        | 25                                                                                                            |
| Components per message                          | 5 action rows, or 40 components with `IS_COMPONENTS_V2`: see [Components](https://docs.klang.gg/bots/reference/components/#limits) |
| Fields per modal                                | 5                                                                                                             |
| Files per file upload in a modal                | 10, with the limits of the person who uploads them: see [Modals](https://docs.klang.gg/bots/modals/#files-choices-and-checkboxes)  |

The privileged intents are free while a bot is in 100 servers or fewer. To keep them above that,
explain what your bot uses them for when you
[ask for verification](https://docs.klang.gg/bots/getting-started/#verify-your-bot): at the 101st server, the ones the
team hasn’t reviewed are switched off ([Above 100 servers](https://docs.klang.gg/bots/getting-started/#above-100-servers)). Bots are free on every Klang plan.
