FastAPI Dependency Injection Offers Per-Tenant API Key and Rate Limit Isolation
A developer at CitizenApp identified a critical scalability problem when their platform grew to 15 tenants sharing a single Anthropic Claude API key, where one tenant's heavy usage could throttle all others. The lack of per-tenant rate limiting forced reliance on fragile middleware workarounds that caused context leakage and debugging difficulties, especially with background tasks. The proposed solution leverages FastAPI's built-in dependency injection system to create isolated, per-request Claude clients and rate-limit buckets for each tenant. Using FastAPI's Depends mechanism, tenant credentials are resolved cleanly from JWT tokens, with each request fetching its own tenant configuration and enforcing its own rate limits. The approach eliminates global state and thread-safety concerns, with the author recommending Redis-backed rate limiting for distributed production deployments.
This is an AI-generated summary. ShortSingh links to the original source for the complete article.
Discussion (0)
Log in to join the discussion and vote.
Log in