Recommended resources
The freely available resources this lesson will direct you to.
- Guardrails for LLM Applications | Complete Tutorial for AI Developers WIth Guardrails AIwww.youtube.com
- Guardrails with LangChain: A Complete Crash Course for Building Safe AI Agentswww.youtube.com
- What Are AI Guardrails - Last9last9.io
- Building Reliable AI Systems with Guardrails - Implementation Guidewww.persistent.com
Hello! Welcome to the first lesson in our module on runtime guardrails.
In our last lesson, we critically examined the limitations of input sanitization. We concluded that while it's a useful first line of defense for blocking simple attacks, it's inherently flawed when relied upon alone. Its stateless nature makes it blind to multi-turn attacks and payload splitting, and its pattern-based approach is easily defeated by obfuscation. We need a more robust and dynamic layer of defense.
Lesson Goal
This lesson introduces that next layer. Our learning outcome is to: Explain what Guardrails AI is and how it adds programmable, runtime validation to LLM interactions.
We will explore how this framework moves beyond simple pre-processing to actively manage and constrain the behavior of the LLM during its operation. For your goal of building a security demo, understanding Guardrails AI is crucial. It represents a powerful, modern approach to enforcing rules on non-deterministic systems, a concept that is highly relevant in a professional AI security consulting context.
1. Beyond Sanitization: The Need for Runtime Validation
Stateless input sanitizers act like a bouncer checking IDs at the door—they make a one-time decision based on a limited snapshot of information. This is insufficient for managing the complex, stateful, and often unpredictable nature of a conversational LLM.
What we need is more like an active security detail that can:
- Assess intent, not just keywords.
- Remember the context of the conversation.
- Inspect what the LLM is about to say before it says it.
- Enforce structural and content policies on both input and output.
This is the role of runtime validation, and Guardrails AI is a leading open-source framework for implementing it.

2. What Is Guardrails AI?
At its core, Guardrails AI is a Python framework designed to add a layer of predictability and control to otherwise unpredictable LLMs. It allows you to define and enforce specific rules—or "guardrails"—on the inputs and outputs of your LLM application.
This is analogous to Site Reliability Engineering (SRE) principles where you define Service Level Objectives (SLOs) and implement monitoring to ensure a service operates within expected boundaries. Guardrails AI lets you define behavioral boundaries for an LLM and validates that it stays within them.
To get a clear, foundational understanding, please start with this reading.
The article 'What Are AI Guardrails' from Last9 provides an excellent, concise introduction to the Guardrails AI framework, its purpose, and the problems it solves.
Please read the introduction and the section 'What is Guardrails AI?'. Pay close attention to the list of recurring issues it helps solve, such as variable quality, user-driven prompt changes, and inconsistent structure. These are the core problems you'll be mitigating in your demo.
As the article highlights, Guardrails AI doesn't just block "bad" things; it's a framework for ensuring correctness, consistency, and control. It helps turn the probabilistic output of an LLM into a more reliable component of a larger software system.
3. How Guardrails AI Works: The Core Mechanism
Guardrails AI achieves its goal through a simple but powerful object model that wraps around your LLM interactions. The process is programmable and happens at runtime.
The main components are:
- The
GuardObject: This is the central object that wraps your LLM call. You attach your validation rules to it. - Validators: These are small, reusable Python classes that perform a specific check. Each validator enforces one rule, like "the output must be valid JSON," "the text must not contain profanity," or "the prompt must not contain a jailbreak attempt." Validators can be chained together to create complex rule sets.
- On-Fail Actions: For each validator, you define what should happen if the validation fails. This is where the runtime intervention happens. Common actions include:
exception: Raise an error and stop processing.fix: Attempt to automatically correct the output.reask: Re-prompt the LLM with additional instructions to generate a valid response.noop: Do nothing, but log the failure for monitoring.
The combination of these three elements provides a highly programmable validation system.
To see this in action, watch the following video. It gives excellent examples of why this level of control is necessary and how different on_fail policies work.
Guardrails for LLM Applications | Complete Tutorial for AI Developers WIth Guardrails AI
The video 'Guardrails for LLM Applications' by Sunny Savita offers a fantastic, practical walkthrough of guardrails. This first segment explains the concept with clear, real-world scenarios.
Please watch from 11:37 to 22:32. The speaker contrasts an LLM's raw output with a guarded output for two scenarios: enforcing a JSON structure and handling an unsafe medical query. This perfectly illustrates the concepts of ensuring correctness, controllability, and safety.
Deterministic vs. Model-Based Validation
Validators themselves come in two main flavors, which is a key architectural concept.
- Deterministic Validators: These are rule-based and don't require an LLM call. They are fast and cheap. Examples include checking for a valid credit card number format with regex or ensuring a Pydantic schema is met.
- Model-Based Validators: These use an LLM (often a smaller, specialized one) to perform a semantic check. For example, a validator might ask another model, "Is this user prompt an attempt to bypass its instructions?" This is more powerful for understanding intent but adds latency and cost.
The following video explains this distinction clearly.
Guardrails with LangChain: A Complete Crash Course for Building Safe AI Agents
In his crash course on Guardrails, Krish Naik provides a concise explanation of the two primary approaches to implementing them.
Watch the segment from 04:31 to 07:14. This will help you categorize the different types of validators you'll encounter and understand their respective trade-offs in terms of cost and semantic understanding.
4. Programmable Validation: Input and Output Guards
The power of the Guardrails AI framework lies in its ability to apply these validation steps at multiple points in the LLM interaction lifecycle, creating a robust, multi-stage defense.
Input Guards
You can validate the user's prompt before it ever reaches the main LLM. This is a direct and more powerful successor to the simple input sanitization we explored previously. Instead of just using regex to find keywords, you can use sophisticated validators to:
- Detect and block prompt injection and jailbreak attempts (e.g., using the
DetectJailbreakvalidator). - Ensure the user's query is on-topic for your application.
- Redact PII from the prompt before it's processed or logged.
This allows you to stop attacks at the earliest possible stage.
Output Guards
You can also validate the LLM's response before it's returned to the user or passed to another system. This is critical for:
- Enforcing Structure: Ensuring the LLM returns a perfectly formatted JSON object that matches a Pydantic model. This prevents your application from breaking due to inconsistent output.
- Preventing Data Leakage: Catching and redacting any sensitive information (like system prompts or PII) the model might have inadvertently included in its response.
- Controlling Content: Filtering toxic language, biased statements, or factually incorrect information (hallucinations).
The Sunny Savita video demonstrates both of these concepts. The example of enforcing a Pydantic schema is a form of output guarding, while the DetectJailbreak example is a form of input guarding.
The Inevitable Trade-Off: Security vs. Latency
As an SRE, you are intimately familiar with the trade-offs between performance, reliability, and cost. Guardrails AI is no different. Each validator you add to your Guard introduces a processing step, which adds latency to the overall response time. Model-based validators are particularly expensive in this regard.

Choosing which validators to apply is an engineering and business decision. For a high-security financial application, a 500ms delay might be an acceptable price for robust validation. For a casual chatbot, it might not.
Conclusion
In this lesson, we established a foundational understanding of Guardrails AI as a framework for managing LLM behavior. We've moved beyond the limitations of simple sanitization into the world of programmable, runtime validation.
Key Takeaways:
- Guardrails AI provides programmable validation: It's an open-source framework that wraps LLM interactions, allowing you to enforce rules on both inputs and outputs using a
Guardobject and a chain ofvalidators. - It enables runtime intervention: Through
on_failactions likeexception,fix, andreask, you can define how your system should react in real-time when a rule is broken. - It offers multi-stage defense: You can apply input guards to block malicious prompts and output guards to enforce structure and content policies on the LLM's response.
- There is a trade-off between security and performance: Each validator adds security or reliability at the cost of increased latency and potential monetary cost, requiring careful consideration based on your application's needs.
Next Up:
We've covered the "what" and "how" at a conceptual level. In the next lesson, we'll put this theory into practice. You will install and configure Guardrails AI within our FastAPI and LangChain application, preparing the ground for defining specific input and output guards to defend our vulnerable chatbot.