{
  "id": 972123,
  "title": "FastAPI Dependency Injection for Anthropic Claude: Isolating API Keys and Rate Limits Per Tenant",
  "url": "https://urgent.news/2026/08/15/fastapi-dependency-injection-for-anthropic-claude-isolating-api-keys",
  "topic": "ai",
  "section": "AI",
  "published": "2026-08-15T06:02:17.000Z",
  "source": {
    "name": "Dev.to",
    "slug": "dev-to",
    "url": "https://dev.to/uaslimcreate/fastapi-dependency-injection-for-anthropic-claude-isolating-api-keys-and-rate-limits-per-tenant-4n7j"
  },
  "original_language": "en",
  "account": "FastAPI Dependency Injection for Anthropic Claude: Isolating API Keys and Rate Limits Per Tenant When CitizenApp reached the milestone of 15 tenants, the team quickly discovered that relying on a single global Claude API key posed significant challenges. A single customer's aggressive usage of the API key could inadvertently throttle the performance of all other tenants. Furthermore, enforcing per-tenant rate limits proved to be a complex and error-prone task without proper isolation.\n\nThe solution lay in harnessing FastAPI's powerful dependency injection system to create tenant-specific Claude clients and rate-limit buckets. This approach eliminated the need for global variables, thread locks, and intricate debugging procedures. By leveraging FastAPI's `Depends` system, the team could cleanly encapsulate the dependencies required for each request, allowing handlers to focus solely on their core functionality.\n\nTo implement this solution, the team first defined a `Tenant` model in their database schema, which included fields for the tenant's name, Anthropic API key, and configurable rate limits. They then created an `RateLimitBucket` class to manage in-memory rate limiting logic, which could be easily adapted for distributed deployments using a Redis backend.\n\nNext, they defined custom dependency providers in `dependencies.py` to handle the retrieval of tenant-specific data. The `get_db` dependency fetched a database session for use within the handler, while the `get_tenant_id` dependency extracted and validated the tenant ID from the request's JWT or API key header. If the authentication failed, an HTTP 401 Unauthorized exception was raised.\n\nThe `get_tenant` dependency further refined the process by combining the previously defined dependencies. It extracted the tenant ID, then retrieved the corresponding tenant record from the database. This approach ensured that each request was provided with a fully configured Claude client and rate limit bucket, tailored to the specific tenant's requirements.\n\nBy adopting this tenant-aware dependency injection strategy, CitizenApp was able to cleanly isolate API keys and rate limits per tenant, eliminating the risks associated with a global API key and the complexities of managing shared rate limit buckets. This approach provided a robust, maintainable, and scalable foundation for the application's integration with Anthropic's Claude API, paving the way for seamless scaling as the number of tenants continued to grow.",
  "summary": "FastAPI Dependency Injection for Anthropic Claude: Isolating API Keys and Rate Limits Per Tenant When CitizenApp hit 15 tenants, I realized our single global Claude API key was a ticking time bomb. One customer's agentic loop burning through their quota would throttle everyone else. Worse, we had no way to enforce per-tenant rate limits without adding middleware spaghetti that would make…",
  "key_points": [
    "CitizenApp faced challenges with a single Claude API key for 15 tenants",
    "FastAPI s dependency injection isolated API keys and rate limits per tenant",
    "Tenant-specific Claude clients and rate-limit buckets eliminated global variables"
  ],
  "editors_take": "Isolating API keys and rate limits per tenant via FastAPI dependency injection allows CitizenApp to scale its Anthropic Claude API integration while eliminating risks tied to a global API key.",
  "illustration": null,
  "coverage": {
    "outlets": 1,
    "also_reported_by": []
  },
  "ai_generated": true,
  "disclaimer": "Summaries, key points and the editor’s take are written by software from other outlets’ reporting and may contain errors — always check the linked original."
}