Quick Start
How It Works
The rate limiter uses a two-phase approach:| Phase | Description | Benefits |
|---|---|---|
| Reserve | Under global lock: check tokens, reserve capacity, update channel tracking | Thread-safe bookkeeping |
| Sleep | Outside lock: actual delay based on computed wait time | Multiple channels can sleep concurrently |
Configuration Options
| Option | Type | Default | Description |
|---|---|---|---|
messages_per_second | float | 1.0 | Token refill rate for the global token bucket. |
burst_size | int | 5 | Max tokens that can accumulate (burst capacity). |
per_channel_delay | float | 1.0 | Minimum seconds between two sends to the same channel. |
Platform Presets
| Platform | messages_per_second | burst_size | per_channel_delay | Notes |
|---|---|---|---|---|
| Telegram | 25.0 | 30 | 0.05 | ~30 msg/sec to different users |
| Discord | 1.0 | 5 | 1.0 | 5 messages per 5 seconds per channel |
| Slack | 1.0 | 1 | 1.0 | 1 message per second per channel |
| 50.0 | 80 | 0.1 | ~80 msg/sec Cloud API limit |
Memory Management
Per-channel state is tracked in an LRU cache capped at 4096 channels. If a bot serves more channels than that, the least-recently-used channels fall out of the cache and theirper_channel_delay window resets. This bounds memory for long-running bots.
Concurrency Design
The global lock is held only long enough to reserve a token and update bookkeeping — the actual sleep happens outside the lock. Multiple channels can be rate-limited concurrently without serialising on one mutex. Before PR #1870 (serialized):Best Practices
Use Platform Presets
Use Platform Presets
Start with
RateLimiter.for_platform() instead of custom configs. Platform presets are tuned for each API’s documented limits and real-world behavior.Share Limiters Across Bot Instances
Share Limiters Across Bot Instances
Monitor Rate Limit Logs
Monitor Rate Limit Logs
The rate limiter logs debug messages when applying delays. Monitor these to tune your configuration.
Handle Platform-Specific Burst Patterns
Handle Platform-Specific Burst Patterns
Some platforms allow bursts followed by longer delays. The
burst_size parameter accommodates this pattern.Related
Rate Limiter (LLM)
Rate limiting for LLM API calls (different from bot message rate limiting)
Messaging Bots
Build bots for Telegram, Discord, Slack, and WhatsApp platforms

