Skip to content

1.2 Full-stack AI engineering

A model is a small part of an AI system. Around it sit the pipelines that collect and validate data, the feature store that serves inputs consistently to training and inference, the training and evaluation machinery, the servers that batch requests onto GPUs, the application and API that users touch, and the monitoring that tells you when the world has drifted away from what the model learned. A full-stack AI engineer can work at every one of those layers, and interviews for AI roles increasingly probe all of them in a single design conversation.

Concepts from other sections (18)

This section uses these ideas. Each has a short definition and a link to the section that explains it in full. Tap a dotted term in the text to preview it.

This section walks the stack from bottom to top, in the order data flows through it:

[Data Collection] --> [Data Processing] --> [Feature Engineering]
|
[Model Training]
|
[Evaluation & Validation]
|
[Model Serving]
|
[Monitoring & Feedback]
|
[User-Facing Application (Web/Mobile/API)]

For each layer it gives the patterns and trade-offs interviewers expect, with code for the pieces you may be asked to build: a data validator, an online feature store, an evaluator, a dynamically batching model server, and a drift detector. It assumes the general background in Distributed systems fundamentals; the training side is covered in more depth in ML infrastructure patterns.

Learning objectives

  • Choose a data pipeline pattern and validation checks for a given ML workload.
  • Explain online and offline feature stores and why point-in-time correctness matters for training.
  • Pick distributed training and hyperparameter search strategies, and offline and online evaluation methods.
  • Implement dynamic batching for a model server and explain its latency-throughput trade-off.
  • Design an ML API and a monitoring plan, including drift detection.

Everything starts with data, and the first decision is how it moves. The common pipeline patterns differ in latency:

PatternUse CaseTools
Batch ETLDaily model retrainingSpark, Airflow, dbt
Stream processingReal-time featuresKafka, Flink, Kinesis
Change Data CaptureDatabase -> feature store syncDebezium, DynamoDB Streams
Lambda architectureCombined batch + real-timeBatch layer + speed layer

Whatever the pipeline, bad data silently produces bad models, so validate it before training. A small framework is enough: register named checks, each a function that returns whether it passed and a human-readable detail, with a severity so that warnings can be distinguished from blocking errors. The check constructors below are closures over a column name, which keeps each check reusable.

class DataValidator:
"""Schema and quality validation for ML datasets."""
def __init__(self):
self.checks = []
def add_check(self, name: str, fn: callable, severity: str = "error"):
self.checks.append((name, fn, severity))
def validate(self, df) -> list[dict]:
results = []
for name, fn, severity in self.checks:
passed, details = fn(df)
results.append({
"check": name,
"passed": passed,
"severity": severity,
"details": details
})
return results
# Common checks
def no_nulls(column):
def check(df):
null_count = df[column].isnull().sum()
return null_count == 0, f"{null_count} nulls in {column}"
return check
def value_range(column, min_val, max_val):
def check(df):
out_of_range = ((df[column] < min_val) | (df[column] > max_val)).sum()
return out_of_range == 0, f"{out_of_range} values out of [{min_val}, {max_val}]"
return check
def no_duplicates(columns):
def check(df):
dup_count = df.duplicated(subset=columns).sum()
return dup_count == 0, f"{dup_count} duplicate rows on {columns}"
return check

Data also needs versioning, because a training run is only reproducible if its data version is pinned. DVC (Data Version Control) provides Git-like versioning for datasets, and table formats such as Delta Lake and Apache Iceberg provide time travel and versioning at the table level.

Raw data becomes model inputs through features, and the central piece of infrastructure is the feature store. A feature pipeline writes each feature to two places: an online store (Redis or DynamoDB) that serves the latest values with low latency for real-time inference, and an offline store (S3 or BigQuery) that keeps history for building training sets.

[Raw Data] --> [Feature Pipeline] --> [Feature Store]
|
+-------------+-------------+
| |
[Online Store] [Offline Store]
(Redis/DynamoDB) (S3/BigQuery)
| |
[Real-time serving] [Training data]

Computing both from one pipeline is what prevents training-serving skew, where a feature is computed one way for training and another way in production. Features differ in how fresh they must be:

TypeExampleLatencyUpdate Frequency
StaticUser age, locationN/ARarely
Batch30-day purchase countHoursDaily
Near-real-timeLast 5 items viewedSecondsOn event
Real-timeCurrent session durationMillisecondsContinuous

A simplified online feature store fits in a page. Each entity maps feature names to a value with a timestamp and version. get_features serves inference, returning None for missing features. get_training_data builds training rows and takes an as_of time, so that a training example never sees a feature value recorded after the moment it describes.

from dataclasses import dataclass
from typing import Any
import time
@dataclass
class Feature:
name: str
value: Any
timestamp: float
version: int
class FeatureStore:
"""Simplified online feature store."""
def __init__(self):
self.store: dict[str, dict[str, Feature]] = {} # entity_id -> {feature_name: Feature}
def set_feature(self, entity_id: str, name: str, value: Any, version: int = 1):
if entity_id not in self.store:
self.store[entity_id] = {}
self.store[entity_id][name] = Feature(name, value, time.time(), version)
def get_features(self, entity_id: str, feature_names: list[str]) -> dict[str, Any]:
"""Get multiple features for an entity. Returns dict of name -> value."""
entity = self.store.get(entity_id, {})
return {
name: entity[name].value if name in entity else None
for name in feature_names
}
def get_training_data(self, entity_ids: list[str], feature_names: list[str],
as_of: float = None) -> list[dict]:
"""Point-in-time correct feature retrieval for training."""
rows = []
for eid in entity_ids:
entity = self.store.get(eid, {})
row = {"entity_id": eid}
for name in feature_names:
if name in entity:
feat = entity[name]
if as_of is None or feat.timestamp <= as_of:
row[name] = feat.value
else:
row[name] = None
else:
row[name] = None
rows.append(row)
return rows

Warning: Because this store keeps only the latest value of each feature, get_training_data can only avoid leakage, not achieve full point-in-time correctness: if a feature was updated after as_of, it returns None instead of the value that was current at as_of. A real offline store keeps the full history and does an “as of” join, picking for each example the latest value with a timestamp at or before the example’s time. Being able to explain that difference is the point of the question.

With features in hand, training follows a pipeline from data loading and preprocessing through training, hyperparameter search, and evaluation, ending in a model registry that versions the result:

[Data Loader] --> [Preprocessing] --> [Model Training]
|
[Hyperparameter Search]
|
[Evaluation]
|
[Model Registry]

Every training run should be tracked, recording its hyperparameters, dataset version, metrics (loss, accuracy, and custom metrics), model artifacts, code version (the git hash), and environment (library versions). MLflow, Weights & Biases, Neptune, and Comet are the common tools.

When a model or dataset outgrows one GPU, training is distributed in one of four ways:

PatternWhat’s DistributedUse When
Data ParallelTraining data across GPUsModel fits on one GPU
Model ParallelModel layers across GPUsModel too large for one GPU
Pipeline ParallelModel stages as pipelineVery deep models
ZeROOptimizer state, gradients, parametersMemory-efficient data parallel

Hyperparameter search has its own trade-offs. Random search is often better than grid search when there are many parameters, because it does not waste trials on unimportant dimensions:

StrategyDescriptionWhen to Use
Grid SearchExhaustive search over combinationsFew parameters, small search space
Random SearchRandom samplingMany parameters (often better than grid)
Bayesian OptimizationModel the objective, sample promising pointsExpensive evaluations
Population-Based TrainingEvolutionary approach across workersVery large scale

Before a model ships it is evaluated offline, on held-out data. The evaluator below computes weighted precision, recall, and F1 for classification (weighted by class support, so imbalanced classes are accounted for), plus a one-vs-rest AUC when probabilities are available, and MSE, MAE, and R2R^2 for regression. R2=1−∑(y−y^)2/∑(y−yˉ)2R^2 = 1 - \sum (y - \hat y)^2 / \sum (y - \bar y)^2 measures how much better the model does than always predicting the mean.

from sklearn.metrics import precision_score, recall_score, f1_score
import numpy as np
class ModelEvaluator:
def __init__(self):
self.metrics = {}
def evaluate_classification(self, y_true, y_pred, y_prob=None):
self.metrics = {
"precision": precision_score(y_true, y_pred, average="weighted"),
"recall": recall_score(y_true, y_pred, average="weighted"),
"f1": f1_score(y_true, y_pred, average="weighted"),
}
if y_prob is not None:
self.metrics["auc_roc"] = self._auc(y_true, y_prob)
return self.metrics
def evaluate_regression(self, y_true, y_pred):
self.metrics = {
"mse": np.mean((y_true - y_pred) ** 2),
"mae": np.mean(np.abs(y_true - y_pred)),
"r2": 1 - np.sum((y_true - y_pred) ** 2) / np.sum((y_true - np.mean(y_true)) ** 2),
}
return self.metrics
def _auc(self, y_true, y_prob):
# Simplified AUC calculation
from sklearn.metrics import roc_auc_score
return roc_auc_score(y_true, y_prob, multi_class="ovr", average="weighted")

Offline metrics only predict online behaviour, so the model is then evaluated on real traffic, carefully. In shadow mode the new model runs alongside production and its outputs are compared without being served. A canary deployment serves 1-5% of traffic with the new model. Interleaving mixes results from the old and new models in the same response, which is a sensitive test for ranking. And throughout, guardrail metrics for safety and quality are monitored and must not degrade.

Serving patterns are defined by their latency budget:

PatternLatencyThroughputUse Case
Real-time (online)< 100msPer-requestAPI calls, search ranking
Near-real-time< 1sMicro-batchContent moderation
BatchMinutes-hoursVery highRecommendations, reports
StreamingContinuousHighAnomaly detection, monitoring

For real-time serving on GPUs, the key technique is dynamic batching. A GPU processes a batch of 32 inputs in little more time than one, so a server that groups concurrent requests gets far more throughput. The listing below shows how. Each call to predict enqueues its input with a future and awaits the future. A background loop waits for at least one request, then keeps collecting until it has max_batch_size requests or max_wait_ms has elapsed since the first, runs one batched inference, and resolves each caller’s future with its own result, or with the exception if the batch failed.

from abc import ABC, abstractmethod
from typing import Any
import asyncio
class ModelServer(ABC):
"""Abstract model serving interface."""
@abstractmethod
async def predict(self, input_data: Any) -> Any:
pass
@abstractmethod
async def health(self) -> dict:
pass
class BatchingModelServer(ModelServer):
"""Server with dynamic batching for GPU efficiency."""
def __init__(self, model, max_batch_size: int = 32, max_wait_ms: float = 10.0):
self.model = model
self.max_batch_size = max_batch_size
self.max_wait_ms = max_wait_ms
self.queue: asyncio.Queue = asyncio.Queue()
self._running = False
async def predict(self, input_data: Any) -> Any:
future = asyncio.get_event_loop().create_future()
await self.queue.put((input_data, future))
return await future
async def _batch_loop(self):
while self._running:
batch_inputs = []
batch_futures = []
# Wait for at least one item
input_data, future = await self.queue.get()
batch_inputs.append(input_data)
batch_futures.append(future)
# Collect more items up to batch size or timeout
deadline = asyncio.get_event_loop().time() + self.max_wait_ms / 1000
while len(batch_inputs) < self.max_batch_size:
remaining = deadline - asyncio.get_event_loop().time()
if remaining <= 0:
break
try:
input_data, future = await asyncio.wait_for(
self.queue.get(), timeout=remaining
)
batch_inputs.append(input_data)
batch_futures.append(future)
except asyncio.TimeoutError:
break
# Run batch inference
try:
results = self.model.predict_batch(batch_inputs)
for future, result in zip(batch_futures, results):
future.set_result(result)
except Exception as e:
for future in batch_futures:
future.set_exception(e)
async def start(self):
self._running = True
asyncio.create_task(self._batch_loop())
async def health(self) -> dict:
return {"status": "healthy", "queue_size": self.queue.qsize()}

max_wait_ms is the knob that trades latency for throughput: under light load a request waits at most 10 ms for company, and under heavy load batches fill instantly and no one waits. Two production details to mention: model.predict_batch is called synchronously, so it blocks the event loop while the GPU works (run it in an executor), and inside coroutines asyncio.get_running_loop() is the modern replacement for get_event_loop(). LLM servers go a step further with continuous batching, adding and removing sequences at every decoding step.

Batching is one of several ways to make serving cheaper. Roughly what each buys, and costs:

TechniqueSpeed ImprovementQuality Impact
Quantization (FP16 -> INT8)2-4xMinor (<1% accuracy loss)
Knowledge Distillation5-10x (smaller model)Moderate (1-5% loss)
Pruning2-5xMinor-moderate
ONNX Runtime1.5-3xNone
TensorRT2-5xNone-minor
Batching3-10x throughputNone

At the top of the stack, AI products have developed their own UI patterns:

PatternDescriptionExample
Streaming responseShow results as they generateChatGPT, Claude
Progressive disclosureShow confidence levelsSearch suggestions
Human-in-the-loopRequire approval for actionsGitHub Copilot suggestions
Feedback collectionThumbs up/down on outputsTraining data for improvement
ExplanationShow reasoning or sourcesRAG with citations

The API behind them should follow five principles. Make it asynchronous by default, because inference can be slow. Include request IDs for tracing. Return confidence scores, not just labels. Support streaming for long-running inference. And version your models in the API, so that clients choose when to move. A request and response for a text classifier look like this:

# Good ML API design principles
# 1. Async by default (inference can be slow)
# 2. Include request IDs for tracing
# 3. Return confidence scores
# 4. Support streaming for long-running inference
# 5. Version your models in the API
# POST /v1/predict
{
"model": "text-classifier-v2",
"input": {"text": "This movie was great!"},
"parameters": {
"temperature": 0.0,
"top_k": 5
}
}
# Response
{
"request_id": "req_abc123",
"model": "text-classifier-v2",
"output": {
"label": "positive",
"confidence": 0.94,
"alternatives": [
{"label": "neutral", "confidence": 0.04},
{"label": "negative", "confidence": 0.02}
]
},
"usage": {
"input_tokens": 12,
"processing_time_ms": 45
}
}

The loop closes with monitoring. An ML system needs the usual service metrics plus two kinds that ordinary services lack, model quality and data quality:

CategoryMetricsAlerting Threshold
Latencyp50, p95, p99 inference timep99 > 2x baseline
ThroughputRequests/sec, tokens/secDrops below expected
ErrorsError rate by type> 0.1%
Model qualityPrediction distribution shiftKL divergence > threshold
Data qualityFeature null rates, value distributionsOut of expected range
ResourcesGPU utilization, memory, CPU> 90% sustained

Model quality is the hard one, because labels usually arrive late or never. A practical proxy is drift in the distribution of predictions. The detector below records a reference histogram from validation predictions, keeps a sliding window of recent predictions, and once the window is half full compares the two with the KL divergence DKL(Pref ∥ Pcur)=∑ipilog⁡(pi/qi)D_{KL}(P_\text{ref} \,\|\, P_\text{cur}) = \sum_i p_i \log (p_i / q_i), flagging drift above a threshold. The small constant added to each bin avoids dividing by or taking the log of zero.

import numpy as np
from collections import deque
class DriftDetector:
"""Detect distribution shift in model predictions."""
def __init__(self, window_size: int = 1000, threshold: float = 0.1):
self.reference_distribution = None
self.window = deque(maxlen=window_size)
self.threshold = threshold
def set_reference(self, predictions: list[float]):
"""Set the reference distribution from validation data."""
self.reference_distribution = np.histogram(predictions, bins=50, density=True)
def observe(self, prediction: float) -> bool:
"""Observe a new prediction. Returns True if drift detected."""
self.window.append(prediction)
if len(self.window) < self.window.maxlen // 2:
return False # Not enough data
current_hist = np.histogram(list(self.window),
bins=self.reference_distribution[1],
density=True)
# KL divergence
ref = self.reference_distribution[0] + 1e-10
cur = current_hist[0] + 1e-10
kl_div = np.sum(ref * np.log(ref / cur))
return kl_div > self.threshold

Two refinements are worth raising. With density=True the histogram values are densities, not probabilities, so the sum is scaled by the inverse bin width; normalising the counts to sum to one makes the threshold independent of the bins. And because the current histogram reuses the reference bin edges, predictions outside the reference range are silently dropped, which is exactly the kind of drift you most want to catch. Recomputing the histogram on every observation is also costly; checking every few hundred observations is usually enough. The ML infrastructure patterns section covers PSI and the Kolmogorov-Smirnov test as alternatives.

Latency estimates for an ML system draw on a few component figures:

ComponentLatencyNotes
Feature store lookup (online)1-10msRedis/DynamoDB
Embedding search (ANN)5-50msDepends on index size
Classification model inference5-50msDepends on model size
LLM inference (first token)100-500msDepends on input length
LLM inference (per token)10-30msAfter first token
Image model inference50-200msResNet/ViT class
Batch training jobMinutes-daysDepends on data/model size

This material comes up across the companies in this book, each with its own emphasis. At Google it is recommendation system design and ML serving platforms; at OpenAI and Anthropic, LLM inference and training infrastructure; at Netflix, recommendation ML and A/B testing platforms; at Amazon, SageMaker-like platform design and product ranking; at Apple, on-device and privacy-preserving ML; at NVIDIA, GPU-optimised training and inference; at Palantir, enterprise ML platforms and data pipelines; and at Jane Street, signal generation and online learning. The next section, ML infrastructure patterns, goes deeper into training infrastructure, experiment tracking, and drift detection.

  • Chip Huyen, Designing Machine Learning Systems, for the end-to-end view this section summarises.
  • D. Sculley et al., “Hidden Technical Debt in Machine Learning Systems” (NeurIPS 2015), on why the model is the smallest part of the system.
  • Feature store: a system that computes features once and serves them both online, for inference, and offline, for training.
  • Training-serving skew: a mismatch between how features are computed for training and for production.
  • Point-in-time correctness: building each training example only from feature values that existed at the example’s timestamp.
  • Dynamic batching: grouping concurrent inference requests into one batch, bounded by a size and a maximum wait.
  • Shadow mode: running a candidate model on live traffic without serving its outputs.
  • Drift: a change in the distribution of inputs or predictions relative to what the model was trained on.
  • An AI system is a pipeline from data to monitoring; the model is one stage of it.
  • Validate and version data, and serve features from one pipeline to avoid training-serving skew and leakage.
  • Evaluate offline with metrics suited to the task, then online with shadow mode, canaries, interleaving, and guardrails.
  • Dynamic batching trades a bounded wait for large GPU throughput gains; quantisation, distillation, and compilers cut cost further.
  • Monitor model and data quality as well as service health, using prediction drift as a proxy when labels are late.
  1. Why does a feature store have both an online and an offline store, rather than one?

    Answer

    Inference needs the latest value of a few features for one entity within milliseconds, which suits a key-value store such as Redis. Training needs historical values of many features for millions of entities, which suits a warehouse or object store. Writing both from the same pipeline keeps the two consistent and prevents training-serving skew.

  2. What is label leakage through features, and how does an as_of timestamp prevent it?

    Answer

    Leakage happens when a training example includes a feature value recorded after the event it is meant to predict, so the model learns from information it will not have at inference time. Restricting each example to values with timestamps at or before its as_of time prevents it; a full implementation picks the latest such value from the feature’s history.

  3. In the batching server, what happens to latency and throughput as max_wait_ms increases?

    Answer

    Under light load, larger waits let more requests join each batch, raising throughput but adding up to max_wait_ms of latency to each request. Under heavy load batches fill to max_batch_size before the deadline, so the wait rarely matters. The setting is a cap on the latency you are willing to pay for batching.

  4. Why monitor the distribution of predictions rather than accuracy directly?

    Answer

    Accuracy needs ground-truth labels, which often arrive days later or never. The distribution of predictions is available immediately, and a significant shift in it is an early signal that inputs have drifted or the model is behaving differently.