Week 16 • Day 2Week 16
Day 2:Policy-as-Code Guardrails (Input, Output & Tool Rails)
Battle-tested daily lesson note • 100% runnable code, interview Q&A, test suite & interactive quiz
Day 2: Policy-as-Code Guardrails (Input, Output & Tool Rails)
1. First-Principles Principle: Never Use an LLM to Enforce Hard Security Policies
An LLM-based guardrail ("Judge LLM") is probabilistic and vulnerable to adversarial jailbreaks. Hard operational boundaries (Role-Based Access Control, production replica ceilings, read-only maintenance freezes) must be enforced by deterministic Policy-as-Code before any tool executes.
Three-Rail Guardrail Architecture
- Input Rail: Screens incoming user prompts for prompt-injection signatures (
IGNORE ALL PREVIOUS INSTRUCTIONS, system prompt extraction) and PII/secrets. - Tool Execution Rail (Policy-as-Code): Evaluates
(caller_role, environment, tool_name, arguments)against deterministic policy rules:ALLOWExecute immediately.REQUIRE_APPROVALPause graph execution and trigger Human-in-the-Loop (HITL) escalation.DENYBlock tool call and log policy violation.
- Output Rail: Verifies the final response contains no leaked credentials or prohibited financial/legal advice.
In Simple English: Input, Tool & Output Guardrails (Policy-as-Code)
Think of guardrails as 3 security checkpoints around your agent:
- Input Rail (Front Door): Catch prompt injections, extreme token bombs, and banned PII before paying LLM tokens.
- Tool Rail (Vault Door): Block destructive tool arguments (
DROP TABLE, private IP fetches, unapproved money transfers). - Output Rail (Exit Door): Scrub accidental leaked API keys (
sk-...,AIza...), enforce valid JSON schema, and verify grounding before the user sees a single word!