Recommended resources
The freely available resources this lesson will direct you to.
- Master ALL 20 Agentic AI Design Patterns [Complete Course]www.youtube.com
- Open Approach to Trust & Safety: Llama Guard 3, Prompt Guard & More | Llama for Developerswww.youtube.com
- Patterns for Building LLM-based Systems & Productseugeneyan.com
- Llama Guard 3: Modular Safety Classifier - Emergent Mindwww.emergentmind.com
Hello! In our last lesson, we established the critical role of programmatic validation in creating deterministic, provably safe agentic systems. We saw how tools like Pydantic can enforce structure and how custom validators can check for everything from business rule violations to factual grounding.
Today, we'll build on that foundation by focusing on a specific, powerful type of programmatic validation. Your learning outcome is to integrate an external content moderation layer to screen agent inputs and outputs. This is a fundamental pattern for building production-ready systems that you can confidently deliver to clients, as it outsources the complex and ever-evolving task of content safety to a specialized, dedicated component.
1. The Principle of Separation of Concerns in AI Safety
In system architecture, the principle of separation of concerns dictates that a system should be divided into distinct parts with minimal overlap in functionality. You wouldn't typically embed your database logic directly inside your UI code. The same principle applies with even greater force to AI safety.
An external content moderation layer is a service—often a specialized model—that acts as a dedicated gatekeeper. Its sole responsibility is to classify text against a predefined safety policy. It functions as a classic middleware or filter:
- It screens user inputs before they reach your primary agent.
- It screens agent outputs before they are sent back to the user.
This approach is analogous to using a Web Application Firewall (WAF) in network security. Instead of trying to code security rules into every endpoint of your application, you deploy a specialized appliance that inspects all traffic. This is a mature architectural pattern that significantly enhances robustness and maintainability. For the agentic systems you'll design for clients, this isn't just a good idea; it's a requirement for responsible deployment.
To frame this concept, let's briefly review a general article on guardrails.
Patterns for Building LLM-based Systems & Products
The article 'Patterns for Building LLM-based Systems & Products' by Eugene Yan provides a good overview of guardrails. We'll look at the sections that specifically define content and input safety checks, which are the domain of external moderation layers.
Please read the introduction to 'Guardrails' and the subsection 'How to apply guardrails?', focusing specifically on the paragraphs about 'Content safety guardrails' and 'Input guardrails'. Notice how it describes using classifiers to check for harmful content—this is precisely what an external moderation layer does.
2. A Concrete Architectural Pattern
Implementing an external moderation layer follows a clear, logical flow. Think of it as airport security for your agentic system.
Let's watch a short video that outlines this process. While it discusses guardrails generally, its step-by-step model for input checking is a perfect illustration of how an external moderation layer fits into a larger system.
Master ALL 20 Agentic AI Design Patterns [Complete Course]
In his 'Agentic AI Design Patterns' course, Mark Kashef provides a clear, practical workflow for input validation that we can adopt for our moderation layer.
Please watch from 52:38 to 56:09. Pay close attention to the sequence he describes for processing an input: Sanitize Input: The initial cleaning step. PII Detection/Redaction: Checking for and masking sensitive personal information. Injection Detection: Looking for adversarial attacks (which we'll discuss separately). Malicious Content Filter/Block: This is the core function of our external moderation layer. Risk Classification & Human-in-the-Loop: Based on the output of the moderation layer, the system decides whether to proceed, block, or escalate to a human.
This sequence forms a robust pre-processing pipeline for user input. A similar, though often simpler, pipeline is used to check the agent's output before it's displayed to the user. The key architectural decision is that these checks are performed by a component external to the main reasoning agent.
3. Case Study: Meta's Purple Llama and Llama Guard
To make this concrete, let's examine one of the most prominent open-source initiatives in this space: Meta's Purple Llama. This suite of tools is designed to provide modular, system-level safety for LLM applications.
The centerpiece is Llama Guard, an LLM fine-tuned specifically for content classification. It's not a general-purpose chatbot; it's a safety expert.
Let's explore the philosophy and components of this initiative.
Open Approach to Trust & Safety: Llama Guard 3, Prompt Guard & More | Llama for Developers
This developer-focused video from AI at Meta introduces their 'Open Approach to Trust & Safety,' which centers on the Purple Llama toolset. It provides an excellent high-level view of why a modular, system-level approach to safety is necessary for building agentic systems.
Please watch the following segments: The Vision for System-Level Safety (02:00 - 03:46): Understand the shift from model-level safety to a broader, system-level paradigm. This justifies our architectural choice to use an external layer. The Power of Modularity (06:51 - 10:30): Listen for the argument that a one-size-fits-all safety approach is ineffective. Modular tools allow you to create bespoke safety solutions for different client use cases, a key advantage when delivering AI as a service. Introducing Llama Guard (10:30 - 13:22): This section details what Llama Guard is—an input/output content moderation filter—and how it works. This is the core of our lesson.
As you saw, Llama Guard is designed to be an independent component. It takes a piece of text and a safety policy, and it returns a classification: safe or unsafe, along with the category of any violation.
This image shows a perfect example of Llama Guard in action.

Notice the structure:
- The Policy: A clear, human-readable (and machine-readable) definition of what is considered unsafe, broken down into categories. This policy is customizable.
- The Conversation: The text to be evaluated. Here, it's the agent's turn.
- The Assessment: The model's simple, structured output, which your system can then programmatically act upon.
This modularity is a significant architectural advantage. For a deeper technical dive into how it's designed to be integrated, let's look at a summary.
Llama Guard 3: Modular Safety Classifier
This article from Emergent Mind, 'Llama Guard 3: Modular Safety Classifier,' concisely summarizes the technical design and deployment principles.
Please read sections 1 ('Design Principles and Function') and 4 ('Modularity, Language, and Capability Coverage'). Focus on how the text describes Llama Guard as an 'independent component' that can be 'wrapped around any Llama 3 instance' for input or output filtering. This reinforces the architectural pattern we're discussing.
4. Integrating the Moderation Layer into System Architecture
Now, let's visualize where this component fits. The diagram below shows the architecture for NVIDIA's NeMo Guardrails, a similar system for controlling conversational AI.

In this architecture, the "NeMo Guardrails Runtime" would be responsible for making the API call to an external moderation layer like Llama Guard. The flow would be:
- User input enters the runtime.
- The runtime sends the input to the Llama Guard API for input screening.
- Llama Guard returns a
safe/unsafeclassification. - If
safe, the runtime proceeds, eventually calling the main LLM service. - The main LLM generates a response.
- The runtime sends this response to the Llama Guard API for output screening.
- If
safe, the runtime sends the response to the user. - If at any point the classification is
unsafe, the runtime executes a fallback procedure (e.g., reject, retry, or escalate).
This clean separation ensures your primary agent doesn't need to be "aware" of the content moderation policy. It simply operates within the safe corridor created by the external moderation layer. This makes the system easier to design, test, and maintain.
Conclusion
In this lesson, we have seen how to architect a critical safety component for any production-grade agentic system. By integrating an external content moderation layer, you are not just adding a feature; you are adopting a mature software engineering principle—separation of concerns—to solve one of the hardest problems in AI safety.
Key Takeaways:
- An external content moderation layer is a specialized service that acts as a programmatic filter for agent inputs and outputs.
- This architectural pattern offloads the complex, dynamic task of content safety to an expert component, allowing you to focus on the agent's core logic.
- Tools like Meta's Llama Guard provide powerful, open-source, and customizable classifiers that can be "wrapped around" any primary LLM.
- The integration pattern is a straightforward middleware flow:
Input -> Moderation Check -> Agent -> Moderation Check -> Output. - For the systems you build for clients, using a dedicated moderation layer is a non-negotiable best practice for responsible and reliable deployment.
In our previous lessons, we've built up a formidable defense-in-depth strategy: prompt-based guardrails for semantic guidance, programmatic validation for structural integrity, and now, external layers for content safety. With these safeguards in place, we can begin to trust our agents with more autonomy.
Our next lesson, "Define a graduated autonomy plan," will address exactly that. We'll explore how to design a deployment strategy that starts with heavy human oversight and methodically reduces it as you gather evidence that your guardrails are working effectively.