Asynchronous Token Bucket Rate Limiter#35
Open
essiebx wants to merge 2 commits into
Open
Conversation
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.
Summary
This PR introduces an optional asynchronous token bucket rate limiter to smooth bursty LLM traffic and reduce request-rate-based 429 Too Many Requests errors.
The limiter is applied at a single choke point (
BaseModel._post_with_retries) and is provider-agnostic across OpenAI, Anthropic, and OpenRouter.When disabled (
throttle_rate: 0.0, default), the implementation introduces no behavioral changes and zero runtime overhead.Changes
1. AsyncTokenBucket Implementation
Location:
src/webwright/utils/throttle.pyProvides an async-safe token bucket rate limiter that:
asyncio.sleepPurpose: Prevent burst traffic from reaching external APIs before rate limiting is applied.
2. Process-Global Keyed Registry
Introduced a registry mapping:
Purpose:
3. Configuration Updates
Added the following fields to
BaseModelConfig:throttle_rate(requests per second)throttle_capacity(burst size)Default:
throttle_rate: 0.0Purpose: Fully opt-in behavior with no overhead when disabled.
4. Integration into Request Pipeline
Throttling is enforced inside:
Before each external API request:
Purpose: Ensures all provider requests pass through a single unified rate-limiting choke point.
5. Test Coverage
Location:
tests/unit/test_throttle.pyAdded 15 unit tests covering:
asyncio.Lock)Backward Compatibility
No breaking changes.
Scope
This PR addresses request-rate-based throttling only.
It does not handle API quota exhaustion errors (
insufficient_quota), which are unrelated to rate limiting logic.