Skip to main content

Redis-based distributed RateLimiter for LangChain with RPM/RPS support

Project description

LangChain Redis Rate Limiter

English | 한국어

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

langchain_redis_rate_limiter-0.1.0.tar.gz (9.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

langchain_redis_rate_limiter-0.1.0-py3-none-any.whl (8.7 kB view details)

Uploaded Python 3

File details

Details for the file langchain_redis_rate_limiter-0.1.0.tar.gz.

File metadata

  • Download URL: langchain_redis_rate_limiter-0.1.0.tar.gz
  • Upload date:
  • Size: 9.2 kB
  • Tags: Source
  • Uploaded using 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}

File hashes

Hashes for langchain_redis_rate_limiter-0.1.0.tar.gz
Algorithm Hash digest
SHA256 4ccbb659ef9b48127e3bbf613685abd43a67c20d9091f4cb7e9f88267e3a4faf
MD5 f7d9096f9a1360eff175bb40bed741ca
BLAKE2b-256 bbbf0799ec9a1e315edbe5f006c642d1775ef346c8f7fa054647889d10525f1a

See more details on using hashes here.

File details

Details for the file langchain_redis_rate_limiter-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: langchain_redis_rate_limiter-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 8.7 kB
  • Tags: Python 3
  • Uploaded using 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}

File hashes

Hashes for langchain_redis_rate_limiter-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 30b5463a0db400a8ee14a4534e121118a904532e63fd1e9f1103a7e93fdda6fe
MD5 07211f7865dfa249534c1ef2d8c4148c
BLAKE2b-256 a8f38dd88dcf0bc32aaee1eb0de4105ae03519822cb5d9cd44a8dbf654057953

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page