Phase 7: Safety Gate · ~90 min · Python
End-to-End Safety Gate
Pre-gen, during-gen, post-gen. Three checkpoints, one verdict, an audit trail per request.
Introduction
- Type: Build
- Languages: Python
- Prerequisites: Earlier lessons in this course
- Time: ~90 min
The Problem
Lessons 82-86 in this track each shipped a single piece: a taxonomy, an input detector, an evaluation framework, an output classifier, a rules engine. A real safety gate has to compose them, run them at the right moment in the request lifecycle, decide what action to take when they disagree, and produce a trace a reviewer can read on Monday morning. The composition is the lesson.
The gate sits at three checkpoints. Pre-gen runs before the model is called: the detector from Prompt Injection Detector looks at the prompt and either passes it, blocks it outright (high-confidence attack), or attaches a flag for downstream layers to weigh. During-gen runs as the model emits tokens: a streaming filter buffers chunks and terminates the stream early if a forbidden phrase appears (prefix-injection survives this if the gate only looks post-hoc). Post-gen runs after the model finishes: the classifier router from Content Classifier Integration and the rules engine from Constitutional Rules Engine inspect the full output, the gate aggregates their verdicts with the pre-gen signal, and the gate applies a final action.
The gate is self-terminating: every fixture in the Jailbreak Taxonomy taxonomy is run end to end, the gate emits a per-request trace, and the demo exits zero whether the gate blocks every attack or not. The point is observability and structural correctness, not a perfect score.
Unlock the full lesson
You've read the first 2 sections. The rest of this lesson covers The Concept, Build, Check Yourself, Key Terms & Next — plus a hands-on lab, quiz, and project artifact.
Create a free account — the opening phases of 29 of 35 courses, and the first lesson of every phase, are free, no credit card.
Browse all courses · View pricing · DeVenture Academy