LangChain Redis Rate Limiter
A Redis-based RateLimiter for LangChain, compatible with BaseRateLimiter. It supports both Requests Per Minute (RPM) and Requests Per Second (RPS) limiting, designed to work seamlessly in distributed environments.
Features
- Redis Support: Compatible with Local Redis, AWS ElastiCache.
- LangChain Integration: Inherits from
BaseRateLimiter, allowing direct usage with LangChain models. - Async Support: Supports both sync and async request.
- Distributed Support: Uses Redis to manage shared status across multiple servers or processes.
Installation
pip install langchain-redis-rate-limiter
Usage
Direct Model Integration (Recommended)
You can pass the RedisRateLimiter directly to LangChain models that support the rate_limiter parameter (e.g., ChatOpenAI, ChatAnthropic, ChatGoogleGenerativeAI).
from langchain_redis_rate_limiter import RedisRateLimiter
from langchain_openai import ChatOpenAI
# 1 request per second (60 RPM)
limiter = RedisRateLimiter(
redis_url="redis://localhost:6379",
requests_per_second=1,
check_every_n_seconds=0.1,
max_bucket_size=1
)
model = ChatOpenAI(
model="gpt-4o",
rate_limiter=limiter
)
model.invoke("Hello world")
Parameters
- redis_url: Redis connection URL (e.g., redis://localhost:6379).
- key_prefix: Prefix for Redis keys (default: langchain_limiter). Use different prefixes for different limit rules.
- requests_per_second: The maximum number of requests allowed per second. For example, setting this to 5 means you can make up to five requests each second.
- check_every_n_seconds: When the limit is reached and a request needs to wait, this value determines how often the system checks again to see whether a new request is allowed. Example: 0.1 checks every 0.1 seconds.
- max_bucket_size: Controls how many requests can be handled in a short burst. A larger value allows brief spikes in traffic before the limiter slows things down.
License
Apache-2.0 license
Metadata
Release files for langchain-redis-rate-limiter 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| langchain_redis_rate_limiter-0.1.0.tar.gz | 9.2 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| langchain_redis_rate_limiter-0.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 17.9 kB
Release files / langchain_redis_rate_limiter-0.1.0.tar.gz
| Download URL | langchain_redis_rate_limiter-0.1.0.tar.gz |
|---|---|
| Size | 9.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
4ccbb659ef9b48127e3bbf613685abd43a67c20d9091f4cb7e9f88267e3a4faf
|
|
BLAKE2b-256 checksum How to use checksums |
bbbf0799ec9a1e315edbe5f006c642d1775ef346c8f7fa054647889d10525f1a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.9.11 {"installer":{"name":"uv","version":"0.9.11"},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
|
Release files / langchain_redis_rate_limiter-0.1.0-py3-none-any.whl
| Download URL | langchain_redis_rate_limiter-0.1.0-py3-none-any.whl |
|---|---|
| Size | 8.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
30b5463a0db400a8ee14a4534e121118a904532e63fd1e9f1103a7e93fdda6fe
|
|
BLAKE2b-256 checksum How to use checksums |
a8f38dd88dcf0bc32aaee1eb0de4105ae03519822cb5d9cd44a8dbf654057953
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.9.11 {"installer":{"name":"uv","version":"0.9.11"},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
|