Module 1 · AI workloads on your platform · Chapter 02 · Free
The reference serving stack: from request to token
- A production request crosses six layers before the first token: gateway, router, queue, serving engine, GPU node, and back out.
- Each layer owns one job and one set of failures. If you cannot name the owner, you cannot fix the incident.
- Four side systems make the stack real: model registry, vector database, cache, and the observability pipeline.
The picture: the stack is a relay race. Each runner owns one leg, and the baton is the request, with its request id.
- A reference stack you can map onto any vendor or cloud.
- What each layer owns, and the symptom that points at it.
- A latency budget that splits one slow request across the layers.
- A sorting skill: given a symptom, name the layer that owns it.
Copperline is a made up B2B SaaS company. Ana, the support lead, reports that its support copilot took 8 seconds to answer a simple refund question. All figures here are illustrative.
- Priya Nair, the staff platform engineer, opens the dashboard. It shows one number: total time, 8 seconds.
- No trace links the gateway, the queue, and the GPU, so nobody can say where the time went.
- Tom, the SRE, checks the GPU. It looks busy. That tells him nothing about this one request.
The model may have been fine. With no layer timings, the team had no way to know, and no fix to make.
- The front door: client, gateway, and router.
- The waiting room and the engine room: queue, serving engine, GPU node.
- The side systems: registry, vector database, cache, observability.
- The picture: one request walked through the whole stack.
- A latency budget in numbers, then a symptom matching lab.
Next: the front door, where a request earns the right to cost you GPU time.
Step 1: The front door, client, gateway and router
The client sends a request. The API gateway is the first platform layer it meets, and it owns the rules of entry.
- Gateway owns: authentication, rate limits per tenant, request size checks, timeouts, and minting a request id that follows the request everywhere.
- Router owns: choosing which model and which pool serves the request, based on tenant, task, and cost or latency class.
- Why separate them: the gateway asks "may you enter, and how often". The router asks "where should you go". Different questions, different owners.
Think of a hotel: the front desk checks your booking, then directs you to the right floor. Where the picture breaks down:
- A hotel guest waits once. A request can be re-routed or rejected in milliseconds, many times a second.
- The desk does no real work on your stay. The gateway also does no inference, and it should stay that cheap.
Two habits make this layer pay off. First, set the gateway timeout above the honest total budget, or slow but valid answers die at the door. Second, log the request id at entry and pass it on unchanged. Without that one thread, every later layer keeps its own diary and nobody can join them up in an incident.
Next: the request is admitted. Now it waits, and waiting is a design choice.
Step 2: The waiting room and the engine room
The request queue sits between the router and the serving engine. It owns admission and order.
- Queue owns: who waits, who goes first (priorities), and backpressure: shedding or delaying load before the engine drowns.
- Serving engine owns: batching requests together, managing the KV cache, and streaming tokens back as they are generated. Engines in this category include vLLM, TensorRT-LLM and SGLang.
- GPU node owns: the physical run: drivers, GPU memory, and the node health the engine depends on.
Streaming changes what the user feels. The engine can send the first token early, while the rest still generate, so a long answer can feel fast to start and slow to finish. That is why your traces need two numbers, not one: time to first token, and total time. A stack that only records total time hides a queue problem behind a healthy looking stream, and a stack that only records first token hides a decode problem the same way.
Chapter 5, "Serving engines: what vLLM and its peers actually do", goes deep on the engine. Chapter 6, "Batching and streaming: the latency budget of a token", goes deep on the wait. For model basics, see the latency and cost chapter of the AI engineering course.
Next: the layers nobody demos, and the ones that bite you at 3 a.m.
Step 3: The side systems every serious stack needs
Four systems sit beside the request path. Remove any one and the stack still demos well, then fails in production.
- Model registry and artifact store: the versioned source of truth for weights and configs. The engine loads exactly the version the registry names.
- Vector database: stores embeddings of help documents and returns the passages the copilot cites. Retrieval basics are in the RAG chapter of the AI engineering course. Chapter 13 of this course runs it in production.
- Cache: stores repeated answers, embeddings, and retrieved passages, so common questions skip the expensive path.
- Observability pipeline: traces, logs, and metrics keyed by request id, with per layer timing. This is what Copperline lacked in the cold open.
Notice the split. The registry, vector database, and cache hold state the request depends on, so their failures change answers or versions. The observability pipeline holds no user facing state at all, yet it decides whether the other failures take minutes or days to find. Budget for it like a serving layer, not like paperwork, and test it by tracing one real request end to end before launch day, not during the first incident.
Predict first:
In the worked example below, queue wait and decode dominate. That is the common lesson of a first trace: the wait and the generation, not the plumbing, eat the budget. Your own stack may differ, which is the point of measuring.
Next: the picture, one request through every layer.
Step 4: The picture, one request through the stack
Press Next to walk the refund question. Notice that every hop adds time, and every layer can add its timing to the trace.
- The client sends the question. The gateway checks auth and rate limits, then stamps a request id on it.
- The router picks the copilot model and its GPU pool, based on tenant and task.
- The queue admits the request and holds it until the engine has room. This wait is real latency, and it is timed.
- The vector database returns the refund passages. They join the prompt before generation starts.
- The engine batches the request, manages the KV cache, and runs prefill and decode on the GPU node, streaming tokens back.
- The observability pipeline records each layer timing under the one request id, so a slow answer has an address.
Next: the same 8 seconds, split into a budget you can act on.
Step 5: Worked example, a latency budget for 8 seconds
Priya replays Ana's refund question with tracing on. All figures are illustrative, for one copilot request.
1. Gateway and network in: 0.3 seconds. Running total: 0.3.
2. Queue wait: 2.4 seconds behind a batch of document jobs. Running total: 0.3 + 2.4 = 2.7.
3. Retrieval: 0.8 seconds for the vector search and passage fetch. Running total: 2.7 + 0.8 = 3.5.
4. Prefill: 1.1 seconds to process the prompt and retrieved passages. Running total: 3.5 + 1.1 = 4.6.
5. Decode: 3.2 seconds to generate the answer tokens. Running total: 4.6 + 3.2 = 7.8.
6. Network back: 0.2 seconds. Total: 7.8 + 0.2 = 8.0 seconds.
The model work, prefill plus decode, is 1.1 + 3.2 = 4.3 seconds. The queue alone is 2.4. Priya's first fix is not a faster model: it is a separate priority lane so support requests do not queue behind batch document jobs.
Next: the lab, where symptoms arrive and you name the owner.
Step 6: Hands-on lab, match the symptom to the layer
Why this exercise: incidents arrive as symptoms, not layer names. Naming the owner first cuts the search from six layers to one.
Do this:
Lab: Priya replays Ana's slow refund question with tracing on, and the chapter's budget appears: gateway and network in 0.3 seconds, queue wait 2.4, retrieval 0.8, prefill 1.1, decode 3.2 for a 400 token answer, network back 0.2. This lab runs in your browser and needs JavaScript enabled.
- Each symptom has one first owner, even when a second layer later joins the incident.
- The request id is what turns this matching game into a two minute lookup in production.
Next: a field note, then the mix-ups.
Production teams commonly trace AI requests with the same request id pattern used for ordinary services, extended with token counts and per layer timing. The habit worth copying is boring on purpose: one id, every layer, no gaps. Claims about how much time any one layer takes vary widely by workload, so measure your own stack rather than borrowing a number.
Mix-ups to avoid
- "The model was slow." Total time is not model time. Split the budget before blaming the engine or the GPU.
- "The gateway and the router are one thing." Entry rules and model choice fail differently and are fixed by different people.
- "A queue hides overload." A queue moves overload into wait time. Without backpressure and priorities, it just delays the failure.
- "Side systems are optional extras." Registry, retrieval, cache, and tracing are what make the demo stack operable.
Chapter quiz
Five questions. Score 70% or more to mark this chapter complete.
Review this chapter in 5 minutes
Watch the whole chapter recapped: six layers and their owners, four side systems, one request id, and the eight second latency budget.
- Six layers on the path: gateway (auth, limits, request id), router (model choice), queue (admission, priorities, backpressure), engine (batching, KV cache, streaming), GPU node (drivers, memory), and back out.
- Four side systems: model registry and artifact store, vector database, cache, observability pipeline.
- Budget example, illustrative: queue wait 2.4 s and decode 3.2 s dominate an 8.0 s total. Model work is 4.3 s of it.
- First fix there: a priority lane for interactive traffic, not a faster model.
- Every symptom has a first owner. Name it before you change anything.
Decision rule: for any slow or wrong answer, split the request by layer under one request id, then fix the owning layer first.
Review
Glossary
- API gateway
- The entry layer: auth, rate limits, timeouts, request ids. Example: rejecting an unauthenticated call.
- Router
- The layer that picks model and pool per request. Example: premium tenants to the large model.
- Request queue
- Where admitted requests wait, in priority order. Example: support ahead of batch jobs.
- Backpressure
- Delaying or shedding load before a layer drowns. Example: pausing batch intake at noon.
- Serving engine
- Software that batches requests, manages KV cache, and streams tokens. Example: engines in the vLLM category.
- KV cache
- Stored attention state that avoids recomputing the prompt per token. Sized in the next chapter.
- Model registry
- The versioned source of truth for model weights and configs. Example: version 14 named as current.
- Vector database
- A store of document embeddings searched at request time. Example: returning the refund passages.
- Request id
- One identifier stamped at the gateway and logged by every layer. Example: req-8f31 in every trace.
- Latency budget
- A total time split into per layer allowances. Example: 8.0 s split six ways in this chapter.
Next: "Reading a model like an operator: weights, KV cache and context".