Build a layered LLM safety gate
Build a three-checkpoint safety gate: a jailbreak taxonomy, a measured injection detector, refusal evals, output classifiers, a constitutional rules engine and a streaming filter, with an audit trace per request.
Build it step by step
- 1Jailbreak TaxonomyPhase 19: Capstone projects
- 2Prompt Injection DetectorPhase 19: Capstone projects
- 3Refusal EvaluationPhase 19: Capstone projects
- 4Content Classifier IntegrationPhase 19: Capstone projects
- 5Constitutional Rules EnginePhase 19: Capstone projects
- 6End-to-End Safety GatePhase 19: Capstone projects
What You'll Build
A safety gate for an LLM product you name (for example a customer support bot or a coding assistant). It runs at three checkpoints: an input-side injection detector before generation, a streaming filter during generation, and output classifiers plus a constitutional rules engine after generation. A deterministic aggregation table turns their signals into block, redact, warn or allow, and every request leaves a structured trace. Each layer is measured with precision, recall or refusal rates against a labelled attack taxonomy and a benign set, and you report the numbers honestly, including what gets through.
Requirements
- Taxonomy. The six categories (role-play, instruction-override, context-smuggling, multi-turn-ramp, encoding-trick, prefix-injection), at least 7 fixtures each with
id,category,subtype,prompt,target_behaviorandseverity1 to 5, at least half written by you for your product. A validator that hard-fails on empty prompts, missing categories, bad severities or duplicate ids, trigram-cosinematch, andtaxonomy.json. - Detector. A normalise pass (zero-width and bidi removal, base64, rot13, hex and leet decoding) followed by substring and regex rules stored as data. Per-category precision, recall and F1 over the taxonomy plus a benign set of at least 20 prompts that includes hard cases such as legitimate creative role-play, written to
detector_report.json. - Refusal evaluation. A labelled safe and unsafe prompt set, the three scripted mock policies (strict, over-cautious, leaky), a refusal-phrase classifier, under-refusal and over-refusal rates, accuracy, ECE from stated confidence and per-category under-refusal. The report must show over-cautious with the highest over-refusal and leaky with the highest under-refusal.
- Output classifiers. Toxicity with a negation window, PII (email, phone, SSN-shaped, card-shaped, IP) and instruction leakage by trigram overlap with your system prompt, each with its own redactor. A router maps max severity to block, redact, warn or log, and each verb fires on at least one fixture.
- Rules engine. A YAML or JSON constitution with atomic predicates,
all_of,any_of,not_,applies_when(reported asnot_applicablewhen it does not apply), severities, explanations, fixer operations and a structured diff between draft and revised output. Include at least two rules specific to your product. - Gate. Pre-gen detector, a during-gen filter that buffers two chunks of a streaming mock LLM and terminates early on forbidden continuations, post-gen classifiers and rules, and the lesson 87 aggregation table. A
RequestTraceper request with prompt, each checkpoint's verdict, final action, final output and latency. Run every taxonomy fixture plus at least 10 benign prompts, writegate_trace.json, print counts of blocks, redacts, warns, allows and early terminations with a per-category breakdown, and exit 0. - Honest results. Report attack block rate, benign false-block rate and the categories that leak through, with one trace example of a miss.
Constraints
- Python standard library plus numpy (PyYAML optional, with a JSON fallback). CPU only, no network; the mock LLM keeps the run deterministic.
- A real model is optional through any OpenAI-compatible endpoint (the KatalockAi API or a local model), behind the same interface and a
--mockflag for the graded run. - Fixtures describe attack techniques and must use placeholders such as
[restricted procedure]for the harmful payload. Never put real operational harmful content in the repo or in your outputs. - Rule-based classifiers are teaching tools, not production moderation. Say so in the README.
Stretch Goals
- Replace the deterministic aggregator with a weighted score and sweep the threshold to plot precision against recall on the taxonomy.
- Add a seventh category for indirect prompt injection through retrieved documents, with ten fixtures.
- Run the during-gen filter in a thread and show the added latency stays within a 50 ms budget.
How to Submit
Public repo with safety/ (taxonomy, detector, refusaleval, classifiers, rules, gate, mockllm), fixtures/, rules.yml, main.py, tests/ and results/ (taxonomy.json, detectorreport.json, refusalevalreport.json, classifierreport.json, rulesreport.json, gatetrace.json). The README must name the product you are protecting, give run commands, explain the trust boundary each category abuses, show your detector and refusal metrics, explain the aggregation table and any change you made to it, and give an honest account of what the gate misses and what you would add next.
How It's Reviewed
Professor Katalock reads your repository and the evidence you paste, and scores each criterion out of 100. You pass with a weighted total of 70 or more and no criterion below 40. A copied or padded submission is flagged and fails.
Submit
Push your work to a public GitHub repository with a README that explains how to run it, then paste the evidence below. Up to 3 reviews a day, free.
- Full output of `python main.py` with the summary table
- Per-category metrics from detector_report.json
- The three-policy comparison from the refusal evaluation
- One complete RequestTrace for a blocked request and one for a missed attack
- Output of `python -m pytest -q`
- A paragraph on which category leaks most and why
Reviews are done by AI against the rubric above. They check the evidence in your repository; they are not a substitute for a human code review, and a review can be wrong. If you think it is, improve the README so the evidence is unmistakable and resubmit.