Agentic AI

Guardrails

Middleware that checks what goes in and out: PII, human approval and custom rules.

Guardrails are checks in code that run at fixed points of every agent run: before the agent starts, around each model and tool call, and before the answer goes back. This module starts with the ideas (what to check, where, and with rules or with a model), then uses LangChain's built-in middleware to redact personal data and to require a person's approval, writes its own input and output checks as middleware classes, and finally stacks them into a healthcare assistant, ending with the assistant in one script with Gemini. Run the notebook cells in order, and try each practice task before you open its solution.

Lessons

  1. 1What guardrails areWhy agents need guardrails, the risks they address, the four checkpoints of an agent run, deterministic and model-based checks compared on the same inputs, layering and failing closed, what guardrails can't do, interview questions and two practice tasks in plain Python.
  2. 2PII and approval middlewareWhy personal data must not reach the model, PIIMiddleware's types and four strategies, what the model, the tools and the stored conversation see, testing the strategies without a model, what the detectors catch and miss, custom detectors, input, output and tool results, blocking on an agent with memory, approval as a guardrail, interview questions and a practice task.
  3. 3Custom guardrails with hooksAgentMiddleware subclasses, before_agent and after_agent, ending a run with jump_to and hook_config, a keyword filter and a model-based safety judge, testing hooks by calling them, why a filter must read the newest message, why a hook must return its changes instead of editing the state, the function form, which hook for which check, interview questions and two practice tasks.
  4. 4Layered guardrails: a healthcare assistantStacking middleware, the order before, after and wrap hooks run in, a five-layer agent and what its layers get wrong, the healthcare assistant with both fixes, four saved runs and a blocked one, the stack in run order, the guardrails at a glance, the assistant in one script with Gemini, interview questions and two practice tasks.