Recommended resources
The freely available resources this lesson will direct you to.
- LLM Chronicles #6.9: Design Patterns for Securing LLM Agents Against Prompt Injection (Paper Review)www.youtube.com
- Understanding Constitutional AI - the paper and key conceptswww.youtube.com
- [PDF] NeMo Guardrails: A Toolkit for Controllable and Safe LLM ...aclanthology.org
- Constitutional AI: Harmlessness from AI Feedbackarxiv.org
Hello! Welcome to the next lesson in our module on "Human-in-the-Loop and Guardrails."
In our previous session, we designed the core mechanisms for human oversight: approval checkpoints and escalation triggers. We established how to pause a workflow and bring a human into the loop when an agent encounters a situation requiring judgment. We now shift our focus from the reaction to a problem to the prevention of it.
This lesson addresses the fundamental question: How do we define the rules of "good behavior" for an agent in the first place? Your learning outcome is to apply prompt-based guardrails to define an agent's safety boundaries and expected behavior. We will explore how to embed principles, policies, and constraints directly into an agent's operational logic using prompts, forming the first and most crucial line of defense in a safe and reliable system.
1. Guardrails: The Rules of the Road
In agentic systems, "guardrails" are mechanisms designed to control an agent's behavior, ensuring it stays within predefined boundaries. They are the policies that prevent an agent from generating harmful content, going off-topic, misusing tools, or violating client-specific rules. Violations of these guardrails are often the very events that fire the escalation triggers we designed in the last lesson.
There are two primary categories of guardrails:
- Prompt-Based Guardrails: These leverage the LLM's instruction-following capabilities. We define the rules using natural language within the prompts we send to the model. They are flexible and powerful for controlling nuanced behaviors like tone and ethical stances.
- Programmatic Guardrails: These are deterministic checks implemented in code. For example, validating that an agent's output conforms to a specific JSON schema or that a financial transaction amount is below a hard-coded limit.
This lesson focuses on the first category. While probabilistic, prompt-based guardrails are indispensable for shaping the high-level, semantic behavior of an agent. Let's explore three powerful techniques for implementing them, moving from high-level principles to low-level security architecture.
2. Technique 1: Constitutional AI - Encoding Principles
A powerful, high-level approach to instilling safety is to provide the agent with a "constitution"—a set of explicit principles it must follow. This technique, pioneered by Anthropic, shifts the burden from constant human labeling of "bad" outputs to teaching the model to self-correct based on its principles.
The core idea is a two-stage process:
- Supervised Fine-Tuning: A model is prompted to generate a response, critique its own response against a constitutional principle (e.g., "Do not encourage illegal activity"), and then revise the response based on the critique. The model is then fine-tuned on these self-corrected, "more harmless" responses.
- Reinforcement Learning from AI Feedback (RLAIF): A preference model is trained to choose which of two agent responses better adheres to the constitution. This AI-generated preference data is then used as a reward signal to further train the agent, reinforcing constitutionally-aligned behavior at scale.
While you won't be fine-tuning models yourself, understanding this concept is crucial for system design. It demonstrates that an agent's safety boundaries can be derived from a human-authored set of principles, which could be your client's brand voice, a code of conduct, or an ethical framework.
To get a concise overview, start with this video.
Understanding Constitutional AI - the paper and key concepts
The video 'Understanding Constitutional AI' provides a clear, high-level explanation of the paper and the core concepts.
Please watch the first 4 minutes and 38 seconds. Focus on how the model is prompted to critique and then revise its own harmful response, and how this data is used for fine-tuning. This is the essence of the supervised learning stage.
Now, let's go to the source paper for a more rigorous understanding. Given your background, the methodology will be of particular interest.
Constitutional AI: Harmlessness from AI Feedback
The paper 'Constitutional AI: Harmlessness from AI Feedback' describes the full methodology. We will focus on the sections that detail the process and provide concrete examples of the prompts and principles used.
Please read the following sections: Section 3.1 'Method' (page 7): This details the prompt chain for critique and revision, including the Wi-Fi hacking example. It's a concrete demonstration of a prompt-based guardrail in action. Appendix C 'Constitutional Principles...' (pages 21-22): Skim through the lists of principles for the SL-CAI and RL-CAI stages. These are prime examples of what 'prompt-based guardrails' look like in practice. Appendix A 'Sample critiques and revisions' (page 18): Review the 'stealing from a grocery store' example. It beautifully illustrates the iterative refinement process, showing how the agent's responses become progressively more aligned with its constitution.
As an architect, your takeaway is that you can define a client's "constitution" and use it within your system prompts to guide agent behavior, even without fine-tuning. For example, a system prompt can include a rule: "Before responding, critique your draft against the principle of 'do no harm'. If the critique is negative, revise your response."
3. Technique 2: Runtime Moderation Rails
A more direct and common approach is to implement a moderation layer. This pattern treats safety checking as a distinct step in the agent's execution flow, often using a separate, highly-aligned LLM as a "moderator." This is analogous to having a security checkpoint for all incoming and outgoing traffic.
NVIDIA's NeMo Guardrails is an open-source toolkit that formalizes this pattern. It acts as a proxy between the user and the agent, enforcing programmable rails.

The moderation component typically has two parts:
- Input Moderation (Jailbreak Rail): This rail inspects the user's prompt before it reaches the main agent. Its goal is to detect malicious intent, prompt injection attacks, or requests that are out of scope.
- Output Moderation: This rail inspects the agent's generated response before it's sent to the user. Its goal is to catch any harmful, unethical, or inappropriate content the agent might have produced.
The following paper provides the exact prompt templates used to implement this pattern.
[PDF] NeMo Guardrails: A Toolkit for Controllable and Safe LLM ...
The 'NeMo Guardrails' paper details a practical implementation of a guardrail system. We will focus on the moderation rails, which are a direct application of prompt-based safety checks.
Please read the following sections: Section 3.3.3 'Moderation Rails' (page 434): This section describes the input and output moderation pipeline. Appendix D.2 'Moderation Rails' (page 441): This is the key part. It gives you the precise, simple prompt templates used to ask an LLM to vet both user input and bot responses. Notice the direct, zero-shot nature of the prompts, asking for a simple 'yes/no' answer.
This pattern offers a clean separation of concerns. Your primary agent can focus on its task, while a specialized moderation service handles safety. This is a robust design you can propose to clients, as it makes the safety component explicit and auditable.
4. Technique 3: Architectural Guardrails
The most robust guardrails are not just prompts; they are embedded in the very architecture of the system. This approach is particularly effective against security threats like prompt injection, where an attacker embeds malicious instructions in what should be passive data (e.g., in an email the agent is summarizing). An LLM trying to follow all instructions at once can be tricked into executing the malicious ones.
From your computer science background, you can think of this as applying principles like process isolation, sandboxing, and least privilege to agent design.

The following video provides an excellent overview of architectural design patterns that serve as powerful guardrails against prompt injection.
LLM Chronicles #6.9: Design Patterns for Securing LLM Agents Against Prompt Injection (Paper Review)
This video, 'Design Patterns for Securing LLM Agents', reviews a paper on architectural solutions to prompt injection. It translates security principles into concrete agent designs.
Please watch the following segments, focusing on the system design logic: Prompt Injection Threat (03:02 - 05:28): A clear definition of the problem. Action Selector Pattern (09:28 - 12:24): The LLM can only select from a predefined list of safe actions, not generate arbitrary ones. This severely limits an attacker's capabilities. Dual LLM Pattern (16:50 - 20:19): This is a key pattern. A 'privileged' LLM has tool access but never sees untrusted data. A 'quarantined' LLM processes untrusted data but has no tool access. This is a direct parallel to sandboxing. Context Minimization Pattern (24:37 - 26:23): The system extracts structured intent from a user prompt and then discards the original prompt. This removes any hidden malicious instructions before they can influence subsequent steps.
These architectural patterns are your strongest defense. They don't just ask the agent to behave; they make it structurally impossible for certain types of misbehavior to occur. When designing a system for a client, especially for high-stakes applications, recommending a pattern like Dual LLM demonstrates a deep understanding of production-grade security.
Conclusion
In this lesson, we have equipped you with a multi-layered toolkit for defining and enforcing an agent's safety boundaries and expected behaviors using prompt-based guardrails.
Key Takeaways:
- Guardrails are Non-Negotiable: They are the foundation of a safe, trustworthy agentic system.
- Layer Your Defenses: A robust strategy combines multiple techniques. You can define high-level principles via a Constitution, enforce them at runtime with a Moderation Layer, and build the most secure foundation using Architectural Patterns like the Dual LLM model.
- From Principles to Prompts: A client's abstract requirements (e.g., "be professional," "don't give medical advice") can be translated into concrete constitutional principles and moderation prompts.
- Architecture is the Ultimate Guardrail: For threats like prompt injection, clever prompting is insufficient. The structure of the system itself is the most effective defense, a principle that aligns directly with established best practices in computer security.
In our next lesson, we will complement these probabilistic, prompt-based rules with the certainty of code. We will explore how to design programmatic validation on agent outputs to filter or reject responses that violate strict, deterministic constraints, adding another critical layer to our defense-in-depth strategy.