Serving, Gateways, and Compute
A model is not served merely because an endpoint returns text. It is served when a declared class of requests receives acceptable results within a latency, availability, cost, and policy envelope. The unit to operate is therefore a versioned serving contract: the model artifact, runtime, adapter, request semantics, route policy, and compute placement that jointly produced the result. This extends the versioned served system from Chapter 81 into production.
The theory behind the runtime is developed in Chapter 31 and Chapter 32. This chapter turns it into an operating method. First freeze the contract. Then measure the complete path under representative load. Only after that should a team choose an engine, insert a gateway, or buy capacity. Those components are means, not the architecture's starting point.
Freeze the serving contract
An HTTP request shape is useful plumbing, but it is not a behavioral standard. Providers differ in message roles, tool and JSON-schema dialects, tokenization, stream events, stop conditions, usage fields, errors, safety behavior, data handling, and model-specific defaults. Official documentation shows the gap: Anthropic lists transformed, ignored, and unsupported fields, while Google recommends its native API when an application is not already tied to the common client (Anthropic n.d.; Google n.d.). Treat an OpenAI-shaped API, or any other shared envelope, as a common transport subset. Put provider-specific behavior behind adapters and retain native escape hatches where the common subset is insufficient.
The internal request contract should state at least the following.
| Concern | What the contract must fix |
|---|---|
| Identity | Tenant, caller, request class, trace ID, and idempotency key when an effect may occur |
| Model input | Message roles, prompt template, tool schemas, media formats, context policy, and token limits |
| Generation | Sampling and reasoning settings, structured-output dialect, stop behavior, and allowed tool choice |
| Lifecycle | Deadline and cancellation, streaming and error behavior, backpressure, and overload response |
| Policy | Data classification and region, retention, provider allowlist, safety policy, and budget |
| Effects | Read-only or side-effect class, authorization subject, retry eligibility, and duplicate-effect handling |
| Evidence | Selected deployment, adapter version, usage, finish reason, quality check, and correlated trace |
For each provider adapter, run an adapter conformance corpus. It should cover ordinary messages, every tool/schema feature the application uses, partial and cancelled streams, context-limit errors, usage accounting, timeouts, rate limits, refusals, and malformed responses. A field that is transformed, emulated, or dropped must be rejected or reported; silently ignoring it is not conformance. Record that outcome as a translation loss report. The same corpus must run against every candidate route and during every adapter upgrade.
The deployment identity must include every layer that can change behavior. One useful fingerprint is
where is the provider adapter and version; the engine image and configuration; the model, tokenizer, and artifact digests; the prompt template, tool parser, and structured-output settings; the numerical precision and parallel placement; the attention and KV-cache policy; the decoding, batching, and admission policy; the accelerator and network topology; and the route, retry, and fallback policy. Every symbol denotes a versioned record, not a product name. Changing any term creates a new candidate that must pass the relevant evaluation again.
Figure 82.2 shows the boundary. A mediation service is optional, and the self-managed branch is only one possible deployment mode. The contract and evidence cross every branch.
Measure what the user waits for
The engine processes a prompt during prefill, then produces further tokens through autoregressive decode. Prefill often has enough parallel work to be compute-limited; decode often has low arithmetic intensity and becomes limited by memory traffic. These are common regimes, not laws. Prompt length, batch shape, model architecture, kernels, cache state, and hardware can move either phase to another bottleneck. Measure the exact fingerprint rather than assigning it a bottleneck by name.
For a simple request with output tokens, a useful critical-path decomposition is
Here is admission and scheduling delay; includes the work needed to produce the first token; is the interval for output token ; and is serial tool or external-service time on the critical path. For parallel branches, add the maximum branch duration, not their sum. Network and client buffering should be recorded separately when they are material.
Time to first token (TTFT) measures the interval from accepted request to the first visible token. For , time per output token (TPOT) is commonly reported as the elapsed time from first to last visible token divided by . End-to-end latency still matters: an attractive TTFT can hide a slow decode, tool call, or retry. Report p50, p95, and p99 by request class, together with the rejected and timed-out fraction. A system can otherwise manufacture good latency by refusing its hardest traffic.
Engine mechanisms are conditional levers
Modern serving runtimes combine several techniques. Their history clarifies what each one actually solves. Orca introduced iteration-level scheduling: the active batch can change between decoding iterations instead of waiting for a static batch to finish (Yu et al. 2022). vLLM's PagedAttention applied block-based allocation to the growing KV cache, reducing over-reservation and fragmentation that limited resident sequences (Kwon et al. 2023). Later systems explored chunked prefill and separate prefill/decode placement (Agrawal et al. 2024; Zhong et al. 2024). None of these results establishes a universal production default.
| Mechanism | Potential benefit | Prerequisite and cost |
|---|---|---|
| Iteration-level or continuous batching | Rebuilds the active set as requests arrive and finish, improving scheduling opportunities | Admission and batch policy change TTFT, token cadence, fairness, and memory pressure |
| Paged KV allocation | Avoids requiring one contiguous maximum-size cache allocation and reduces wasted capacity | Block metadata, tail waste, cache eviction, and kernel support remain |
| Exact prefix reuse | Skips repeated prefill work for an exact tokenized prefix | Model revision, template, adapter state, positions, cache format, multimodal state, and isolation policy must match; changing document order or timestamps causes misses |
| Quantization | Reduces some combination of weight, activation, or KV memory and may enable faster kernels | Name the quantized object: weights, activations, or KV state; calibration, scales, kernel availability, and task quality all require verification |
| Speculative decoding | Uses a proposer and target acceptance rule to reduce serial target steps | Acceptance rate, proposer cost, memory, batch shape, and distribution-preservation assumptions determine the gain; see Chapter 33 |
| Grammar-constrained output | Restricts completed output to a supported grammar | Guarantees syntax, not semantic correctness, authorization, safe tool choice, or a complete non-refused stream (Dong et al. 2025) |
| Chunked prefill | Interleaves bounded prompt chunks with ongoing decodes to reduce long generation stalls | Adds scheduler choices and can change TTFT, throughput, and cache behavior |
| Prefill/decode disaggregation | Isolates phase interference and permits independent placement and sizing | Adds KV transfer, network queueing, duplicated capacity, routing state, and new failure boundaries |
Prefix reuse is not semantic caching: similar meaning does not imply reusable KV state. It also saves prefill work only. When decode dominates or prefixes do not repeat, retaining cached state can be a net capacity cost. Likewise, a lower-bit artifact can be slower if the runtime lacks an efficient kernel and must repeatedly convert it. Validate the exact artifact on the exact hardware, then rerun the task-quality gate from Chapter 87.
Structured decoding deserves the same restraint. After generation, check the finish reason, parse and schema result, domain invariants, caller authorization, and any effect-specific preconditions. A syntactically valid request to transfer money is still an authorization decision. That boundary connects serving to Chapter 56.
Benchmark the complete fingerprint
A useful comparison fixes a production-shaped, open-loop arrival trace. It preserves the observed request-class mix, input and output length distributions, prefix-reuse distribution, tool and schema mix, cancellations, and bursts. Open-loop means arrivals follow the recorded schedule rather than waiting for the previous response; otherwise a slow system reduces its own offered load and looks deceptively stable.
For each candidate:
- Pin the full fingerprint and verify adapter conformance and task quality.
- Run both cold and representative warm-cache conditions.
- Sweep offered load from idle through saturation without changing the trace.
- Record admitted and rejected requests, TTFT, TPOT, end-to-end tails, output rate, memory, power where available, and cost per accepted task.
- Change one mechanism at a time. A one-feature ablation makes the cause of a gain inspectable.
- Repeat enough trials to expose variance, then test worker loss, network delay, cancellation, and recovery.
Do not compare two engine names while also changing model revision, template, precision, hardware, or request mix and attribute the result to the engine. If those changes are intentional, compare them as two complete served-system candidates.
A configuration is eligible only when it passes every hard constraint and its joint service objective. For request class , define all three gates together:
Here covers such constraints as license, region, interface, and capacity; is measured quality with threshold ; , , and are the TTFT, TPOT, and end-to-end limits; and is the required joint attainment rate. Unknown hard-constraint evidence does not pass. Optimize cost or throughput only over this feasible set, and always publish the admission rate beside the conditional latency result.
Use mediation where it earns its boundary
A gateway or mediation service is a design option, not a mandatory layer. It can centralize identity, credential references, quotas, routing, budgets, and telemetry when many callers or providers would otherwise duplicate them. A direct, well-tested adapter can be simpler for one application and one provider. The question is whether centralization creates a boundary the team can actually enforce and operate.
A mediation boundary is valuable only if its invariants are explicit:
- Authenticate the caller and tenant, then authorize the requested model, tools, data class, and effect. Possession of a proxy key is not sufficient.
- Route only within the allowed candidate set that has passed the same contract and policy-compatible fallback evaluation.
- Check and reserve the budget atomically before dispatch; reconcile the reservation against measured usage afterward.
- Propagate the original remaining deadline and cancellation. A retry does not receive a fresh timeout.
- Retry only when the failure class and operation are eligible, an idempotency key or read-only guarantee makes repetition safe, and budget and deadline remain. An ambiguous outcome is not proof that no work occurred.
- Correlate every attempt, selected deployment, policy decision, and result in one trace. W3C Trace Context defines interoperable propagation fields for that purpose (Kanzhelev et al. 2021).
- Fail closed when identity, authorization, route qualification, or budget evidence is missing. An observability outage should not silently disable policy.
Separate the administrative control plane---where routes, policies, and credentials change---from the request data plane that evaluates those records. Protect and audit both. Prefer workload identity or short-lived, audience-restricted credentials over long-lived bearer keys, and resolve provider secrets through a credential reference rather than copying them into route configuration.
HTTP distinguishes idempotent methods, but inference is commonly invoked with
POST, and the generated result may cause an external action. Transport retry
safety therefore cannot establish application-effect safety by itself
(Fielding et al. 2022). If a timeout occurs after a tool may have executed, query
the effect ledger by idempotency key or surface an ambiguous outcome for
reconciliation. Do not blindly submit the action to a different model.
Assign one retry owner and set a bound on total attempts. Nested retries in the client, mediator, and provider SDK multiply rather than add. Distinguish a transport retry to the same deployment, provider failover to another qualified deployment, and model fallback to a different served system. A fallback is eligible only before the first byte has reached the caller and only within the same contract; once streaming has started, return a visible partial-stream failure. Never treat authentication, authorization, invalid input, or an unreconstructable stateful session as a reason to try another provider.
The following policy object is illustrative. It is an internal design record, not a promise that a particular gateway accepts this schema.
route_contract: support-summary-v3
request_class: read_only_summary
candidates:
- deployment_ref: hosted-eu@sha256:...
adapter_ref: adapter-a@sha256:...
data_classes: [internal]
regions: [eu]
- deployment_ref: managed-eu@sha256:...
adapter_ref: adapter-b@sha256:...
data_classes: [internal]
regions: [eu]
credential_ref: workload-identity://inference/support
deadline_ms: 2500
budget_reservation: required
retry:
attempts: 1
eligible_failures: [connect_before_send, rate_limited]
requires: read_only_or_idempotency_key
fallback:
requires_same_contract: true
telemetry:
record_content: false
propagate_trace_context: true
This route has two candidates only because both have passed the conformance, quality, region, and load gates. A different model behind the same alias is not automatically a fallback. It can change tool behavior, safety policy, context limits, data handling, latency, and price.
Record low-cardinality operational facts by default: request class, logical route, deployment version, token counts, finish reason, latency, attempts, policy outcome, and cost ledger references. OpenTelemetry provides evolving generative-AI semantic conventions, but input and output content can contain PII and other sensitive data (OpenTelemetry n.d.). Keep prompts, completions, tool arguments, and retrieved documents out of ordinary telemetry unless a specific retention, access, and redaction policy authorizes them.
Keep an unsampled logical request ledger separate from sampled diagnostic traces. The ledger links every attempt to the route revision, actual provider and deployment, upstream request ID, translation loss, usage, price version, estimated charge, policy decision, and stream termination. Reconcile that ledger with provider invoices; a local estimate is not invoice reconciliation and cannot by itself prove a hard budget ceiling.
Procure capacity from measurements
Deployment and procurement are separate choices. A team can use a first-party API, a provider-operated managed endpoint for a selected artifact, or a self-managed engine on rented or owned accelerators. The right comparison applies the same hard constraints, quality gate, workload, and accounting horizon to all three.
| Dimension | Questions to record |
|---|---|
| Charging | Per token, request, accelerator-second, or reserved interval? What is the billing quantum and minimum commitment? |
| Capacity | Is there a capacity guarantee, quota, reservation, or merely best effort? How are bursts and admission handled? |
| Startup | What cold-start and scale-up delay applies, and what warm capacity must be paid while idle? |
| Failure | Is capacity interruptible? What notice, checkpoint, retry, and recovery behavior is supported? |
| Location | Which regions, data paths, storage tiers, and egress charges apply? |
| Hardware | Which accelerator, memory size, interconnect, host bandwidth, and topology are actually allocated? |
| Operations | Who owns patches, rollout, monitoring, incident response, and capacity forecasting? |
| Exit | Can artifacts, logs, adapters, and evaluation evidence move without changing the serving contract? |
There is no universal utilization percentage at which one mode becomes cheaper. Capacity is purchased in steps, demand is bursty, warm replicas and redundancy are billed, and a cheaper route may produce fewer acceptable results. Model low, base, and high demand, including capacity steps, commitments, interruption, egress, operations labor, and incident cost. Then compare the end-to-end cost per accepted task from Chapter 81 and Chapter 76.
Establish a planning bound, then load-test it
Here is the declared peak arrival rate, is measured accelerator-seconds of service per request under the production mix, and is a chosen planning utilization cap. A rough lower bound on accelerator count is
Here the expression is a planning approximation, not an autoscaling formula. Dynamic batching makes service demand non-linear; prompt and output tails matter; tensor or pipeline placement can require several accelerators as an indivisible unit; and startup delay prevents instantaneous scale-out. For measured service rate at placement size , queue stability requires
where is offered load and is measured service rate for the placement. That necessary condition does not guarantee p95 or p99 latency. Validate with the open-loop sweep, then add explicit headroom and redundancy for failures, deploys, maintenance, and forecast error. Provisioned capacity is the feasible placement size plus those reserves, rounded to the supplier's allocation unit.
For each demand scenario, report accepted tasks, rejected tasks, quality-pass rate, SLO attainment, billed idle time, and total cost. Divide the full ledger by accepted tasks, not raw requests or generated tokens. That prevents a cheap but unreliable or low-quality path from appearing economical.
Choose orchestration by workload semantics
Once the team controls a pool, it needs placement and lifecycle controls. Do not choose the control plane from an industry popularity claim. Write down the workload first.
- A long-lived service needs health and readiness checks, rolling deployment, bounded disruption, stable addressing, traffic draining, autoscaling, and fast rollback.
- A finite distributed job may need gang scheduling, topology-aware placement, checkpoint and restart, preemption, priorities, and fair quota across teams.
- Both need accelerator identity, health, isolation, and accounting. Multi-node jobs also need network-topology constraints and all-or-nothing admission.
- A Python or other application execution framework can manage tasks and actors, but it does not replace cluster admission, device allocation, or service lifecycle controls.
Slurm is designed as a cluster workload manager with allocation, queueing, and job-step execution (SchedMD n.d.). Kubernetes supplies service controllers and pod placement; extensions such as Kueue add quota and workload admission without replacing the scheduler or job controller (Kubernetes SIG Scheduling n.d.). Either can be extended, and some organizations combine them. The durable distinction is the required lifecycle and scheduling semantics, not a slogan such as “training uses Slurm, inference uses Kubernetes.”
Autoscaling is bounded by physical reality. If a replica takes minutes to allocate hardware, load weights, compile kernels, and warm caches, a seconds-old queue signal cannot save the current request burst. Maintain warm capacity or admit less traffic, forecast ahead of known peaks, and test the full scale-up path. Device plugins and dynamic allocation likewise do not prove that a model fits, that the requested topology exists, or that degraded hardware is safe to serve.
Serving economics can influence training choices, but the causal chain must be measured. A smaller model may justify more training compute when its lifetime serving demand makes lower per-request cost valuable, as discussed in Chapter 5. That argument depends on accepted-result quality, expected volume, hardware, and accounting horizon; it is not implied by a low parameter count alone.
Operate the decision
The end-to-end workflow in Figure 82.3 keeps architecture and evidence together.
- Freeze the contract and workload. Declare request classes, arrival traces, quality gates, SLOs, admission policy, data rules, and cost horizon.
- Pin one baseline. Record the complete fingerprint and establish a simple, colocated configuration before adding cache reuse, speculation, or phase separation.
- Verify behavior. Run adapter conformance, task evaluation, safety checks, and structured-output invariants. Reject unknown hard-constraint evidence.
- Load test and size. Sweep offered load through saturation, including cold and warm states. Size capacity with headroom and redundancy, then price low, base, and high demand.
- Run failure injection. Exercise connection failure, provider
429and5xxresponses, slow streams, cancellation, worker loss, scale-up delay, corrupted output, budget exhaustion, and an ambiguous tool action. Verify that deadline, retry, authorization, and accounting invariants hold. - Roll out reversibly. Shadow production traffic where policy permits, canary by request class and tenant, and maintain tested rollback for the adapter, route, engine, model, and scheduler configuration.
- Keep an evidence record. Define a re-evaluation trigger for artifact or adapter change, provider behavior drift, workload shift, SLO regression, price or quota change, policy change, and scheduled expiry.
Prefill/decode disaggregation, learned routing, semantic caches, and multi-cloud failover enter this workflow as hypotheses. Add one only when the simpler baseline misses a declared objective and the new design wins under the same trace after transfer, duplicate capacity, policy, and failure costs are counted.
Teams reasonably disagree about where to place the operating boundary. A direct provider adapter minimizes moving parts; mediation can centralize policy and evidence across many callers. First-party APIs reduce operations; managed endpoints expose more artifact control; self-managed engines expose the whole runtime and capacity problem. Advanced schedulers can improve a measured bottleneck while creating another queue or failure domain. There is no durable product default. Preserve the versioned contract and compare complete candidates against the same workload.
The worked stack in Chapter 88 is one implementation of these boundaries. The method here is the acceptance test for it: an endpoint, gateway, or GPU pool earns its place only when the complete served system produces better accepted results without violating the contract.
Further reading
- Yu et al., “Orca: A Distributed Serving System for Transformer-Based Generative Models” (Iteration-level scheduling for autoregressive model serving), 2022. usenix.orgOrca introduces iteration-level scheduling so a generative-model server can rebuild its batch after each decoding step instead of waiting for a static batch to finish.
- Kwon et al., “Efficient Memory Management for Large Language Model Serving with PagedAttention” (Block-based KV-cache allocation in vLLM), 2023. arXiv:2309.06180vLLM uses PagedAttention to place fixed-token KV-cache blocks non-contiguously; its reported throughput gains are scoped to the complete evaluated system, workloads, models, and baselines.
- Zheng et al., “SGLang: Efficient Execution of Structured Language Model Programs” (Radix-tree management of reusable token prefixes), 2024. proceedings.neurips.ccSGLang includes compressed finite-state machines and jump-forward processing to reduce sequential decoding work across deterministic structured-output spans.
- Agrawal et al., “Taming Throughput-Latency Tradeoff in LLM Inference with Sarathi-Serve” (Chunked prefill and stall-aware batching), 2024. usenix.orgSarathi-Serve splits long prefills into chunks and schedules them with decodes to limit generation stalls while retaining batching opportunities.
- Zhong et al., “DistServe: Disaggregating Prefill and Decoding for Goodput-optimized Large Language Model Serving” (Separate prefill and decode placement under joint latency objectives), 2024. usenix.orgDistServe places prefill and decode on separate GPU pools and defines goodput through the arrival rate sustainable at a chosen TTFT and TPOT SLO attainment.
- Dong et al., “XGrammar: Flexible and Efficient Structured Generation Engine for Large Language Models” (Grammar-constrained decoding for structured syntax), 2025. proceedings.mlsys.orgXGrammar accelerates context-free grammar execution with prechecked tokens, persistent parser stacks, and overlap between grammar work and accelerator execution.
- Anthropic, “OpenAI SDK Compatibility” (Documents transformations, ignored fields, and native-feature gaps in a compatibility layer), n.d.. platform.claude.comAnthropic documents its OpenAI-SDK compatibility layer as a convenience for testing and lists semantic differences and unsupported or ignored request fields.
- Google, “OpenAI Compatibility” (Provider documentation for an OpenAI-shaped adapter and its limits), n.d.. ai.google.devGoogle documents how OpenAI-library calls map to Gemini and recommends native Gemini integration when an application needs provider-specific capabilities.
- Fielding et al., “HTTP Semantics” (Defines safe and idempotent HTTP methods and retry constraints), 2022. rfc-editor.orgRFC 9110 defines idempotency in terms of the requested server effect and limits automatic retry when a client cannot know whether a non-idempotent request was applied.
- Kanzhelev et al., “Trace Context” (Interoperable propagation of request identity across service boundaries), 2021. w3.orgW3C Trace Context standardizes distributed correlation headers; those headers are not an authorization channel and must avoid sensitive data.
- OpenTelemetry, “OpenTelemetry Generative AI Semantic Conventions” (Common telemetry attributes and warnings for sensitive model content), n.d.. opentelemetry.ioThe OpenTelemetry registry defines generative-AI telemetry attributes and warns that model inputs and outputs are likely to contain sensitive or personally identifiable information.
- MLCommons, “MLPerf Inference Benchmark Rules” (System-under-test, quality, load-generation, and latency rules for inference measurement), n.d.. github.comMLPerf Inference specifies complete systems under test, minimum quality, open-loop server arrivals, and latency constraints, illustrating why throughput cannot be interpreted without workload and service conditions.
- SchedMD, “Slurm Workload Manager: Overview” (Resource allocation, queued jobs, and job-step execution), n.d.. slurm.schedmd.comThe Slurm overview separates cluster resource allocation, queued-job scheduling, and execution of job steps on allocated nodes.
- Kubernetes SIG Scheduling, “Kueue Overview” (Kubernetes-native workload admission, quota, and fair sharing), n.d.. kueue.sigs.k8s.ioKueue manages when quota-consuming workloads are admitted, wait, or are preempted while leaving pod placement, autoscaling, and job lifecycle to their respective Kubernetes components.
- Kubernetes, “Dynamic Resource Allocation” (Structured claims and device selection for specialized resources), n.d.. kubernetes.ioKubernetes Dynamic Resource Allocation lets workloads request devices through structured claims and lets drivers participate in device selection and preparation.
Comments
Log in to comment