# Ethics, Safety & Alignment in AI Engineering: Complete Phase 18 Curriculum

> Explore Phase 18 of AI Engineering from Scratch. Learn value alignment, reward modeling, interpretability, privacy, and governance to build safe AI systems with 15 hands-on lessons.

- Repository: [Rohit Ghumare/ai-engineering-from-scratch](https://github.com/rohitg00/ai-engineering-from-scratch)
- Tags: curriculum
- Published: 2026-08-26

---

**Phase 18 of the AI Engineering from Scratch repository delivers 15 hands-on lessons covering value alignment, reward modeling, interpretability, differential privacy, and governance frameworks to build production-ready safe AI systems.**

The `rohitg00/ai-engineering-from-scratch` curriculum treats **Ethics, Safety & Alignment** as a core engineering discipline rather than an afterthought. This phase transforms abstract ethical principles into runnable Python implementations, ranging from foundational safety theory to a capstone agent that demonstrates real-time safety enforcement at inference time.

## Phase 18 Curriculum Overview

Phase 18 is organized into 15 self-contained lessons, each combining theoretical documentation ([`docs/en.md`](https://github.com/rohitg00/ai-engineering-from-scratch/blob/main/docs/en.md)) with executable code artifacts. The curriculum progresses from foundational concepts to advanced safety engineering:

- **01 – Foundations of AI Safety**: Historical context and safety terminology
- **02 – Value Alignment Theory**: Objective-function problems and Corralling AI values
- **03 – Reward Modeling & Inverse RL**: Building reward models from human feedback
- **04 – Interpretability & Explainability**: Saliency maps and feature attribution
- **05 – Robustness to Distribution Shift**: OOD detection and adversarial robustness
- **06 – Bias, Fairness & Mitigation**: Demographic bias measurement and debiasing
- **07 – Privacy & Data-Governance**: Differential privacy and secure pipelines
- **08 – Ethical Decision-Making Frameworks**: Utilitarian, deontological, and virtue-ethics applications
- **09 – AI Governance & Policy**: ISO/IEEE standards and regulatory ecosystems
- **10 – Risk Assessment & Containment**: Hazard analysis and kill-switch design
- **11 – Human-Centric Evaluation**: RLHF loops and human-in-the-loop testing
- **12 – Certification & Auditing**: Audit trails and reproducible safety reports
- **13 – Future-Proofing & Long-Term Alignment**: Speculative safety research roadmaps
- **14 – Capstone: Safety-First Agent**: End-to-end agent with integrated safety checks
- **15 – Reflection & Checklist**: Personal safety checklist consolidation

Each lesson follows the **Build-It / Use-It** pattern: you first implement the concept from scratch, then run the same logic through higher-level libraries to compare production implementations.

## Reward Modeling from Human Feedback

Lesson 03 implements a basic reward model using linear regression to predict human preferences. Located in [`phases/18-ethics-safety-alignment/03-reward-modeling/code/train_reward.py`](https://github.com/rohitg00/ai-engineering-from-scratch/blob/main/phases/18-ethics-safety-alignment/03-reward-modeling/code/train_reward.py), this script demonstrates how to estimate reward functions from scalar human ratings.

```python

# phases/18-ethics-safety-alignment/03-reward-modeling/code/train_reward.py

import numpy as np
from sklearn.linear_model import LinearRegression

# Toy dataset: (state, action) → scalar human rating

X = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])
y = np.array([0.1, 0.8, 0.2, 0.9])          # higher = more desirable

model = LinearRegression().fit(X, y)

def reward(state, action):
    """Return a learned reward for a (state, action) pair."""
    return float(model.predict(np.array([[state, action]]))[0])

print("Reward for (1,0):", reward(1, 0))

```

The `reward()` function serves as a minimal inverse reinforcement learning (IRL) component, illustrating how alignment systems translate human feedback into optimizable objectives.

## Differential Privacy Implementation

Lesson 07 covers privacy-preserving computation through the Gaussian mechanism. The implementation in [`phases/18-ethics-safety-alignment/06-privacy/code/dp_mean.py`](https://github.com/rohitg00/ai-engineering-from-scratch/blob/main/phases/18-ethics-safety-alignment/06-privacy/code/dp_mean.py) adds calibrated noise to statistical queries to provide formal privacy guarantees.

```python

# phases/18-ethics-safety-alignment/06-privacy/code/dp_mean.py

import numpy as np

def dp_mean(data, epsilon=1.0, delta=1e-5, sensitivity=1.0):
    """Return a differentially‑private estimate of the mean."""
    n = len(data)
    true_mean = np.mean(data)
    # Gaussian mechanism

    sigma = np.sqrt(2 * np.log(1.25 / delta)) * sensitivity / epsilon
    noise = np.random.normal(0, sigma / np.sqrt(n))
    return true_mean + noise

print(dp_mean([1, 2, 3, 4, 5]))

```

The `dp_mean()` function implements the Gaussian mechanism for $(\epsilon, \delta)$-differential privacy, allowing you to release aggregate statistics while bounding the risk of individual data reconstruction.

## Capstone: Safety-First Agent Architecture

Lesson 14 culminates in a production-pattern agent that enforces safety constraints at inference time. Located at [`phases/18-ethics-safety-alignment/14-capstone/code/safe_agent.py`](https://github.com/rohitg00/ai-engineering-from-scratch/blob/main/phases/18-ethics-safety-alignment/14-capstone/code/safe_agent.py), this implementation demonstrates pre-check and post-check filtering.

```python

# phases/18-ethics-safety-alignment/14-capstone/code/safe_agent.py

from utils import llm, safety_filter   # utility modules shipped with the lesson

def run(query):
    # 1️⃣ Safety pre‑check

    if not safety_filter.is_safe(query):
        return "⚠️ Query rejected for safety reasons."

    # 2️⃣ Normal LLM call

    response = llm(query)

    # 3️⃣ Post‑check (optional)

    if not safety_filter.is_safe(response):
        response = "⚠️ Generated output filtered for safety."

    return response

print(run("Write a helpful guide for new AI researchers."))

```

The `run()` function implements a **safety-at-inference** pattern, where `safety_filter.is_safe()` acts as a governance hook that can be substituted with content moderation APIs, alignment classifiers, or rule-based guardrails.

## Build-It / Use-It Methodology

Every lesson in Phase 18 follows a consistent pedagogical structure designed to expose implementation details before abstracting them away. You first write the algorithmic core (the "Build-It" phase), then compare it against optimized library implementations (the "Use-It" phase).

This approach surfaces the same pitfalls that appear in large-scale systems while keeping the computational requirements minimal enough to run on a laptop. All generated artifacts—including prompt templates, skill definitions, and model wrappers—are versioned in `phases/18-ethics-safety-alignment/outputs/` for downstream reuse.

## Key Files and Repository Structure

The phase organizes content into lesson-specific directories with consistent naming conventions:

- **[`phases/18-ethics-safety-alignment/01-foundations/docs/en.md`](https://github.com/rohitg00/ai-engineering-from-scratch/blob/main/phases/18-ethics-safety-alignment/01-foundations/docs/en.md)**: Narrative documentation covering safety fundamentals and historical context
- **[`phases/18-ethics-safety-alignment/03-reward-modeling/code/train_reward.py`](https://github.com/rohitg00/ai-engineering-from-scratch/blob/main/phases/18-ethics-safety-alignment/03-reward-modeling/code/train_reward.py)**: Implementation of linear reward models from human feedback
- **[`phases/18-ethics-safety-alignment/06-privacy/code/dp_mean.py`](https://github.com/rohitg00/ai-engineering-from-scratch/blob/main/phases/18-ethics-safety-alignment/06-privacy/code/dp_mean.py)**: Differential privacy mean estimator using the Gaussian mechanism
- **[`phases/18-ethics-safety-alignment/14-capstone/code/safe_agent.py`](https://github.com/rohitg00/ai-engineering-from-scratch/blob/main/phases/18-ethics-safety-alignment/14-capstone/code/safe_agent.py)**: Production-pattern agent with integrated safety filtering
- **`phases/18-ethics-safety-alignment/outputs/`**: Directory containing reusable safety artifacts, audit templates, and skill definitions produced throughout the lessons

## Summary

Phase 18 of the AI Engineering from Scratch curriculum equips you with practical **Ethics, Safety & Alignment** implementations:

- **15 progressive lessons** covering alignment theory, interpretability, privacy, and governance
- **Runnable Python implementations** of reward modeling, differential privacy, and safety filters
- **Build-It / Use-It pattern** that compares scratch implementations with production libraries
- **Capstone safety-first agent** demonstrating real-time safety enforcement and governance hooks
- **Reusable artifacts** stored in `outputs/` for integration into production pipelines

## Frequently Asked Questions

### What is the Build-It / Use-It pattern in Phase 18?

The Build-It / Use-It pattern requires you to first implement a safety concept from scratch (such as the `reward()` function or `dp_mean()` estimator) before running the same logic through higher-level libraries. This methodology exposes implementation pitfalls and library optimizations while ensuring you understand the mathematical foundations before relying on abstraction layers.

### How does the capstone agent implement safety checks?

The capstone agent in [`phases/18-ethics-safety-alignment/14-capstone/code/safe_agent.py`](https://github.com/rohitg00/ai-engineering-from-scratch/blob/main/phases/18-ethics-safety-alignment/14-capstone/code/safe_agent.py) implements a three-stage pipeline: a **pre-check** that validates inputs against `safety_filter.is_safe()`, the standard LLM inference call, and a **post-check** that filters generated outputs. This pattern allows you to inject governance controls at both ingestion and generation points without modifying the underlying model weights.

### What differential privacy techniques are covered?

Lesson 07 implements the **Gaussian mechanism** for $(\epsilon, \delta)$-differential privacy through the `dp_mean()` function in [`phases/18-ethics-safety-alignment/06-privacy/code/dp_mean.py`](https://github.com/rohitg00/ai-engineering-from-scratch/blob/main/phases/18-ethics-safety-alignment/06-privacy/code/dp_mean.py). The code calibrates noise based on query sensitivity and privacy budget parameters, teaching you to release aggregate statistics while maintaining formal guarantees against membership inference attacks.

### How can I reuse the safety artifacts from Phase 18?

All artifacts generated during the lessons—including prompt templates, skill definitions, and audit reports—are stored in `phases/18-ethics-safety-alignment/outputs/`. These files are designed as modular components that can be imported into downstream projects, providing ready-to-deploy safety checklists, model wrappers, and governance documentation that align with ISO/IEEE standards discussed in Lesson 09.