Skip to content

Instantly share code, notes, and snippets.

@matthew-harper
Created October 5, 2025 00:29
Show Gist options
  • Select an option

  • Save matthew-harper/ba986041b60960e05b5adbe31dc9ebaf to your computer and use it in GitHub Desktop.

Select an option

Save matthew-harper/ba986041b60960e05b5adbe31dc9ebaf to your computer and use it in GitHub Desktop.
Pattern 4 — Guardrails as Context Hygiene (shadow vs enforce)
import re
def run_guardrails_demo(prompt: str, shadow: bool = True) -> None:
"""Pattern 4: Guardrails as Context Hygiene"""
print(f"\nInput: {prompt!r}")
print(f"Mode: {'Shadow (logging only)' if shadow else 'Enforcement'}")
if shadow:
print("Shadow mode: logging only (no enforcement).")
print(f"Response: {prompt}")
print("(Would log to monitoring system: PII pattern detected)")
else:
redacted = re.sub(r"\b\d{3}-\d{2}-\d{4}\b", "<REDACTED-SSN>", prompt)
print(f"Enforced response: {redacted}")
if redacted != prompt:
print("(PII detected and redacted)")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment