Lesson 15.1 · 23 min · video lesson
LLM Guardrails: Filtering Inputs and Outputs for Safety
The model is trained to be helpful and safe, so why do production chatbots still wrap it in extra layers of checks?
In short: LLM guardrails are checks placed around a language model that inspect what goes in and what comes out, and then allow, block, modify or escalate. Input guardrails catch harmful, off-topic or injected prompts before the model sees them; output guardrails catch unsafe content, leaked data, hallucinations and broken formats before users see them. Guardrails range from simple rules to classifier models and LLM judges, and work best in layers, because none is perfect.
What you will learn
- What is an LLM
- What are LLM guardrails
- Why do we need guardrails
- Where guardrails sit: input and output
- Types of guardrails
- A simple input guardrail with code
- A simple output guardrail with code
- Using another model as a guardrail
- A step-by-step walkthrough of a request
- Limitations of guardrails
- Best practices for guardrails
In this lesson
- What is an LLM, and what are guardrails?
- Why do we need guardrails?
- Where guardrails sit: input and output
- Types of guardrails
- A simple input guardrail and output guardrail, in code
- Using another model as a guardrail
- A step-by-step walkthrough of a request
- Limitations and best practices
- Worked example, step by step
- Practice: try it yourself
Key terms in this lesson
- Guardrail
- A check around an LLM that inspects inputs, outputs or actions and enforces a policy.
- Input guardrail
- A check that runs on the user's input before the model is called.
- Output guardrail
- A check that runs on the model's output before it reaches the user or a tool.
- Guard model
- A separate model trained or prompted to classify text as safe or unsafe for a policy.
- PII
- Personally identifiable information, such as names, emails, phone or card numbers.
- False positive
- A harmless request or answer wrongly blocked by a guardrail.
This lesson is part of the Generative AI Engineering and Complete AI Engineer tracks. See plans and pricing to open the full lesson, its video, labs and quiz, or try the free lesson first (free account needed).
← 14.4 Agent Observability: Traces, Spans, and Debug Signals · 15.2 Prompt Injection: Attacks Against LLM-Powered Systems →