Claude API Multi-Tenant SaaS Integration Guide
Claude API Multi-Tenant SaaS Integration Guide
If you're building a SaaS product where each customer (tenant) gets their own AI features powered by Claude, the integration challenge isn't calling the API — it's isolating usage, cost, and rate limits per tenant while keeping a single codebase. A naive setup where every customer shares one API key and one global rate limit breaks down fast: one noisy tenant can exhaust your quota, you can't bill accurately, and a leaked key compromises every customer at once.
The practical answer is to treat each tenant as a first-class identity in your Claude integration, not just a row in your database. That means per-tenant API keys (or at least per-tenant metadata tagging), per-tenant usage metering, and a rate-limiting layer that protects tenants from each other. Below is a concrete architecture for doing this, whether you're calling Anthropic's API directly or routing through a gateway.
Why Shared Keys Don't Scale for Multi-Tenant SaaS
A single shared API key across all tenants creates three recurring problems:
- No cost attribution. You can't tell which customer drove up your Claude bill this month without parsing logs and reconstructing usage after the fact.
- No blast radius control. If a tenant's integration goes into a retry loop or a malicious actor scrapes your app, every other tenant's requests slow down or fail.
- No clean offboarding. Revoking access for one churned customer means rotating the key for everyone.
The fix is to give every tenant (or at least every paying plan tier) its own credential, scoped and trackable independently.
Pattern 1: Per-Tenant API Keys
The cleanest model issues a distinct key per tenant and stores it encrypted in your tenant record. Your backend looks up the tenant's key before each Claude call:
async function callClaudeForTenant(tenantId, messages) {
const apiKey = await getDecryptedKey(tenantId); // from your tenant store
const res = await fetch('https://api.anthropic.com/v1/messages', {
method: 'POST',
headers: {
'x-api-key': apiKey,
'anthropic-version': '2023-06-01',
'content-type': 'application/json',
},
body: JSON.stringify({
model: 'claude-sonnet-4-5',
max_tokens: 1024,
messages,
}),
});
return res.json();
}
This gives you native usage separation at the provider level, but it also means managing hundreds or thousands of credentials, rotating them, and watching each one for rate limit exhaustion individually. That's manageable for 10 tenants; it becomes an operational burden at 500.
Pattern 2: One Account, Many Scoped App Keys
A more scalable approach — and the one SubToAPI is built around — is to keep one underlying Claude access but issue scoped application keys per tenant, team, or feature from a single dashboard. You create a sub_live_... key per tenant through the API, each with its own usage metadata, without managing separate Anthropic accounts or billing relationships.
curl https://api.subtoapi.app/v1/messages \
-H "Authorization: Bearer $SUBTOAPI_KEY" \
-H "content-type: application/json" \
-d '{
"model": "claude-sonnet-4-5",
"max_tokens": 1024,
"messages": [{"role": "user", "content": "Summarize this support ticket."}],
"metadata": {"tenant_id": "acme-corp"}
}'
Issue one sub_live_... key per tenant from the dashboard, tag requests with metadata.tenant_id as a secondary label, and you get both hard isolation (per-key) and queryable attribution (per-metadata) without juggling multiple provider accounts. See /docs/quickstart for setup and /docs/messages for the full request shape.
Tracking Usage and Cost Per Tenant
Whatever pattern you choose, you need a usage ledger keyed by tenant. At minimum, log for every request:
tenant_id- input/output token counts
- model used
- timestamp
- request latency
function recordUsage(tenantId, response) {
db.insert('claude_usage', {
tenant_id: tenantId,
input_tokens: response.usage.input_tokens,
output_tokens: response.usage.output_tokens,
model: response.model,
created_at: new Date(),
});
}
This table is what lets you answer "which tenants are costing us the most" and feed usage-based billing or plan-tier caps. If you're using SubToAPI's keys, usage metadata is already broken out per key in the dashboard, so you can skip building this ledger yourself for the provider-side numbers and focus your own logging on product-specific events.
Rate Limiting and Fair Use Per Tenant
Protect tenants from each other with a per-tenant token bucket or sliding-window limiter sitting in front of your Claude calls:
const limiters = new Map();
function getLimiter(tenantId) {
if (!limiters.has(tenantId)) {
limiters.set(tenantId, new RateLimiter({ tokensPerInterval: 50, interval: 'minute' }));
}
return limiters.get(tenantId);
}
async function guardedCall(tenantId, messages) {
const limiter = getLimiter(tenantId);
await limiter.removeTokens(1);
return callClaudeForTenant(tenantId, messages);
}
Tie the limiter's threshold to the tenant's plan tier — free trial tenants get a small bucket, paid tenants get a larger one. This is independent of whatever global rate limits your Claude provider enforces at the account level.
Streaming and Tool Use Across Tenants
If your SaaS streams responses to end users (chat UIs, live assistants), make sure your streaming layer still tags each connection with the originating tenant so you can cut off or throttle a specific stream without affecting others. See /docs/streaming for SSE handling details. If tenants have different tool/function permissions (e.g., one customer's agent can query their CRM, another's can't), scope tool definitions per tenant request rather than hardcoding a global tool list — /docs/tools covers the schema.
Security Isolation Beyond the API Key
Multi-tenant AI integrations need isolation beyond credentials:
- Never mix tenant context in the same prompt or conversation history — build the message array fresh per request from that tenant's data only.
- Strip or validate any tenant-supplied content that gets injected into system prompts, to avoid one tenant's input leaking instructions that affect another's session.
- Store conversation history and uploaded documents with tenant_id as a mandatory filter in every query, not an optional one.
Getting Started
For teams that don't want to run their own key-issuing and metering infrastructure, SubToAPI gives you per-tenant application keys, streaming, tool use, and usage metadata out of the box, with team seats if multiple people on your side manage tenant onboarding. Compare plans at /pricing or start a free trial at /signup.
Questions
Should each tenant get a separate Claude API key or shared key with metadata tags? Separate keys give hard isolation and clean revocation; shared keys with metadata tags are easier to manage at scale but rely on your own logging discipline for accurate per-tenant attribution.
How do I bill tenants based on their actual Claude usage? Log input/output tokens per request keyed by tenant_id, aggregate monthly, and apply your markup or plan-based caps — or use a provider that exposes per-key usage metadata directly in a dashboard.
Can I enforce different rate limits for different tenant plan tiers? Yes — implement a per-tenant rate limiter in your own backend (token bucket or sliding window) sized by plan tier, independent of any limits the underlying API provider enforces at the account level.