Building a Secure Multi-Tenant LLM Gateway: A Step-by-Step Production Playbook
The critical nuance is that "data residency" is not just about where the model runs — it's about where the data transits. Logs, telemetry, and audit trails also carry request content and must be pin
Introduction
A misconfigured gateway is expensive. A single leak exposes tenant data. Runaway spend drains budgets in hours. Cross-tenant contamination erodes trust permanently.
A gateway is your single control point. It governs security, cost, and compliance across all model traffic. Multi-tenancy multiplies the risk. Isolation failures are not just bugs; they are breach vectors.
This playbook delivers a production-ready path. It moves you from zero to a hardened, multi-tenant LLM gateway. Every section maps to a concrete task, not theory.
This guide is for platform engineers, ML infrastructure architects, and LLM Ops leads. You should already know Kubernetes, authentication and authorization (authN/authZ), and model APIs. You should understand token-based billing and streaming responses.
By the end, you will have a deployable architecture. You will know the seven isolation patterns that matter. You will know how to route, secure, observe, and operate at scale.
Key stat: A single exposed API key can cost an enterprise an average of $150K in unauthorized model spend in under 48 hours.
Let me walk you through the full playbook, from requirements to production.
1. Why a Multi-Tenant LLM Gateway Beats a Plain API Gateway
A plain API gateway is request-aware. It routes HTTP calls and checks headers. An LLM gateway must be token-aware, stream-aware, and model-aware. That is a fundamental difference.
1.1 Token Awareness vs. Request Awareness
A normal API call costs the same regardless of payload. An LLM call costs based on tokens. Tokens are the units of text a model processes. Request awareness cannot measure this.
Token awareness lets you budget, meter, and charge per tenant accurately. It lets you predict cost before a call completes. A plain gateway is blind to this dimension.
1.2 Model Routing, Streaming, and Semantic Caching
LLM responses stream. Text arrives token by token, not as one payload. A generic gateway mishandles streaming under load.
Model routing picks the best model for each request. Semantic caching stores responses by meaning, not exact match. Both are capabilities a generic API gateway lacks entirely.
1.3 The Multi-Tenancy Differentiator
Multi-tenancy demands per-tenant isolation. It demands per-tenant budgeting and audit trails. Generic gateways do not enforce these.
The differentiator is control. You control which tenant talks to which model. You control spend ceilings and data boundaries. A plain gateway cannot do this safely.
2. Requirements Gathering & Non-Functional Targets
Requirements come before architecture. Define them clearly or pay later in rework and breaches.
2.1 Defining Tenancy Model: Silo, Pool, or Bridge
Choose your tenancy model early. A silo gives maximum isolation and maximum cost. Each tenant gets dedicated resources. A pool shares resources across tenants for lower cost. A bridge is a hybrid of both.
Silos suit regulated tenants with strict data rules. Pools suit high-volume, lower-sensitivity workloads. Bridges let you mix them under one control plane.
[ILLUSTRATION 2: Tenancy model comparison table — Silo (dedicated resources, max isolation, highest cost), Pool (shared resources, lower cost, interference risk), Bridge (hybrid, mixed workloads under one control plane), with a recommendation matrix mapping tenant sensitivity to model choice.]
2.2 SLOs: Latency, Availability, Cost Ceiling
Define service-level objectives (SLOs) before building. Set your p95 latency target. That is the latency 95 percent of requests must meet. Set your availability target, often 99.9 percent.
Set a per-tenant cost ceiling. This prevents one tenant from exhausting shared capacity. Without a ceiling, budgets spiral and neighbors suffer.
2.3 Compliance & Jurisdiction Constraints
Compliance dictates architecture. The General Data Protection Regulation (GDPR) governs EU data. SOC 2 governs security controls and audit evidence. Data residency laws restrict where data can live.
These constraints decide where your control plane can sit. They decide which regions serve which tenants. Resolve them now, not during an audit.
3. Reference Architecture: Control Plane vs. Data Plane
Separate the slow path from the fast path. The control plane handles configuration, keys, and policy. The data plane handles routing, streaming, and rate limiting. Keep them apart.
3.1 Control Plane Components
The control plane is the brain. It stores configuration, tenant keys, and access policies. It writes audit records and manages model catalogs.
Control-plane changes are rare and deliberate. They do not need sub-millisecond speed. They do need strict access control and full change history.
3.2 Data Plane Components
The data plane is the muscle. It routes requests, enforces rate limits, and proxies streaming. It applies semantic caching and response filtering.
The data plane must be fast and horizontally scalable. It carries every request and every token. Its performance defines your user experience.
3.3 Request Lifecycle Walkthrough
Every request crosses a defined sequence. Authentication happens first. Then policy evaluation. Then routing. Then rate limiting. Then the model call. Then response filtering. Then audit logging.
Design rule: A control-plane failure must not take down the data plane. Cache policies locally, queue audit writes, and design for graceful degradation — not total outage.
[ILLUSTRATION 1: Multi-tenant LLM gateway reference architecture diagram showing control plane (config, keys, policy, audit) and data plane (routing, rate limiting, streaming, caching) with the request lifecycle flowing through authN → policy → routing → rate limit → model call → response filter → audit.]
4. Tenant Isolation: 7 Patterns You Must Implement
Isolation is the heart of multi-tenancy. Implement all seven patterns. Do not cherry-pick; each closes a distinct risk.
4.1 Namespace & Network Isolation
Isolate tenants at the infrastructure layer. Give each tenant its own Kubernetes namespace. Enforce network policies that block cross-tenant traffic.
This prevents one tenant from reaching another's services. It is your first line of defense against lateral movement.
4.2 Per-Tenant Credential Vaulting
Every tenant gets its own credentials in a vault. A leaked key exposes only one tenant. Never share keys across tenants.
Use a secrets manager with per-tenant scoping. Rotate credentials automatically. Treat each tenant as a separate trust boundary.
4.3 Dedicated Model Pools vs. Shared Pools
Dedicated pools isolate noisy neighbors. A heavy tenant cannot slow a light one. Shared pools cut cost but risk interference.
Choose dedicated pools for regulated or latency-sensitive tenants. Use shared pools for tolerant, high-volume, lower-sensitivity workloads where cost outweighs isolation risk.
[ILLUSTRATION 3: Pooling decision diagram — a flowchart branching on tenant sensitivity (regulated/latency-critical → dedicated pool) vs. cost-tolerance (high-volume/low-sensitivity → shared pool), showing the noisy-neighbor tradeoff in each path.]
4.4 Rate Limiting & Quota Enforcement
Rate limiting protects shared capacity. Per-tenant quotas cap concurrent requests and token throughput. A burst from one tenant must not starve others.
Use token-bucket algorithms for smooth metering. Enforce limits at the data plane edge. Reject or queue before the model call, not after.
4.5 Data Residency & Regional Pinning
Pin each tenant to approved regions. A tenant's data must never transit an unapproved jurisdiction. Enforce this at the routing layer.
Store tenant data only where policy allows. Block cross-region model calls by default. This is a compliance requirement, not a preference.
4.6 Audit Logging & Tenant Attribution
Every action must map to a tenant. Log who, what, when, and which model. Immutable audit trails are the backbone of SOC 2 and GDPR evidence.
Correlate requests to tenants even across retries. A request without tenant attribution is a compliance gap. Make attribution mandatory.
4.7 Prompt Injection & Output Filtering
Tenants can send malicious prompts. Prompt injection attempts to override system instructions. Filter both inbound prompts and outbound model output.
Sanitize inputs and validate outputs. Apply per-tenant content policies. This closes the last isolation vector — the model itself.
5. Security Hardening: AuthN/AuthZ, Secrets, and Transport
Security is layered, not bolted on. Each layer assumes the next can fail. Defense in depth is non-negotiable.
5.1 Authentication & Authorization
Authenticate every request at the edge. Use OAuth 2.0 or OIDC for human users. Use API keys or mTLS for machine-to-machine traffic.
Authorize per tenant, per model, per action. Never rely on the client to self-identify alone. Enforce scopes server-side.
5.2 Secrets Management & Rotation
Never hardcode keys in code or config. Store all secrets in a vault with per-tenant scoping. Rotate on a schedule and on any suspected leak.
Bind credentials to tenant identity, not shared pools. A rotated key must invalidate immediately. Automate rotation to remove human error.
5.3 Transport Security & Egress Control
Encrypt all traffic in transit with TLS. Pin certificates for model provider endpoints. Enforce egress allowlists so the gateway calls only approved models.
Block unexpected egress destinations. This contains a compromised tenant. Network policy is a security boundary, not just a performance feature.
6. Observability: Metrics, Logs, and Cost Attribution
You cannot secure or operate what you cannot see. Observability is a first-class requirement. Build it in from day one.
6.1 Metrics: Latency, Tokens, Error Rates
Track p95 and p99 latency per tenant and per model. Track token throughput and error rates. These three tell you if SLOs are met.
Alert on SLO burn rate, not just thresholds. A slow drift is harder to catch than a spike. Burn-rate alerts catch degradation early.
6.2 Structured Logging & Trace Correlation
Emit structured logs with tenant and request IDs. Correlate spans across the full request lifecycle. A trace must follow a request from edge to model and back.
Include token counts and cost in every log line. This turns logs into billing and audit evidence. Never log raw prompts or responses by default.
6.3 Cost Attribution & Chargeback
Attribute every token to a tenant and a model. Compute cost in real time as tokens stream. This enables per-tenant billing and chargeback.
Break down cost by model, tenant, and time window. This reveals anomalies and runaway spend. Cost is an observability signal, not just an accounting figure.
7. Rate Limiting, Quotas, and Cost Control
Cost control is a security control. Unbounded spend is an availability risk. A runaway tenant can take down the whole gateway.
7.1 Token-Bucket Rate Limiting
Token-bucket algorithms smooth bursty traffic. They allow bursts up to a ceiling while holding average rate. This balances responsiveness and protection.
Apply buckets per tenant and per model. A tenant's burst must not exceed its quota. This is the core of fair sharing.
7.2 Per-Tenant Spend Ceilings
Set hard spend ceilings per tenant. Stop or throttle when a ceiling is hit. A ceiling is a kill switch against runaway cost.
Enforce ceilings in the data plane, in real time. Do not wait for a batch job to discover overspend. Real-time enforcement prevents the $150K scenario.
7.3 Budgeting, Alerts, and Auto-Throttling
Forecast spend with token budgets. Alert when a tenant approaches its ceiling at 70, 90, and 100 percent. Escalate automatically.
Auto-throttle before a hard stop. Queue or degrade gracefully instead of failing hard. This protects availability while containing cost.
8. Deployment Topologies & Scaling
Where you deploy affects latency, compliance, and cost. Choose a topology that matches your SLOs and residency rules.
8.1 Single-Region vs. Multi-Region
Single-region is simplest and lowest latency for one geography. Multi-region adds failover and residency flexibility. It also adds complexity and cross-region cost.
Match regions to tenant data residency. Do not serve a GDPR tenant from a non-approved region. Routing must respect jurisdiction.
8.2 Horizontal Scaling of the Data Plane
The data plane scales horizontally. Add replicas behind a load balancer as load grows. Each replica is stateless for routing.
Keep state (quotas, caches) in shared stores. Or shard by tenant for stronger isolation. Stateless replicas scale cleanly; stateful ones need care.
8.3 Blue/Green and Canary Deployments
Deploy changes without downtime. Blue/green runs two full stacks and flips traffic. Canary rolls out to a small tenant subset first.
Canary against low-risk tenants first. Watch latency and error SLOs before full rollout. This protects the platform from bad releases.
9. Security Testing & Incident Response
Assume the gateway will be attacked. Test it like an adversary would. Prepare to respond before the incident, not after.
9.1 Threat Modeling & Penetration Testing
Threat-model the gateway annually. Include tenant-isolation bypass, prompt injection, and key theft. These are your top risks.
Run penetration tests against the data plane. Test cross-tenant data access explicitly. A successful cross-tenant read is a critical finding.
9.2 Isolation Breach Response
Define a breach playbook before it happens. Isolate the affected tenant immediately. Revoke keys, block traffic, and preserve evidence.
Communicate per your compliance obligations. GDPR breach notification has strict timelines. Prepare the runbook now.
9.3 Compliance Audits & Evidence Collection
Maintain continuous audit evidence. SOC 2 and GDPR audits need provable controls. Automate evidence collection from logs and config.
Map every control to evidence. An audit should be a retrieval task, not a scramble. Continuous readiness beats audit-season panic.
10. Production Checklist & Next Steps
You now have the full architecture. Here is the order to build it. Sequence matters; isolation before scale, security before cost.
10.1 The Build Order
Start with requirements and tenancy model. Then control/data plane separation. Then the seven isolation patterns. Then security hardening. Then observability. Then cost control. Then scaling and testing.
Do not skip isolation for speed. Do not ship without audit logging. Every shortcut becomes a breach vector later.
10.2 The Production Checklist
- Tenancy model chosen (silo, pool, or bridge)
- SLOs defined (p95, availability, cost ceiling)
- Compliance and residency constraints mapped
- Control plane and data plane separated
- All seven isolation patterns implemented
- Per-tenant credential vaulting with rotation
- AuthN/AuthZ enforced at the edge
- Transport encryption and egress allowlists
- Metrics, structured logs, and trace correlation
- Real-time cost attribution and spend ceilings
- Token-bucket rate limiting per tenant
- Deployment topology matches SLOs and residency
- Threat model and penetration tests scheduled
- Breach response and audit evidence automated
10.3 Next Steps
Deploy the control plane first. Then stand up the data plane. Then enable isolation, then observability, then cost controls. Test security throughout, not at the end.
Start with a pilot tenant. Prove isolation and cost control before onboarding more. A successful pilot de-risks the full rollout.
Final rule: A secure multi-tenant LLM gateway is not a feature — it is a discipline. Isolation, observability, and cost control are continuous, not one-time. Build the discipline, and the gateway will serve you safely at scale.
Expert Q&A
Q: We already run a mature API gateway (Kong, Envoy, or similar). Why can't we just bolt LLM features onto it instead of building a separate LLM gateway?
A: You can, and many teams start that way — but the gap appears exactly where LLM traffic differs from normal HTTP traffic. Your existing gateway is request-aware: it routes on URL, headers, and methods, and it treats every call as a uniform unit. An LLM gateway must be token-aware, stream-aware, and model-aware. Three concrete failures occur if you force LLM traffic through a generic gateway:
- Streaming mishandling. LLM responses stream token-by-token over a long-lived connection. Generic gateways often buffer or kill long-lived streams under load, breaking partial-response delivery and inflating perceived latency.
- Cost blindness. A generic gateway cannot attribute cost to tokens because it never parses token counts. You lose real-time spend ceilings and per-tenant chargeback — the exact controls that prevent runaway spend.
- Semantic capabilities. Model routing (picking the best model per request) and semantic caching (serving cached responses by meaning, not exact match) are not routing features a generic gateway implements.
The pragmatic path: keep your generic gateway at the edge for TLS termination, authN, and coarse routing, then place a dedicated LLM gateway behind it for token-aware metering, streaming, and model routing. That hybrid gives you the best of both without a rewrite.
Q: We're torn between dedicated and shared model pools. How do we decide without exploding our cloud bill?
A: The decision is a risk/cost tradeoff, and you can make it quantitative instead of emotional. Start by classifying tenants along two axes: data sensitivity and latency sensitivity. Regulated tenants (healthcare, finance) or latency-critical workloads (interactive copilots) should get dedicated pools — the cost premium buys guaranteed isolation and predictable p95. Tolerant, high-volume, lower-sensitivity workloads (batch summarization, internal Q&A) belong in shared pools.
Do not guess. Instrument both paths before you commit. Run a two-week shadow test where a sample of "shared-candidate" tenants runs on a shared pool while you measure the actual p95 impact of noisy neighbors. The key metric is interference ratio: how much does the p95 for the lightest tenant degrade when the heaviest tenant is at peak? If the degradation stays under your SLO budget (say 10–15% of the p95 target), shared pooling is safe. If it blows past it, those tenants need dedicated capacity.
A middle option many teams miss: burstable shared pools with dedicated headroom. Give latency-sensitive tenants a dedicated minimum allocation, then let them burst into shared capacity above that floor. You get silo-grade guarantees at the floor and pool-grade cost efficiency at the peak. This "bridge" model (section 2.1) is often the right default for mixed fleets.
Q: How do we actually enforce per-tenant spend ceilings in real time, given that LLM costs are only known once tokens stream back?
A: This is the crux of LLM cost control, and the answer is estimating cost before the call and reconciling during the stream. You cannot know the exact token count until the response completes, but you can bound it tightly with three mechanisms:
-
Pre-call estimate. Estimate tokens from the request (prompt length) plus a configured
max_tokensceiling for the response. Compute the worst-case cost for that request before you send it to the model. If the worst case would breach the tenant's remaining budget, reject or queue the request immediately. This is your first line of defense. -
Stream-time metering. As tokens stream back, count them in real time and accumulate cost against the tenant's live balance. The moment the running total crosses the ceiling, terminate the stream (or degrade to a cheaper model). This stops overspend mid-response, not after the fact.
-
Continuous token-bucket enforcement. Meter cost through the same token-bucket mechanism you use for rate limiting (section 7.1), but with a spend dimension. This smooths bursts and prevents one tenant from exhausting shared capacity.
The architectural rule is that enforcement lives in the data plane, not a batch job. A nightly reconciliation that discovers overspend is worthless — the $150K exposure happens in hours, not days. Real-time, in-stream enforcement is what turns a spend ceiling from a report into a kill switch.
Q: My team keeps hitting cross-tenant data leaks in shared caches. Is semantic caching safe for multi-tenant LLM gateways at all?
A: Semantic caching is safe — but only if the cache is tenant-scoped and access-controlled, and only for the right content. The leak you're describing almost always comes from one of three mistakes:
- A globally shared cache keyed only by embedding similarity. Two tenants asking semantically similar questions can collide and serve each other's responses. The fix is to namespace the cache key by tenant ID before any similarity lookup. A tenant's query must only ever match entries written by that same tenant.
- Caching sensitive or tenant-specific content. Never cache responses containing PII, proprietary data, or anything whose output depends on tenant context. Cache only model-agnostic, tenant-agnostic content — general knowledge answers, boilerplate, reference material. When in doubt, don't cache.
- No eviction or TTL on cache entries. Stale entries can persist tenant data long after a tenant is deprovisioned. Enforce per-tenant TTLs and purge a tenant's cache entries immediately on deprovision or key rotation.
A safe pattern is per-tenant cache shards: each tenant gets its own cache namespace (and ideally its own cache instance for regulated tenants), with similarity matching confined to that shard. This gives you the cost savings of semantic caching while keeping the isolation guarantees of section 4. If a tenant is regulated or handles sensitive data, the safest choice is to disable semantic caching for that tenant entirely and accept the higher model cost — isolation trumps caching savings.
Q: Our gateway needs to handle both GDPR (EU data residency) and US-based model providers. How do we keep tenant data from transiting unapproved jurisdictions?
A: This is a routing-layer enforcement problem, and the answer is regional pinning with hard egress controls — not hoping the model provider routes correctly. Three layers make it enforceable:
-
Regional model endpoints. Deploy gateway data-plane replicas in the regions your tenants require (e.g., EU and US). Configure each replica to call only the model provider's regional endpoint (e.g., an EU-resident model endpoint for EU tenants). Never let a replica fall back to a default global endpoint, because that fallback can route data through an unapproved jurisdiction.
-
Tenant-to-region pinning at the routing layer. Bind each tenant to an approved region in the control plane. The routing layer must reject or redirect any request whose tenant is pinned to a region the current replica doesn't serve. Enforce this before the model call, not after.
-
Egress allowlists and network policy. Restrict each data-plane replica's egress to a hard allowlist of approved model-provider endpoints and IP ranges. Any egress outside the allowlist is blocked at the network layer, so even a misconfigured or compromised gateway cannot exfiltrate data to an unapproved destination (this ties into section 5.3).
The critical nuance is that "data residency" is not just about where the model runs — it's about where the data transits. Logs, telemetry, and audit trails also carry request content and must be pinned to the same approved regions. Store EU tenant logs only in EU-resident log stores. If you use a US-based observability backend, you must scrub or redact tenant payloads before shipping them, or the transit itself is a GDPR violation. Resolve this in section 2.3 during requirements, because retrofitting regional pinning after launch is painful and error-prone.