Duration: 4 hours | Difficulty: Intermediate–Advanced | Prerequisites: Modules 01–08
Learning Objectives
By the end of this module, you will be able to:
- Map common LLM threat categories (injection, data exfil, toxic outputs)
- Apply layered defenses for prompts and tools
- Validate and filter outputs before users see them
- Integrate Guardrails AI or NeMo Guardrails-style policies
- Follow a responsible deployment checklist
1. The Threat Landscape
Why Safety Matters
Models optimize for plausible text, not safe outcomes. In prod, you own PII, brand, and legal risk—assume adversarial users and sloppy insiders.
| Risk | Example |
|---|---|
| Data leak | Model echoes secrets from context |
| Abuse | Instructions for harm |
| Misinformation | Confident wrong medical/legal claims |
| Availability | Prompt bomb spikes cost |
Try This! Write three “friendly” prompts that try to exfiltrate system instructions—see if your app blurs secrets.
2. Prompt Injection Attacks and Defenses
Types of Prompt Injection
| Style | What attacker does |
|---|---|
| Direct | “Ignore previous rules…” |
| Indirect | Poisoned doc retrieved by RAG |
| Tool misuse | Args that hit forbidden APIs |
Defense: Input Validation
| Layer | Idea |
|---|---|
| Length limits | Cheap DoS brake |
| Allowlists | Only expected languages/channels |
| Separate trusted system text | Don’t concat untrusted docs into system |
Defense: LLM-Based Injection Detection
Run a small classifier prompt (“is this trying to override policies?”) before the main call—treat as heuristic, not proof.
Concept: Defense in depth beats one magic filter.
3. Output Validation and Filtering
Key Example: Regex PII scan + simple issue list—extend with your DLP vendor patterns.
[object Object], re
,[object Object], dataclasses ,[object Object], dataclass
,[object Object],
,[object Object], ,[object Object],:
patterns: ,[object Object],[,[object Object],, ,[object Object],]
,[object Object], ,[object Object],(,[object Object],):
,[object Object],._rx = {k: re.,[object Object],(v) ,[object Object], k, v ,[object Object], ,[object Object],.patterns.items()}
,[object Object], ,[object Object],(,[object Object],) -> ,[object Object],:
hits = {name: rx.findall(text) ,[object Object], name, rx ,[object Object], ,[object Object],._rx.items() ,[object Object], rx.search(text)}
,[object Object], {,[object Object],: ,[object Object], hits, ,[object Object],: hits}
guard = OutputGuard(
{
,[object Object],: ,[object Object],,
,[object Object],: ,[object Object],,
}
)
sample = ,[object Object],
,[object Object],(guard.scan(sample))| Post-process | When |
|---|---|
| Redact PII | Support transcripts |
| Block toxic phrases | Consumer apps |
| JSON schema validate | Tool pipelines |
4. Guardrails AI
Declarative validators (ToxicLanguage, DetectPII, etc.) wrap outputs or prompts. Configure on_fail strategies: fix, reask, exception.
| Plus | Minus |
|---|---|
| Fast to prototype | Another dependency + latency |
Fun Fact: Validators can fight each other—e.g., PII redactor vs citation requirement—test combined configs.
5. NeMo Guardrails
YAML + Colang flows describe conversational policies: input rails, output rails, specialized dialog flows. Good when you want explicit policy files reviewed by compliance.
| Asset | Role |
|---|---|
config.yml | Model endpoints |
.co flows | Allowed / forbidden topics |
6. Responsible AI Deployment Checklist
| Area | Sample items |
|---|---|
| Input | Rate limits, injection alerts, file type allowlist |
| Output | PII scan, moderation, citation rules for RAG |
| System | Secret management, audit logs, kill switch |
| People | Disclosure, escalation path, red-team cadence |
Key Takeaway
Safety is ongoing ops: logs, drills, and updates—not a checkbox before launch.
Practice Exercises
Exercise 1: Injection Red Team (Beginner)
10 prompts; document which defenses triggered.
Exercise 2: PII Shield (Intermediate)
Wire OutputGuard into FastAPI middleware.
Exercise 3: Content Moderation Pipeline (Intermediate)
Tier-1 regex, tier-2 LLM judge.
Exercise 4: Guardrails Integration (Advanced)
Guardrails AI + JSON schema for tool args.
Exercise 5: Adversarial Testing Suite (Advanced)
Mutation fuzzing on user prompts.
Mini-Project: Secure AI Chat Application
Chat UI + RAG + output guard + rate limit + admin view of blocked events (no PII stored raw).
Key Takeaways
Key Takeaway
- Treat user content and retrieved docs as hostile by default.
- Layer input and output controls; no single silver bullet.
- Structured tool args shrink injection surface vs freeform code gen.
- Policy-as-code (NeMo/Guardrails) helps compliance reviews.
- Monitoring + runbooks complete the story—tech alone isn’t enough.