By Abhijith N M · Reviewed 24 October 2026 · Editorial standards
Harbor Helper serves several stores from one deployment. That is a cost decision, not a licence to share data. The invariant is simple to state and easy to break: no read or write may cross a tenant boundary, and no prompt, cache key, vector query, approval queue, log line or job payload may lose the tenant id on the way.
The dangerous version of this bug is not a wrong answer; it is a correct answer about someone else's customer. The model will not protect you, because it does not know what a tenant is. Isolation belongs in the data layer and the request context, enforced in code and tested, not in a system prompt sentence.
| Layer | How a leak happens | Enforcement that holds |
|---|---|---|
| Database rows | A query forgets the tenant_id filter | Tenant column on every table, scoped repository methods and row-level security where the database supports it |
| Vector retrieval | Similarity search spans every tenant's documents | A mandatory tenant filter in the retriever, applied before scoring |
| Cache | A key without the tenant returns another store's answer | Prefix every cache key with the tenant id and the prompt version |
| Queue and jobs | A job payload without a tenant loads the wrong profile | Tenant id is required in the job envelope and validated at claim time |
| Logs and traces | PII from another tenant in a shared dashboard | Tenant tag on every span, and redaction at the logging boundary |
| Model context | A profile loaded by a customer id the model supplied | Load only by (tenant_id, customer_id) from the request context |
A rate limit protects the service. A quota protects the business and the other tenants: it defines what each plan may consume, when a burst is allowed and what happens at the boundary. They are related but not the same decision, and mixing them produces the classic bug where one noisy free tenant exhausts the shared pool for a paying one.
Retry-After and a machine-readable reason before spending the request's tokens, not after."""Apply plan entitlements and rolling windows to one tenant request."""
PLANS = {
"free": {"requests_per_min": 10, "tokens_per_day": 100_000, "jobs": 1},
"plus": {"requests_per_min": 60, "tokens_per_day": 1_000_000, "jobs": 3},
"scale": {"requests_per_min": 600, "tokens_per_day": 20_000_000, "jobs": 20},
}
TENANTS = [
{
"tenant_id": "store-eu",
"plan": "plus",
"requests_last_minute": 58,
"tokens_today": 1_040_000,
"running_jobs": 2,
"requested_tokens": 4000,
},
{
"tenant_id": "store-us",
"plan": "free",
"requests_last_minute": 4,
"tokens_today": 92_000,
"running_jobs": 1,
"requested_tokens": 9000,
},
{
"tenant_id": "store-au",
"plan": "free",
"requests_last_minute": 2,
"tokens_today": 40_000,
"running_jobs": 0,
"requested_tokens": 12_000,
},
]
def decide(tenant):
limits = PLANS[tenant["plan"]]
reasons = []
if tenant["requests_last_minute"] + 1 > limits["requests_per_min"]:
reasons.append("requests_per_min")
if tenant["tokens_today"] + tenant["requested_tokens"] > limits["tokens_per_day"]:
reasons.append("tokens_per_day")
if tenant["running_jobs"] + 1 > limits["jobs"]:
reasons.append("concurrent_jobs")
decision = "deny" if reasons else "allow"
header = "Retry-After: 30" if reasons else "X-Quota-Remaining: ok"
return decision, reasons, header
for tenant in TENANTS:
decision, reasons, header = decide(tenant)
reason_text = ",".join(reasons) if reasons else "within limits"
print(
f"{tenant['tenant_id']} {tenant['plan']}: {decision} "
f"({reason_text}) | {header}"
)The first tenant is on the plus plan and still denied: its request budget is fine, but today's token budget would be exceeded by this one request. Deciding before the call, rather than after it, is what makes the limit an actual limit. The second tenant fails two dimensions at once, so the response should name both reasons and a retry delay. The third fits every budget and is allowed, which is the common case and should be cheap to compute.
Background jobs need their own limits, or a queue becomes a way around the request quota. Count concurrent jobs per tenant, cap the total tokens a job may spend and give batch work a lower priority than interactive work. When the queue is backed up, the right action is usually to slow admission per tenant rather than to stop the workers: fairness is preserved, and a paying tenant sees a delay only when its own budget is the reason.
Key takeaways
Tenant isolation is a data invariant enforced in code and tested with cross-tenant fixtures, never in a prompt sentence.
Scope every layer - rows, retrieval, cache keys, jobs, logs and model context - by the tenant from the request context.
Quotas are multi-dimensional product policy; a request limit alone lets one long request cost more than a thousand short ones.
Decide before spending: return 429 with a reason and Retry-After, and never silently throttle a tenant.
Give background work its own per-tenant limits and priority so the queue cannot bypass the request quota.
Written and edited by the nexGenCoders team. Spotted a mistake? Tell us and we will fix it.
Don't lose this lesson
Create a free account to save your progress, keep your streak, and work toward a shareable certificate.