Skip to main content

LLM Serving and Model Gateways

1. Executive Summary​

LLM serving exposes large language models as production APIs with stringent latency, throughput, and cost requirements. Unlike traditional microservices, inference workloads are GPU-bound, memory-shaped by KV cache, and bimodal—prefill (compute-intensive, parallel) vs decode (memory-bandwidth-intensive, sequential). Production stacks use specialized runtimes (vLLM, TensorRT-LLM, TGI) with continuous batching, PagedAttention, and quantization (FP8, INT4).

A model gateway sits in front of inference clusters as the control plane: authentication, rate limiting, routing across model versions and providers, fallback chains, prompt caching, cost attribution, and policy enforcement (PII, content filters). Examples include custom Kong/Envoy layers, LiteLLM, cloud Azure API Management patterns, and hyperscaler Bedrock/SageMaker endpoints.

Principal architects design gateways to prevent thundering herds, noisy neighbor GPU contention, and unbounded token spend—while maintaining streaming SSE compatibility and observability per tenant.

2. Why This Topic Matters​

Every enterprise LLM deployment needs a serving layer interview narrative:

  • Why not call OpenAI directly from every app? — Governance, cost, routing.
  • TTFT vs inter-token latency? — Different optimization levers.
  • Autoscaling GPU inference? — Cold start, scale-to-zero tradeoffs.
  • Multi-model routing? — Cheap model for classification, large for generation.
  • Gateway failure modes? — Timeouts, circuit breakers, partial streams.

Treating LLM inference as "just another REST service" ignores GPU scheduling and token economics.

3. Problems Being Solved​

ProblemServing/gateway approach
Low GPU utilizationContinuous batching, request pooling
High latency tailQueue management, priority tiers
Uncontrolled API spendRate limits, quotas per tenant
Model version rolloutCanary routing in gateway
Multi-vendor modelsUnified OpenAI-compatible API
ComplianceAudit logs, data residency routing
Prompt injection at edgeInput sanitization policies

Workload fit matrix​

PatternFitCaveat
Chat assistant (streaming)StrongSSE timeout handling
Batch document summarizationStrongAsync job queue better at scale
Real-time code completionStrongUltra-low TTFT requirement
Embedding APIStrongDifferent hardware profile
Fine-tune on requestWeakSeparate training pipeline

4. Assumptions and System Model​

AssumptionImplication
Requests are stateless at gatewaySession state in app or cache
Models loaded in GPU memoryWarm pool vs cold start latency
Streaming responsesLong-lived HTTP connections
Token meteringBill and limit by input+output tokens
Provider SLAs varyFallback and circuit breaking required

Safety: Tenant A cannot access Tenant B prompts in logs or cache. Liveness: Under overload, degrade gracefully (queue, 429) rather than OOM crash.

5. Essential Terminology​

TermDefinition
TTFTTime to first token
TPOTTime per output token (inter-token latency)
Continuous batchingDynamic grouping of inference requests
PagedAttentionNon-contiguous KV cache memory management
Model gatewayPolicy and routing layer before inference
Speculative decodingDraft model proposes; target verifies
Prefix cachingReuse KV for shared prompt prefixes
QuantizationReduced precision weights (INT8/FP8/INT4)
Disaggregated servingSeparate prefill and decode workers
OpenAI-compatible API/v1/chat/completions contract

6. Core Mechanism​

6.1 Serving architecture​

Figure 1: Gateway centralizes policy; router directs to internal or external inference backends.

6.2 Request lifecycle (streaming)​

Figure 2: Streaming path maintains long connection; gateway meters tokens on completion.

6.3 Routing and fallback​

Figure 3: Cascaded routing optimizes cost and latency; fallback handles capacity limits.

7. Step-by-Step Walkthrough​

Walkthrough A: Chat completion through gateway​

  1. App sends JWT; gateway validates tenant T1, checks token quota.
  2. Router selects llama-3-70b pool based on model header.
  3. vLLM worker assigns request to continuous batch; prefill runs.
  4. Tokens stream via SSE; gateway proxies without buffering full response.
  5. On finish_reason=stop, gateway decrements quota, emits billing event.

Walkthrough B: Canary deployment​

  1. New model weights v2 deployed to 10% of GPU pool.
  2. Gateway routes 10% traffic by consistent hash on tenant_id.
  3. Compare TTFT, error rate, quality evals vs v1.
  4. Promote to 100% or rollback based on SLO gates.

Walkthrough C: Prefix cache hit​

  1. System prompt identical across requests (RAG template).
  2. Inference runtime hashes prefix; KV cache reused.
  3. TTFT drops for subsequent requests sharing prefix.
  4. Gateway may route same-tenant requests to warm replica (session affinity optional).

Walkthrough D: Overload protection​

  1. Queue depth exceeds threshold; gateway returns 429 with Retry-After.
  2. Circuit opens to external provider after 5xx spike.
  3. Degrade to smaller model for non-critical tier tenants.
  4. Alert on-call; autoscaler adds GPU nodes (5–10 min lag).

Walkthrough E: Multi-tenant fair queuing​

  1. Premium tenant tier gets dedicated GPU pool with guaranteed concurrency.
  2. Standard tier shares pool with weighted fair queue—max 20 concurrent requests per tenant.
  3. Batch embedding jobs routed to separate low-priority pool overnight.
  4. Gateway tags requests with tenant_id and priority for metrics and billing.
  5. Monthly review adjusts weights based on contract SLAs and actual utilization.

Walkthrough F: Observability-driven tuning​

  1. Trace shows 40% of TTFT in gateway auth + policy; optimize JWT validation cache.
  2. Inference trace reveals batch size 1 dominating—enable min batch wait 10ms for standard tier.
  3. Prefix cache hit rate 12%—identify top 5 system prompts for cache warming.
  4. External API fallback triggered 200 times/day—provision additional internal capacity.
  5. SLO dashboard green after tuning; document changes in runbook.

Gateway capability checklist​

CapabilityProduction requirement
Per-tenant authOAuth2/JWT validation
Token meteringInput + output + cached tokens
Model routingHeader or intent classifier
Streaming proxySSE without full buffer
Canary by hashConsistent tenant routing
Circuit breakerPer backend provider
Audit logRequest ID, model, tokens, latency
PII filterOptional pre/post generation

8. Invariants and Guarantees​

PropertyGuarantee
AuthenticationUnauthenticated requests rejected
Quota enforcementHard stop at tenant limit (configurable soft/hard)
Audit trailRequest metadata logged; content policy varies
Streaming integrityChunks ordered; terminal chunk signals completion
No cross-tenant cache leakPrefix cache scoped per tenant/model

Inference correctness is probabilistic (sampling); availability and cost bounds are architectural guarantees.

9. Failure Scenarios​

FailureBehaviorMitigation
GPU OOMRequest failed mid-streamMax sequence limits; preemption
Worker crashClient sees disconnectRetry idempotency keys; gateway retry
Provider outageFallback model or queueMulti-provider routing
SSE proxy timeoutTruncated responseAlign LB idle timeout with max generation
Thundering herdLatency explosionRate limit + queue + autoscale
Bad canaryQuality regressionAutomated eval gates; fast rollback
Token count mismatchBilling driftReconcile with inference server logs

10. Performance Characteristics​

DimensionBehavior
TTFTPrefill length, batch contention, cache hit
ThroughputTokens/sec scales with batching
Cold startModel load seconds–minutes
Gateway overheadTypically ms vs GPU seconds
Quantization~1.5–2× throughput; accuracy tradeoff [model-specific]

11. Scalability Limits​

  • GPU memory caps concurrent sequences.
  • LB long connections limit connections per instance.
  • Central gateway may bottleneck—horizontal scale stateless tier.
  • External API rate limits cap burst routing.
  • Eval quality may limit aggressive model down-tiering.

12. Operational Considerations​

  • SLOs: p50/p99 TTFT, tokens/sec/GPU, error rate, queue wait.
  • Dashboards per model version and tenant.
  • Load test with realistic prompt length distribution.
  • Align timeouts: client < gateway < LB < inference max generation.
  • Runbooks for OOM, NCCL errors (multi-GPU), provider degradation.
  • Cost dashboards: $/1M tokens per model and tenant.
  • Prefix cache hit rate tracked; warm top system prompts after deploy.
  • Model artifact registry with signed weights and promotion workflow.
  • Synthetic probe every 60s per model pool for availability SLO.
  • FinOps weekly review of top 10 tenants by token spend.

13. Security Considerations​

  • API keys rotated; short-lived tokens preferred.
  • PII redaction in logs; optional prompt encryption in transit/at rest.
  • Prompt injection defenses at gateway and system prompt hardening.
  • VPC/private endpoints for internal model access.
  • Content filtering hooks (policy-as-code).

14. Cost Considerations​

  • GPU amortization: maximize batch utilization.
  • Model tiering: route 80% traffic to smaller/cheaper models.
  • External API premium vs self-hosted break-even analysis.
  • Idle GPU cost—scale-to-zero vs warm pool for TTFT SLO.
  • Output token pricing dominates long generations—max_tokens limits.

SSE and load balancer timeout deep dive​

A common production incident: chat works in staging but truncates at exactly 60 seconds in production. Root cause: AWS ALB idle timeout default 60s while generation continues 90s. Gateway streams bytes but LB severs connection. Fix: raise idle timeout above max generation time; enable TCP keepalive; document client reconnect for resumable streams if product supports it. Principal architects validate full path timeouts—client, gateway, LB, inference—not each layer in isolation.

Model routing economics (illustrative framework)​

Classify intents with a 8B router model costing ~$0.10 per 1M input tokens vs 70B at ~$2.00 [verify vendor pricing]. If 70% of requests are simple FAQ, routing saves ~45% inference spend before quantization. Track misroute rate—router sending complex queries to small model damages quality. Online eval samples flagged conversations for human review weekly.

Cold start vs warm pool decision​

SLOStrategy
TTFT p99 < 2s interactiveWarm minimum 2 replicas per model
Batch overnight onlyScale to zero acceptable
Burst marketing eventPre-warm 24h before; load test

FinOps may push scale-to-zero; product SLO may forbid it—document tradeoff in ADR with dollar cost of warm pool vs lost conversion from slow TTFT [business input required].

15. Production Implementations​

Case study: Enterprise LLM platform (illustrative)​

Context​

50 internal apps; mix of self-hosted Llama and OpenAI fallback; $200k/month GPU budget.

Architecture​

Envoy gateway + custom auth + LiteLLM router. vLLM on K8s with HPA on queue depth. Prefix cache for shared RAG system prompts.

Results (illustrative)​

67% GPU utilization after continuous batching tuning; 40% cost reduction via small-model routing for classification intents.

Extended operations narrative​

Launch day incident: ALB 60s idle timeout truncated CEO demo stream at token 847. Fix raised timeout to 300s and added gateway keepalive comments in SSE stream. Canary deploy of Llama fine-tune blocked when safety eval detected 2 new jailbreak passes—rollback automated via feature flag. Multi-tenant fair queue prevented one batch embedding job from starving interactive chat during month-end reporting crunch.

Tradeoffs​

ChoiceTradeoff
Self-host 70BControl vs ops burden
LiteLLMVelocity vs deep customization
Hard quotasUser friction vs budget protection

16. Alternatives and Tradeoffs​

ApproachProsCons
Direct vendor APIZero opsCost, data egress, lock-in
Self-hosted vLLMControl, cost at scaleGPU ops
Serverless inferenceSimple scaleCold start, cost unpredictability
Edge deploymentLow latencyModel size limits

17. Common Misconceptions​

MisconceptionReality
"Gateway adds too much latency"ms vs GPU seconds
"One model fits all tasks"Routing saves cost and latency
"Streaming is optional"UX standard for chat
"Autoscale is instant"GPU provisioning lags
"max_tokens=4096 always fine"Cost and latency explode

18. Principal Architect Perspective​

  1. Gateway is mandatory for multi-tenant enterprise LLM—not optional middleware.
  2. Define SLOs per tier—premium vs standard queues.
  3. Invest in routing intelligence early—biggest cost lever.
  4. Treat inference as stateful at GPU layer despite stateless HTTP.
  5. Eval gates on every model promotion—not just latency metrics.

The gateway is where enterprise AI economics and risk meet engineering. Every principal roadmap should sequence: auth and quotas first, routing and cost second, model quality optimization third—never invert that order. Load tests must include long SSE streams through production load balancers, not just direct-to-vLLM benchmarks in staging.

Operating playbook (first 90 days)​

Days 1–30: Deploy gateway with auth, quotas, and request tracing. Block direct GPU access from application pods.

Days 31–60: Implement canary routing and model-tier classifier; measure cost per successful request by tenant.

Days 61–90: Load test with realistic prompt-length distribution; fix LB SSE timeouts. Publish inference SLO dashboard for executives.

19. Architecture Review Exercise​

Scenario: Apps call vLLM pods directly via K8s service; no auth; shared GPU pool for batch and interactive.

Findings: No cost control, security gap, batch jobs starve chat. Mandate gateway, separate pools, quotas.

20. Whiteboard Explanation​

"Applications talk to a model gateway, not directly to GPUs. The gateway authenticates tenants, enforces rate limits, and routes to the right model—small for classification, large for generation, external API as fallback. Behind it, vLLM servers use continuous batching: new chat requests join a GPU batch dynamically, and PagedAttention manages KV cache memory like virtual memory. Prefill processes the prompt in parallel; decode streams tokens one at a time. The gateway proxies SSE streams, meters tokens for billing, and supports canary deployments. Autoscaling watches queue depth, but GPU cold starts mean you keep a warm pool for interactive SLOs."

Principal addendum: Gateway owns policy; GPU tier owns throughput. Align LB SSE timeouts with max generation. Model routing is the top cost optimization.

21. Interview Questions​

  1. TTFT vs TPOT? — First token latency vs subsequent token spacing.
  2. Continuous batching benefit? — Higher GPU utilization vs static batching.
  3. PagedAttention? — Efficient non-contiguous KV cache allocation.
  4. Model gateway responsibilities? — Auth, routing, limits, observability.
  5. Why OpenAI-compatible API? — SDK reuse across providers.
  6. Canary inference deploy? — Traffic split; SLO and eval gates.
  7. Prefix caching? — Reuse KV for shared prompt prefixes.
  8. Quantization tradeoff? — Speed/cost vs accuracy.
  9. SSE timeout issues? — LB idle timeout vs long generations.
  10. Disaggregated prefill/decode? — Scale each phase independently.
  11. Speculative decoding? — Draft+verify for faster decode.
  12. Rate limit dimensions? — RPM, TPM, concurrent requests.
  13. Fallback chain design? — Primary internal → external → smaller model.
  14. Multi-tenant GPU isolation? — Separate pools or strict scheduling.

Scoring rubric (principal)​

DimensionStrongWeak
Latency modelPrefill/decode split"GPU slow"
GatewayPolicy + cost + routing"API proxy"
ScaleBatching, warm pool"Just add pods"
OpsSLOs, canary, quotasIgnores overload

Extended scoring notes​

Principal bar: Gateway vs inference separation clear. TTFT/TPOT and SSE timeout chain mentioned. Weak hire: "Deploy vLLM behind load balancer" only.

  1. PagedAttention benefit? — KV memory fragmentation fix.
  2. Speculative decoding? — Draft model speedup.
  3. Token quota vs RPM limit? — Cost vs concurrency control.

22. Interview Follow-Ups​

  1. Design gateway for 100 tenants, 3 models. — Auth, per-tenant quota, router, observability.
  2. p99 TTFT regression after deploy. — Canary comparison, batch contention, prefix cache invalidation.
  3. When self-host vs API? — Volume break-even, data residency, ops maturity.
  4. Handle 10-minute generation. — Stream keepalive, LB timeout, client reconnect policy.
  5. Cost cap per user per day. — Gateway token accounting; hard stop vs notify.

Additional principal scenarios​

Scenario: Security mandates all prompts logged in clear text. Answer: Push back—log hashes/metadata; clear text only with legal retention policy and restricted access; offer opt-in debug sessions.

Scenario: Vendor API outage during product launch. Answer: Pre-negotiated fallback model on self-hosted pool; circuit breaker already tested; communicate degraded mode SLA to product.

Scenario: Finance sees 3× token bill after feature launch. Answer: Audit max_tokens defaults; enable router model; prefix cache for system prompts; per-tenant quotas with alerting at 80% budget.

22A. Production Low-Level Design (Interview + Game Day)​

Model gateway + GPU inference tier with quotas and fallback. Production LLD and Game Day Standard.

Control vs data plane​

PlaneComponents
ControlGateway (auth, quotas, routing), model registry, cost attribution
DatavLLM/TensorRT replicas, KV cache, continuous batching queue

Step-by-step request path:

StepActionSLO lever
1Auth + tenant quota (RPM/TPM)429 before GPU overload
2Route to model version / size tierCost vs quality
3Queue + continuous batchingThroughput
4SSE stream tokens; LB idle timeout alignedLong generation
5Circuit breaker on vendor fallbackDegraded mode SLA

Gateway vs inference contract​

ConcernGatewayInference server
Auth / quotas✓
Batching / KV cache✓
PII filter metadata✓
TTFT / TPOTmeasuresoptimizes

One SLO​

Inference availability: 99.9% successful completions; p99 TTFT < 800ms at P50 prompt length; token budget hard-stop at tenant daily cap.


22B. Game Day Playbooks​

#DrillInjectDetectMitigateVerify
GD-1GPU pool saturation2× traffic spikeQueue depth + TTFT burnScale replicas; shed tier-2p99 TTFT recovers
GD-2Vendor API outageBlock external routeCircuit open metricFallback to self-hosted poolPremium tier OK
GD-3Token budget blowoutRemove max_tokens capCost alert 3× baselineRestore caps; prefix cacheDaily spend normalized

GD-1 — Launch day overload​

23. Strong Answer Example​

Question: "What belongs in a model gateway vs the inference server?"

Answer this as a 45-minute platform split using the PRACTICE framework. Below is how a strong student sounds aloud—policy at gateway, GPU math at inference.


Student interview script (PRACTICE)​

P — Problem framing​

"Clients need one stable API; platform needs routing, quotas, and audit without coupling to vLLM vs TensorRT. I'll split gateway (control) from inference (data plane)."

R — Requirements​

TypeRequirement
FROpenAI-compatible API; multi-model routing; streaming
NFRStateless gateway scale; GPU efficiency on inference
SafetyPer-tenant quotas; PII metadata logging policy
OpsCost attribution; fallback chain

A — Assumptions​

AssumptionImplication
100 tenants, 3 model sizesRouter + pools
Self-host + vendor mixCircuit breaker fallback
SSE long responsesLB timeout alignment

C — Capacity​

Gateway → stateless; 10K RPS horizontal
Inference → batching; TTFT vs TPOT tradeoff
Quotas → RPM + TPM + daily token cap
Prefix cache → system prompt reuse saves 30–50% tokens [workload-dependent]

T — Topology​

"Gateway: auth, quotas, logging, routing, circuit break, cost tags. Inference: model load, continuous batching, KV cache, quantization, stream generation. Internal OpenAI-compatible contract with mTLS and trace IDs."

I — Invariants​

"Policy never only in inference tier. Swap inference backend without client change."

E — Evolution​

PhaseAdd
M1Gateway + single vLLM pool
M2Quotas + fallback
M3Multi-model router + cost dashboard

Closing line​

"Gateway owns platform concerns; inference owns GPU efficiency—nginx alone is not a gateway; batching and KV cache are not gateway problems."

24. Weak Answer Example​

Weak: "Put nginx in front of the model; it scales automatically."

Red flags: No batching, quotas, routing, or streaming considerations.

25. Hands-On Exercise​

  1. Deploy vLLM locally; measure TTFT vs prompt length.
  2. Add LiteLLM proxy routing two models; test fallback.
  3. Load test with Locust; observe queue and 429 behavior.
  4. Configure SSE through reverse proxy; test idle timeout.
  5. Sketch gateway components for your org's compliance needs.

26. Knowledge Check​

  1. TTFT measures? (Time to first output token.)
  2. Continuous batching? (Dynamic request grouping on GPU.)
  3. Gateway rate limits why? (Cost and fairness.)
  4. KV cache used in? (Decode phase.)
  5. Canary routing? (Partial traffic to new version.)
  6. PagedAttention solves? (KV memory fragmentation.)
  7. Prefix cache benefit? (Faster prefill for shared prompts.)
  8. SSE used for? (Streaming token delivery.)
  9. Quantization trades? (Accuracy for speed/memory.)
  10. OpenAI-compatible API why? (Interoperability.)
  11. TPOT measures? (Time per output token.)
  12. Warm pool purpose? (Avoid cold-start TTFT.)
  13. Circuit breaker protects? (Cascading provider failures.)

27. Flashcards​

FrontBack
TTFTTime to first token
TPOTTime per output token
Continuous batchingDynamic GPU request batching
PagedAttentionPaged KV cache management
Model gatewayAuth, routing, limits layer
Prefix cachingReuse KV for shared prompts
Speculative decodingDraft model accelerates decode
QuantizationLower precision weights
SSE streamingServer-sent events for tokens
Token quotaPer-tenant usage limit

28. Cheat Sheet​

SERVING STACK
Apps → Gateway (auth/limit/route) → vLLM/TGI → GPU

LATENCY
TTFT = prefill | TPOT = decode | batching affects both

GATEWAY
Quotas, canary, fallback, audit, OpenAI-compatible API

OPS
Warm pool, HPA on queue, LB SSE timeouts, $/token dashboards

PRINCIPAL ANCHORS
Gateway before GPU for policy
TTFT vs TPOT different tuning
SSE timeout full-path test
Router model saves cost
Canary + eval on promote
Quotas prevent bill shock
Prefix cache for RAG prompts
Self-host vs API break-even math

30. References​

Primary sources​

  • Kwon, W., et al. (2023). Efficient Memory Management for Large Language Model Serving with PagedAttention. SOSP (vLLM).
  • NVIDIA TensorRT-LLM documentation — inference optimization.
  • Hugging Face TGI documentation — production serving patterns.
  • LiteLLM, Envoy AI gateway patterns — implementation choices.
  • OpenAI API reference — de facto contract standard.

Principal study path​

Read Distributed Training and Inference for GPU parallelism context, RAG Architecture for retrieval-serving integration, Caching Fundamentals for prefix cache design, and SLO, SLI, and Error Budgets for inference SLO governance. Together these chapters form the production LLM platform stack interview narrative.

Distinction​

ClaimType
PagedAttention mechanismvLLM paper
Quantization speedupsHardware and model specific
Gateway feature setsProduct/vendor specific