Repository navigation
feat: token-bucket rate limiting and concurrent-answer limits - #28
Merged
Merged
Conversation
Replaces the fixed one-minute rate-limit window with a token bucket (continuous refill, same capacity as the configured/plan-aware limit) -- closes the old window's double-burst-across-a-boundary gap. Also adds a concurrency cap on in-flight /v1/literature/answers calls, enforced per key and per organization the same way the rate limiter already is, independent of (and alongside) request-frequency limiting. Both close the two remaining bullets of design audit gap #7. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.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.
Summary
Closes the last two bullets of design-audit gap #7:
V1Store.hit_rate_limitnow models a continuously-refilling bucket (capacity = the configured/plan-aware limit, refill rate = limit/60 tokens/sec) instead of a hard reset at each minute boundary -- the old window let a caller spend its full budget in the last second of one window and again in the first second of the next, for up to 2x the limit in under two seconds. Not atomic against a concurrent request for the same subject on a real Redis backend (plain GET/SET, no Lua script) -- a deliberate, documented, bounded tradeoff; nothing here is billing-critical the wayreserve_quota's atomic DECR had to be.V1Store.acquire_concurrency_slot/release_concurrency_slotpair caps in-flight/v1/literature/answerscalls per key and per organization (V1_MAX_CONCURRENT_ANSWERS, default 5, operator-tunable -- an overload-protection default, not a per-plan number). Enforced only on/answers(the expensive, LLM-invoking call the gap specifically names), not/search.No
omnibioai-billingchange needed -- unlike the rate limit's plan-aware override, concurrency has no per-plan column; this is gateway-only overload protection.Test plan
pytest tests/test_v1_literature.py-- 56 passed (49 existing + 7 new: token-bucket refill behavior, limit-of-zero edge case, concurrency-cap denial for both key and org, slot release on success/quota-denial, and confirming/searchhas no concurrency cap)🤖 Generated with Claude Code