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

  1. Input Rail: Screens incoming user prompts for prompt-injection signatures (IGNORE ALL PREVIOUS INSTRUCTIONS, system prompt extraction) and PII/secrets.
  2. Tool Execution Rail (Policy-as-Code): Evaluates (caller_role, environment, tool_name, arguments) against deterministic policy rules:
    • ALLOW →\to Execute immediately.
    • REQUIRE_APPROVAL →\to Pause graph execution and trigger Human-in-the-Loop (HITL) escalation.
    • DENY →\to Block tool call and log policy violation.
  3. 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:

  1. Input Rail (Front Door): Catch prompt injections, extreme token bombs, and banned PII before paying LLM tokens.
  2. Tool Rail (Vault Door): Block destructive tool arguments (DROP TABLE, private IP fetches, unapproved money transfers).
  3. 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!