feat: add per-namespace agent rate limiting#532
Open
ashike24 wants to merge 8 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
Adds per-namespace rate limiting to all agent-triggering endpoints in the FinBot vendor portal, protecting the shared LLM quota from exhaustion and ensuring fair access across namespaces.
Motivation
Without rate limiting, a single namespace generating a burst of requests could consume the entire available LLM quota, degrading the experience for every other user of the platform. This change caps the number of agent-invoking requests per namespace within a fixed time window.
Changes
finbot/config.pyAGENT_RATE_LIMIT_MAX(default: 10) andAGENT_RATE_LIMIT_WINDOW_SECONDS(default: 60) to theSettingsclass with full env var override support.finbot/core/ratelimit/limiter.py(new)finbot:ratelimit:{namespace}:agent.event_bus.redisasync client — no second Redis connection.HTTP 429with a descriptive message including current count, max, and TTL remaining.finbot/core/ratelimit/__init__.py(new)finbot/apps/vendor/routes/api.pyDepends(check_agent_rate_limit)to 5 agent-triggering routes:POST /vendors/register,POST /vendors/{vendor_id}/request-review,POST /invoices,POST /invoices/{invoice_id}/reprocess, andPOST /chat.Test Results
10 integration tests added in
tests/integration/test_rate_limiting.py, all passing. No regressions in the existing suite (382 passed, 6 pre-existing failures unchanged).