Safety & Alignment
Hands-on lab with starter code you can copy + run, and guided steps to produce the artifact for this module.
Setup
- Open a fresh terminal in a new folder, e.g.
mkdir lab-7 && cd lab-7. - Create a Python 3.11 venv:
python -m venv .venv && source .venv/bin/activate. - Install the dependencies noted on each starter snippet (the small pip install … line above the code).
- Copy each starter into a file with the suggested name, then run it and capture the output.
Starter code
BLOCKED = ["bomb", "ssn", "credit card"]
def input_guard(text):
low = text.lower()
if any(b in low for b in BLOCKED):
raise ValueError("blocked input")
return text
def output_guard(text, llm):
judge = llm.complete(
f"Does the following violate safety policy? Answer yes/no.\n{text}")
if judge.strip().lower().startswith("yes"):
return "I can't help with that."
return textpython 07_01_lab.pyALLOWED = {"search_web", "calc"}
def execute(action_name, args):
if action_name not in ALLOWED:
raise PermissionError(f"tool {action_name} not allowed")
return TOOLS[action_name](**args)python 07_02_lab.pyGuided steps
Work through each step in order. Stop when your output matches the expected artifact — then push your work to your lab folder.
- 1Guardrail pair
Do — Implement input and output guards (denylist + LLM judge). Test on 20 prompts including 5 unsafe.
Expected artifact — All 5 unsafe blocked; ≤1 false positive on the safe 15.
Run free: - 2Tool allow-list
Do — Wrap tool execution behind an allow-list. Attempt to call a forbidden tool and confirm it raises.
Expected artifact — PermissionError raised; safe tools execute normally.
Run free: - 3Red-team report
Do — Spend 30 minutes trying to jailbreak your own agent. Log every successful exploit.
Expected artifact — redteam.md with ≥3 exploits and a planned mitigation per exploit.
Run free:
Authoritative sources
Independent primary sources used to check and corroborate the guidance on this page.
- NIST National Initiative for Cybersecurity Education (NICE)
US national framework for cybersecurity work roles and competencies.
- Financial Action Task Force (FATF)
Global AML/CFT standards referenced by financial-crime curricula.
- FBI Internet Crime Complaint Center — Annual Reports
Primary loss and typology data used in fraud-investigation training.
- US Federal Trade Commission — Consumer Sentinel Data Book
Official complaint statistics for teaching fraud trend analysis.
