On this page
A shared LLM platform serves one hundred workspaces on one GPU cluster. Tenant A submits a 12,000-token design review; Tenant B asks for a 40-token incident summary. Both stall in the queue even though GPU utilization sits at 55%. Seconds later, B's answer quotes a paragraph from A's private postmortem.
Those two symptoms have completely different root causes. Stall time points to KV-cache exhaustion, chunked-prefill starvation, or quota lock contention; adding GPUs might relieve it. Foreign text in B's reply is an authorization or cache-isolation failure that extra GPUs will never fix.
In Code Completion, we optimized a single low-latency serving path. Here we design the shared platform underneath: sharing frozen base weights and GPU batch steps while keeping identity, quotas, memory pages, adapters, caches, and billing strictly isolated per tenant.
Define what sharing may change
A 72B-parameter model in BF16 (2 bytes per parameter) occupies roughly 144 GB of VRAM just for weights. Running one hundred dedicated copies would burn 14.4 TB before a single token is generated. Sharing frozen model replicas across tenants saves orders of magnitude in hardware, yet sharing weights never implies sharing data or schedules.
Anchor the architecture on six clear guarantees before choosing a serving engine:
| Layer | Design rule |
|---|---|
| Identity | A caller can act only within workspaces it is authorized to access |
| Data state | Prompts, adapters, retrieved chunks, KV caches, and logs stay scoped to the reader |
| Quotas | Concurrent requests cannot double-spend remaining token allowances or GPU pages |
| Scheduling | Backlogged tenants receive their configured compute share without starving short jobs |
| Metering | Retries and duplicate completion messages never double-bill or refund twice |
| Recovery | Uncertain attempts stay held until worker execution stops and usage reconciles |
Separate the control plane (configuration, deployments, artifact permissions, worker membership) from the data plane (gateway routing and GPU inference). Enforce access checks at both the gateway and worker; protecting only the gateway fails if a client can hit worker admin endpoints directly.

Define service-level objectives (SLOs) with exact measurement endpoints:
- Time to first token (TTFT): measured from external gateway arrival to the first usable output token (covering queueing, prefill, and adapter loading), not from when the GPU engine dequeues the prompt.
- Inter-token latency (ITL) vs. TPOT: serving engines like vLLM can bundle multiple tokens into one streamed event, so raw event gaps differ from request-level time per output token (TPOT) .[1]
For example, if a stream emits token 1 at 100 ms, bundles four tokens into one frame at 220 ms, and finishes at 240 ms, the observed event gap is 120 ms while request TPOT is . Specify whether a tenant's SLO tracks per-event stream gaps or average TPOT, and track completion rate alongside latency so failed streams never make fast-failing traffic look healthy.

Establish identity before touching tenant state
Treat an HTTP Tenant-ID: A header as an untrusted workspace selector, never as proof of access. Authenticate the caller, verify current workspace grants server-side, and reject unauthorized selections before any cache lookup, adapter fetch, or retrieval call.[2]
Propagate the resolved tenant, principal, request ID, attempt ID, approved model/adapter revisions, deadline, and output cap inside a signed internal envelope over authenticated service links. Never let a downstream worker overwrite tenant identity from a request body field.
from dataclasses import dataclass
@dataclass(frozen=True)
class Context:
principal: str
tenant: str
GRANTS = {"alice": {"A"}, "bob": {"B"}}
def resolve_context(authenticated_principal, requested_tenant):
if requested_tenant not in GRANTS.get(authenticated_principal, set()):
raise PermissionError("workspace access denied")
return Context(authenticated_principal, requested_tenant)
ctx = resolve_context("alice", "A")
assert ctx.tenant == "A"
for principal, tenant in (("alice", "B"), ("unknown", "A")):
try:
resolve_context(principal, tenant)
except PermissionError:
print(f"denied: {principal} acting for {tenant}")
else:
raise AssertionError("unauthorized context created")denied: alice acting for B
denied: unknown acting for AWorkspace membership is only the outer layer: inside Workspace A, Alice may be allowed to read engineering docs while another user cannot. Pass principal-level permissions into retrieval, tool execution, caches, and logs, and never rely on the LLM prompt to enforce document access.
Reserve quota and memory before dispatching requests
No single rate limit protects every resource:
| Control | Resource it limits | What it leaves unchecked |
|---|---|---|
| RPM | Request arrival rate | Prompt length, concurrency, and decode work |
| TPM | Tokens over a rolling window | Uneven GPU cost between prefill and decode |
| Period quota | Total spend in a billing period | Fair scheduling among active requests |
| Concurrency cap | Simultaneous in-flight streams per tenant | Per-request KV memory size |
| Context / output cap | Maximum sequence length | Aggregate cluster capacity |
| KV-page budget | Reserved attention memory | Compute throughput and tail latency |
Tokenize the fully assembled prompt (including system instructions and retrieved passages) with the target model's tokenizer, then reserve input_tokens + max_output before dispatch. For a fixed-period quota, conservation holds at every instant:
Start with 1,000 units and reserve 300 input plus 400 max output (holding 700, leaving 300 available). If generation finishes at 120 output tokens, actual usage is 420; settling releases the unused 280 and leaves 580 available. Delivering that same completion message twice must still leave 580.
Make reservation and settlement idempotent
Scope every idempotency key to (tenant, request_id) and bind it to a canonical fingerprint of the resolved execution parameters (prompt, model revision, adapter revision, output cap). Replaying the exact request returns the existing hold; reusing the key with different parameters raises a conflict.
Here is an atomic single-process QuotaStore implementing hold, idempotent replay, conflict detection, and exact settlement:
from dataclasses import dataclassfrom threading import Lock
@dataclass(frozen=True)
class Spec:
fingerprint: str
input_tokens: int
max_output: int
kv_pages: int
@dataclass
class Entry:
spec: Spec
output: int | None = None # None while held or uncertain
class QuotaStore:
def __init__(self, tenants, token_limit=1000, concurrency=2, page_limit=20):
if min(token_limit, concurrency, page_limit) <= 0:
raise ValueError("limits must be positive integers")
self.limits = (token_limit, concurrency, page_limit)
self.used = {tenant: 0 for tenant in tenants}
self.entries = {}
self.lock = Lock()
def _held(self, tenant):
return [e for (owner, _), e in self.entries.items()
if owner == tenant and e.output is None]
def available(self, tenant):
with self.lock:
held_tokens = sum(e.spec.input_tokens + e.spec.max_output for e in self._held(tenant))
return self.limits[0] - self.used[tenant] - held_tokens
def reserve(self, tenant, request_id, spec):
if tenant not in self.used:
raise PermissionError("unknown tenant")
if not request_id or not spec.fingerprint:
raise ValueError("identity and fingerprint are required")
if spec.input_tokens < 0 or spec.max_output < 0 or spec.kv_pages <= 0:
raise ValueError("invalid reservation units")
key = (tenant, request_id)
with self.lock:
if key in self.entries:
if self.entries[key].spec != spec:
raise ValueError("idempotency conflict")
return key
held = self._held(tenant)
held_tokens = sum(e.spec.input_tokens + e.spec.max_output for e in held)
held_pages = sum(e.spec.kv_pages for e in held)
limit, concurrency, page_limit = self.limits
if (self.used[tenant] + held_tokens + spec.input_tokens + spec.max_output > limit
or len(held) >= concurrency
or held_pages + spec.kv_pages > page_limit):
raise ValueError("quota or capacity limit exceeded")
self.entries[key] = Entry(spec)
return key
def settle(self, tenant, request_id, output_tokens):
with self.lock:
entry = self.entries[(tenant, request_id)]
if not 0 <= output_tokens <= entry.spec.max_output:
raise ValueError("invalid usage")
if entry.output is not None:
if entry.output != output_tokens:
raise ValueError("conflicting settlement")
return
self.used[tenant] += entry.spec.input_tokens + output_tokens
entry.output = output_tokens
store = QuotaStore(["A", "B"])
spec = Spec("resolved-request-v1", 300, 400, 5)
assert store.reserve("A", "r1", spec) == ("A", "r1")
assert store.reserve("A", "r1", spec) == ("A", "r1")
print("after reservation:", store.available("A"))
store.settle("A", "r1", 120)
store.settle("A", "r1", 120)
assert store.available("A") == 580 and store.available("B") == 1000
print("after repeated settlement:", store.available("A"))after reservation: 300
after repeated settlement: 580Now race two concurrent threads for the same 700-unit reservation against a 1,000-unit quota, and trace a client disconnect before settlement arrives:
from concurrent.futures import ThreadPoolExecutor
from threading import Barrier
def must_reject(call):
try:
call()
except (ValueError, PermissionError):
return
raise AssertionError("invalid operation succeeded")
before = store.available("A")
must_reject(lambda: store.reserve("A", "r1", Spec("different-request", 300, 400, 5)))
must_reject(lambda: store.settle("A", "r1", 121))
must_reject(lambda: store.reserve("A", "bad", Spec("bad", -1, 10, 1)))
must_reject(lambda: store.reserve("unknown", "r1", spec))
assert store.available("A") == before
race = QuotaStore(["A"])
barrier = Barrier(2)
def contender(request_id):
barrier.wait()
try:
race.reserve("A", request_id, spec)
return True
except ValueError:
return False
with ThreadPoolExecutor(max_workers=2) as pool:
accepted = list(pool.map(contender, ["one", "two"]))
assert sum(accepted) == 1 and race.available("A") == 300
uncertain = QuotaStore(["A"])
uncertain.reserve("A", "lost-response", spec)
must_reject(lambda: uncertain.reserve("A", "new-attempt", spec))
assert uncertain.reserve("A", "lost-response", spec) == ("A", "lost-response")
assert uncertain.available("A") == 300
uncertain.settle("A", "lost-response", 120)assert uncertain.available("A") == 580
print("concurrent reservations accepted:", sum(accepted))
print("conflicts and invalid input leave the quota store unchanged")
print("unconfirmed disconnect: held 700; terminal settlement: available", uncertain.available("A"))concurrent reservations accepted: 1
conflicts and invalid input leave the quota store unchanged
unconfirmed disconnect: held 700; terminal settlement: available 580Across multiple gateway replicas, replace the in-process lock with a serializable database transaction or atomic conditional update, retrying serialization conflicts with the same idempotency key.[3] Dispatch to a GPU worker only after the reservation commits, and never release a hold merely because a response timed out while the worker might still be generating.

A gateway loses the response after dispatch. Can it refund the whole reservation and retry with a new request ID?
Answer
No. The worker may still be executing. Keep the request in an uncertain state, recover its status from the worker, and reuse the original idempotency key. Refunding and starting a fresh ID risks double execution and quota overrun.

Budget memory before promising concurrency
The KV cache dominates per-request VRAM. For an 80-layer GQA model with 8 KV heads, head dimension 128, and 2-byte FP16/BF16 elements, every cached token costs:
At 4,000 tokens, a single sequence consumes 1.2207 GiB of KV memory before activations or engine metadata. (Always use KV-head count, not query-head count, for grouped-query attention.)
PagedAttention splits KV memory into fixed-size physical blocks (say 16 tokens, or 5 MiB per block) mapped through per-sequence block tables.[4] Sequence A (47 tokens) occupies 3 pages and Sequence B (31 tokens) occupies 2 pages, wasting only 2 of 80 allocated token slots. Keep ceiling division in exact integers so large inputs never suffer floating-point rounding drift:
BYTES_PER_TOKEN = 2 * 80 * 8 * 128 * 2
BLOCK_TOKENS = 16
def reserve_pages(input_tokens, max_output):
if input_tokens < 0 or max_output < 0:
raise ValueError("token counts must be nonnegative integers")
return (input_tokens + max_output + BLOCK_TOKENS - 1) // BLOCK_TOKENS
for tenant, tokens in (("A", 47), ("B", 31)):
pages = reserve_pages(tokens, 0)
print(f"{tenant}: {pages} pages, {pages * BLOCK_TOKENS - tokens} unused token slots")
print("MiB per page:", BYTES_PER_TOKEN * BLOCK_TOKENS / 2**20)
print("GiB for 4000 cached tokens:", round(BYTES_PER_TOKEN * 4000 / 2**30, 4))
assert reserve_pages(16, 1) == 2
assert reserve_pages(0, 0) == 0
assert reserve_pages(2**54 + 1, 0) == 2**50 + 1A: 3 pages, 1 unused token slots
B: 2 pages, 1 unused token slots
MiB per page: 5.0
GiB for 4000 cached tokens: 1.2207
Reserving input_tokens + max_output up front prevents mid-stream OOMs at the cost of lower peak concurrency. Allocating pages incrementally packs more requests onto each GPU, provided you define explicit preemption (swap to CPU RAM or recompute on resume) when memory fills. For shared prefix blocks, track physical reference counts separately from logical tenant billing so one physical block is never freed while another tenant still reads it.
Schedule active tenant turns fairly
Continuous batching inserts new requests and retires finished ones at every decode iteration.[5] Still, a single 12,000-token prefill from Tenant A can freeze Tenant B's short decode tokens for hundreds of milliseconds.
Chunked prefill slices long prompts into small token chunks interleaved with active decode steps. In vLLM, max_num_batched_tokens caps total tokens processed per step, letting you trade raw prefill throughput against smooth streaming latency.[6]
To keep heavy tenants from crowding out light ones across steps, schedule per-tenant queues with deficit round-robin (DRR). Give Tenant A a 4-unit quantum (chunk cost 3) and Tenant B a 2-unit quantum (chunk cost 1); unused credit carries forward only while a tenant remains backlogged:
from collections import deque
queues = {"A": deque([3] * 10), "B": deque([1] * 20)}
quantum = {"A": 4, "B": 2}
credit = {tenant: 0 for tenant in queues}
served = {tenant: 0 for tenant in queues}
jobs = {tenant: 0 for tenant in queues}
for _ in range(3):
for tenant, queue in queues.items():
if not queue:
credit[tenant] = 0
continue
credit[tenant] += quantum[tenant]
while queue and queue[0] <= credit[tenant]:
cost = queue.popleft()
credit[tenant] -= cost
served[tenant] += cost
jobs[tenant] += 1
if not queue:
credit[tenant] = 0
assert served == {"A": 12, "B": 6} and jobs == {"A": 4, "B": 6}
print("served work units:", served)
print("completed chunks:", jobs)served work units: {'A': 12, 'B': 6}
completed chunks: {'A': 4, 'B': 6}Why can two requests with equal token counts deserve different scheduler charges?
Answer
Their prefill-to-decode split, context length, prefix cache hit rate, and co-batched neighbors change actual GPU execution time. Equal token counts do not mean equal compute cost or equal delay imposed on other tenants.

When memory pressure forces preemption, wait for asynchronous GPU kernels to finish and block references to retire before handing freed pages to another tenant, and add age-based protection so the same low-priority request is not repeatedly evicted.
Keep adapter residency separate from permission
Low-Rank Adaptation (LoRA) lets one frozen base model serve dozens of fine-tuned tenant behaviors in the same batch by adding small low-rank weight deltas.[7]
Resolve mutable aliases like docs-current to an immutable (base_revision, adapter_revision) pair at request entry, verify tenant permission before checking whether the adapter is already hot in VRAM, and pin that revision for the request's entire lifetime (including retries and prefix caches). vLLM supports per-request LoRA selection while warning that dynamic runtime loading belongs only inside a trusted, isolated worker perimeter.[8] Validate artifact ownership, rank, size, and digest inside a trusted adapter service rather than letting tenants supply arbitrary paths.
Track active batch references on every cached adapter slot: an LRU eviction pass must skip any adapter whose reference count is nonzero, either evicting an idle slot, waiting, or routing the incoming request to another replica.

Scope retrieval and caching to the actual reader
Apply tenant and principal access predicates inside the trusted retrieval service before top- truncation, so forbidden passages never leave the database or leak via foreign scores. In PostgreSQL, enabling row-level security without a rule blocks ordinary queries, yet superusers, BYPASSRLS roles, and table owners bypass it unless FORCE ROW LEVEL SECURITY is set.[9] Always test queries under the exact production service role.
Similarly, partition prefix caches by the complete compatibility tuple: (tenant, principal, acl_revision, base_revision, adapter_revision, tokenizer_revision, kv_layout, token_ids). vLLM's prefix caching hashes token blocks with LoRA identity and supports a first-block cache_salt to isolate sharing groups.[10] Combine that salt with server-side authentication so callers can never supply another tenant's salt:
documents = [
{"id": "A-public", "tenant": "A", "readers": {"alice"}, "score": 0.80},
{"id": "A-private", "tenant": "A", "readers": {"carol"}, "score": 0.95},
{"id": "B-secret", "tenant": "B", "readers": {"bob"}, "score": 0.99},
]
def search(context, top_k):
if top_k <= 0:
raise ValueError("top_k must be positive")
allowed = [d for d in documents if d["tenant"] == context.tenant
and context.principal in d["readers"]]
return [d["id"] for d in sorted(allowed, key=lambda d: d["score"], reverse=True)[:top_k]]
def namespace(context, acl_revision, adapter_revision, token_ids):
return (context.tenant, context.principal, acl_revision, "base-v3",
adapter_revision, "tokenizer-v3", "kv-layout-v1", tuple(token_ids))
assert search(ctx, 1) == ["A-public"]
old = namespace(ctx, "acl-v1", "docs-v2", [1, 5, 9])
cache = {old: "private-cache-handle"}
assert namespace(ctx, "acl-v2", "docs-v2", [1, 5, 9]) not in cache
assert namespace(ctx, "acl-v1", "docs-v3", [1, 5, 9]) not in cache
bob = resolve_context("bob", "B")
assert namespace(bob, "acl-v1", "docs-v2", [1, 5, 9]) not in cache
print("Alice's result:", search(ctx, 1))
print("ACL revision, adapter revision, and cross-tenant misses verified")Alice's result: ['A-public']
ACL revision, adapter revision, and cross-tenant misses verifiedBumping acl_revision or adapter_revision immediately misses stale KV entries without touching other tenants. When permissions are revoked, advance the ACL revision and invalidate affected cache entries.

Match isolation perimeters to your threat model
Cache salts and scheduler weights are logical isolation controls, not hardware security perimeters. Pick the physical isolation tier that matches your tenants' threat model:
| Isolation tier | What it separates | What stays shared |
|---|---|---|
| Shared worker process | Logical request queues, adapters, and KV block tables | Process memory, GPU kernels, and serving-engine bugs |
| Separate containers / processes | OS address spaces and cgroup limits | Host kernel, GPU driver, and shared VRAM engines |
| NVIDIA MIG GPU instances | Hardware-sliced GPU memory and SMs | Host OS, driver, and remaining PCIe/NVLink infrastructure |
| Dedicated VMs or node pools | Hypervisor / physical hardware per tenant | Control plane, network fabric, and operator access |
Note a subtle hardware detail: NVIDIA compute instances inside a single MIG GPU instance still share that GPU instance's memory and engines, whereas separate GPU instances partition memory controllers.[11] Also keep encryption and PII redaction distinct from access control: redacting emails does not stop Workspace B from retrieving Workspace A's postmortem.

Separate usage, allocated cost, and the customer bill
Record one immutable usage event per execution attempt carrying tenant, request ID, attempt ID, model/adapter revision, input/cached/output token counts, and terminal status. Distinguish generated tokens, delivered tokens (before a client disconnect), and billable tokens explicitly.
Wall-clock latency never equals exclusive GPU time when requests share a batch across a multi-GPU replica. Measure compute at the step level, then allocate that step cost across co-batched requests without losing conservation. Watch unit consistency: a 4-GPU node running a 0.2 s step burns 0.8 GPU-seconds (at $2/GPU-hour) or 0.2 node-seconds (at $8/node-hour), both equal to $0.000444. Multiplying GPU-seconds by the node-hour price silently quadruples the cost:
| Quantity | Formula | Value |
|---|---|---|
| Step cost (GPU-second basis) | (0.2 s * 4 GPUs * $2/GPU-hr) / 3600 | $0.000444444 |
| Step cost (node-second basis) | (0.2 s * $8/node-hr) / 3600 | $0.000444444 |
| Conserved allocation (25% A / 75% B) | $0.000111111 + $0.000333333 | $0.000444444 |
| Unit-mismatch bug (GPU-s x node-hr) | (0.2 * 4 * $8) / 3600 | $0.001777778 (4x overcharge) |
Track idle replicas, queue wait, storage, and failed retries separately from active GPU steps, and compare allocated internal cost against customer token tariffs to spot unprofitable workload mixes early.
Worker attempt A/r1/1 settles 420 units. Two deliveries have different message IDs but describe that same settlement. Can the second message charge another 420?
Answer
No. Deduplicate on the stable (tenant, reservation, attempt) identity rather than transport message IDs. The second delivery leaves balances unchanged, and any conflicting usage value raises an error.

Recover without losing ownership
Model every request through explicit durable states: RESERVED, QUEUED, RUNNING, CANCEL_REQUESTED, USAGE_UNCERTAIN, and TERMINAL. Cancellation finishes only after the worker halts generation and frees all KV/adapter references.
- Before the first token streams: a retry is transparent if ownership checks prevent two workers from running the same attempt simultaneously.
- After tokens have streamed: restarting blindly appends duplicate or contradictory text. Either implement a resumable sequence-numbered stream or close with a clear partial-output error.
- Poison requests: quarantine after limited retries using scoped IDs and diagnostic metadata rather than copying raw tenant prompts into shared dead-letter queues.
- Model upgrades: pin immutable revisions, isolate incompatible KV namespaces, drain in-flight references before unloading old weights, and verify that any shadow traffic is authorized and accounted for in compute budgets.

Diagnose the opening incident with evidence
Return to the opening outage: stalled queues at 55% GPU utilization plus Tenant A's postmortem leaking into Tenant B's summary.
- Contain the leak first. Pause the suspect retrieval or cache path, preserve scoped diagnostic traces, and inspect gateway identity propagation, database access predicates, and prefix-cache salts. Never treat an isolation breach as a performance ticket.
- Isolate the bottleneck second. Inspect queue age, active sequence count, KV-block occupancy, preemption rate, adapter swap misses, and per-tenant TTFT/ITL. A 55% GPU compute reading commonly hides 100% KV-cache saturation from long prompts (like A's design review) or head-of-line blocking from unchunked prefills. Add replicas only once telemetry confirms capacity, rather than scheduling or memory fragmentation, is the constraint.
Follow-up questions
How would you recover a reservation whose worker disappeared after producing some tokens?
Keep the reservation in USAGE_UNCERTAIN rather than refunding immediately. Reconcile durable dispatch records against worker heartbeats and event logs, confirm the old execution can no longer emit tokens or hold pages, settle confirmed partial usage under the declared billing rule, and guard any replacement attempt with ownership checks.
What would justify sharing cached prefixes across two principals in one tenant?
Both principals must hold identical permissions over every document embedded in the prefix, plus identical model, adapter, and tokenizer revisions. Map them to a server-resolved shared trust group, authorize cache reads on every hit, and bump the group's ACL revision immediately on any permission revocation.
What strong answers show
- Identity is resolved server-side before retrieval, adapter lookup, quota reservation, cache access, or billing, and workers cannot be bypassed around the gateway.
- Quota and KV-page reservations are atomic, idempotent, and hold uncertain attempts until execution terminates.
- KV sizing uses exact GQA head counts and integer block math, while deficit scheduling charges work units rather than raw request counts.
- Logical isolation (salts, quotas, queues) is clearly distinguished from OS, MIG, VM, and physical-node security perimeters.
- Usage events, step-cost allocation, and customer tariffs conserve units cleanly and handle duplicate deliveries safely.