Add Production-Grade Rate Limiting to Prevent API Throttling#14
Open
sobithav18 wants to merge 3 commits intodigitalocean:mainfrom
Open
Add Production-Grade Rate Limiting to Prevent API Throttling#14sobithav18 wants to merge 3 commits intodigitalocean:mainfrom
sobithav18 wants to merge 3 commits intodigitalocean:mainfrom
Conversation
There was a problem hiding this comment.
Pull Request Overview
This PR adds production-grade rate limiting to the ChatGradient client to prevent 429 API throttling errors. The implementation uses a thread-safe token bucket algorithm with a sliding window and is fully backwards compatible (opt-in feature).
Key Changes:
- New
RateLimiterclass with sliding window token bucket algorithm - Integration with
ChatGradientviaenable_rate_limitingandmax_requests_per_minuteparameters - Comprehensive test coverage for unit, integration, and real-world scenarios
Reviewed Changes
Copilot reviewed 14 out of 15 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
langchain_gradient/rate_limiter.py |
New rate limiter implementation with thread-safe token bucket algorithm |
langchain_gradient/chat_models.py |
Integration of rate limiting into ChatGradient's _generate and _stream methods |
langchain_gradient/constants.py |
Added rate limiting parameters to allowed model fields |
tests/unit_tests/test_rate_limiter.py |
Unit tests for RateLimiter class |
tests/unit_tests/test_chat_models_with_rate_limiting.py |
Unit tests for ChatGradient rate limiting integration |
tests/integration_tests/test_rate_limiting.py |
Integration tests for rate limiter |
tests/integration_tests/test_chat_models*.py |
Code formatting improvements (import reordering, trailing comma fixes) |
pyproject.toml |
Added pytest to dev dependencies |
examples/rate_limiting_demo.py |
Comprehensive demonstration of rate limiting features |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
#issue 13, hacktoberfest
This PR adds a thread-safe, configurable rate limiter to ChatGradient to prevent 429 errors during rapid API calls. It uses a token bucket algorithm with a sliding window and supports concurrent requests. The feature is opt-in, fully backwards compatible, and includes unit and integration tests. Users can set custom limits, monitor usage, and handle bursts efficiently. All tests pass locally, and the implementation has no external dependencies. Maintainers with API access are asked to verify real-world behavior.