Skip to main content
GACS will never ask for your seed phrase, private keys, or payment. free and ad-free.
Back to lessonLab · Module 7 / 12
Lab

Safety & Alignment

Hands-on lab with starter code you can copy + run, and guided steps to produce the artifact for this module.

Setup

  1. Open a fresh terminal in a new folder, e.g. mkdir lab-7 && cd lab-7.
  2. Create a Python 3.11 venv: python -m venv .venv && source .venv/bin/activate.
  3. Install the dependencies noted on each starter snippet (the small pip install … line above the code).
  4. Copy each starter into a file with the suggested name, then run it and capture the output.

Starter code

01 · Input + output guardrails
save as: 07_01_lab.py
python
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 text
Run with: python 07_01_lab.py
02 · Action allow-list before tool execution
save as: 07_02_lab.py
python
ALLOWED = {"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)
Run with: python 07_02_lab.py

Guided steps

Work through each step in order. Stop when your output matches the expected artifact — then push your work to your lab folder.

  1. 1
    Guardrail 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:
  2. 2
    Tool 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:
  3. 3
    Red-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.