Unit 1 of 12 · about 4 hours · explain, implement

Typed OpenAI-compatible schemas, token streaming and backpressure

The API schema and its stream are the product the learner operates. Every SLO, bill and debug session starts at the request fields and the event stream, not inside the model.

You'll be able to

  • Send a typed chat-completions request to a local engine and parse its SSE stream down to the [DONE] terminator.
  • Explain where backpressure sits when a client reads slower than decode produces, using per-request queues and stream_interval.
  • Constrain one endpoint with a JSON-schema structured output and state when a schema change defeats the grammar cache.

Covers

Typed OpenAI-compatible request and response schemasToken streaming and backpressureStructured-output API and grammar-cache lifecycle
1

Read

In this order, only the parts listed

0 of 4 done

  1. vLLM OpenAI-Compatible ServervLLM Online Serving, Architecture and Production Metrics

    Supported APIs through Extra HTTP Headers: endpoint table, vllm serve with --api-key token-abc123, extra_body with top_k, --enable-request-id-headers with x-request-id, and X-Vllm-Priority

  2. Streaming with batching from scratchHands-On LLM Serving and Optimization - Chapters 3 and 4, serving system design

    Chapter 3, Streaming with Batching (printed pp. 77-82): Table 3-1 batch evolution T0-T3, /generate_stream with media_type="text/event-stream", per-prompt asyncio.Queue and the None finish signal

  3. vLLM structured outputsvLLM Online Serving, Architecture and Production Metrics

    Online Serving section: choice, regex, json and grammar examples through extra_body, the --structured-outputs-config.backend flag, and the deprecated guided_json mapping box

  4. OpenAI chat streaming contractOpenAI API Reference - streaming, auth, rate limits and request IDs

    Create chat completion: the stream and stream_options.include_usage parameters (the usage chunk arrives before data: [DONE]) and the streaming example ending in data: [DONE]; the linked Streaming events page for the chat.completion.chunk fields delta and finish_reason (page read 2026-10-14)

    Read only the streaming contract. vLLM on your GPU is the implementation under test, not the hosted service.

2

Focus on

  • The typed contract is model, messages, sampling fields such as temperature, top_k and seed, plus vLLM extras (structured_outputs, request_id, session_id, cache_salt, priority) passed through extra_body. suffix is unsupported and user is ignored.
  • Each SSE chunk is one data line carrying a JSON delta object; the stream ends with a bare [DONE] data line, not with connection close. With output tokens and stream_interval , expect an initial role-only chunk, then about token-bearing chunks plus [DONE], since the first and last tokens are always sent immediately.
  • Backpressure lives in per-request queues between the decode loop and the HTTP connection. A slow reader grows queueing and connection time; it does not pause generation or reduce billed tokens.
  • A structured-output grammar is compiled per distinct schema and reused on repeat. Rotating schemas per request turns every call into a cold compile; keep a small fixed schema set.
Common mistakes · 3
  • temperature=0 does not silence reasoning models. A local Qwen3-0.6B with temperature=0 and seed=1 still opens with <think>; temperature controls sampling, not the thinking trace.
  • The server takes default sampling values from the model's generation_config.json in its Hugging Face repository. Fields your request sets still win; omitted ones inherit the model creator's values. Pass --generation-config vllm to fall back to vLLM's own defaults.
  • guided_json, guided_regex, guided_choice and guided_grammar were removed in v0.12.0. In v0.31.0 they do not fail: the server ignores them, returns HTTP 200 with unconstrained output and logs a one-time warning. Use structured_outputs with json, regex, choice or grammar.
3

Try it yourself

Closed-book, before checking anything

Against a local vLLM server, send one chat request with stream=True and max_tokens=8. Save the raw bytes, list every data line, point at the [DONE] terminator, and reassemble the assistant text from the delta fields only.
4

Quiz yourself

Answer first, then mark it. Missed ones go to Review.

0 got · 0 missed

  1. 1.Which endpoints does vLLM's --api-key protect?Answer

    Only paths under /v1, /v2 and /inference as documented (the v0.31.0 source also guards /cohere). /health, /metrics and /invocations stay unauthenticated, so put the server behind a reverse proxy that enforces auth.

  2. 2.How does a client know a stream is finished?Answer

    It receives a bare data: [DONE] line. Connection close alone is ambiguous: it can also mean a dropped connection mid-generation.

  3. 3.With 8 output tokens and stream_interval=3, about how many data chunks arrive?Answer

    About token-bearing chunks (tokens 1, 2 to 4, 5 to 7, then 8) after the role-only chunk, then [DONE]. The first token and the final chunk go out immediately; the middle ones wait for three new tokens.

  4. 4.Why does minting a fresh JSON schema per request hurt structured-output latency?Answer

    The grammar backend compiles each distinct schema, so a fresh schema misses the compiled-grammar cache on every call. A small fixed schema set reuses compiled grammars.

5

Practice

  • llm-model-inference ch03: streaming API tests

    test_generate_stream and test_generate_stream_concurrent against /generate_stream; test_stream.sh for the live-server variant

    Run with a tiny CPU model. Assert close-terminated JSON data lines and concurrent-stream isolation on /generate_stream, which sends no [DONE] line. Then assert the [DONE] terminator on a local vLLM stream of /v1/chat/completions.

You're done when

Your saved stream parses to [DONE], the deltas reassemble to the full text, and you can name which sampling knobs came from the model repository and which from your request.

Only when you can do this without notes.

Leads to

Notes

Resources · 3 for this unit, 10 more in the section

Also in this section

  • vLLM Production MetricsvLLM Project · required

    Define every selected queue, cache, phase and request metric, check histogram buckets and measure exporter overhead. Add model version, cancellation, rejection, quality sample and tenant-safe trace fields in the surrounding service.

  • Machine Learning Systems, Volume 2 - Inference at Scale and Serving Fault ToleranceHarvard (Vijay Janapa Reddi) · required

    Read "Load Balancing and Request Routing", "Multi-Tenancy and Isolation" and "Autoscaling and Global Infrastructure" in Inference at Scale, then "Serving Fault Tolerance" and "Graceful Degradation" in the Fault Tolerance chapter.

  • Inference Engineering - Chapter 7, ProductionPhilip Kiely (Baseten Books) · support

    Read Chapter 7 for containers, autoscaling, cold starts, routing and queueing, zero-downtime deployment, cost estimation and client-side streaming. Skip the vendor section at its end.

  • Gateway API Inference ExtensionKubernetes SIG Network · required

    Study model-aware routing, endpoint selection and operational integration. Use it for architecture and an optional local deployment only after the service passes lifecycle and failure tests without Kubernetes.

  • SGLang documentation - Production MetricsSGLang Project · support

    Compare its metric names and phase boundaries with vLLM's, so your dashboards do not depend on one engine's vocabulary.

  • AI Systems Performance Engineering - Production Inference LabsChris Fregly · required

    Use Chapter 16 monitoring, cache health, capacity planning and load-test utilities, then Chapter 17 routing only if distributed. Add process death, overload and bad-version cases that the performance pairs do not cover.

  • 100 Days of LLM Inference - Days 22 to 26Elizabeth Thomas · support

    Use the container, autoscaling, routing, capacity and zero-downtime notebooks as small operational simulations. Replace calendar order and fixed scale targets with the stage checkpoint and your hardware limits.

  • AI Infrastructure Engineer Learning - Project 103 LLM DeploymentAI Infra Curriculum · support

    Use its Docker, API, monitoring, Kubernetes and cost files as scaffolding only. Check its resource assumptions against your own model and GPU, and rerun any benchmark before reusing a result.

  • LLM Inference Systems Laboratory - Operations and FailureRoadmap-Final · support

    Complete production gateway, isolation, telemetry, canary, drain, rollback and fault campaigns. Store first-cause timelines and revise runbooks from observed blind spots.

  • vLLM Multimodal Inputs - media validation, fetch policy and cache identityvLLM Project · reference

    Read Online Serving for image, video and audio inputs: --allowed-media-domains, fetch timeouts, --limit-mm-per-prompt, local-media path restriction, and media UUIDs for cache identity. The link is pinned to v0.31.0.