Guardrails in practice and secure RAG
Where guardrails sit in a request, what guard models and rule checks are each good at, how to measure their cost in latency and false positives, and how to secure retrieval: access control before ranking, tenant isolation, screening retrieved text for injected instructions, and output checks. With a runnable three-layer example.
By Sandip Maity, Applied AI Engineer
"Add guardrails" is the most common security advice for LLM apps and the least specific. This lesson makes it concrete: where each check sits, what it catches, what it costs, and how to secure the part of the system most apps get wrong first, retrieval.
#Where guardrails sit
- 1Inputauthenticate, rate-limit, length cap, topic and safety check
- 2Retrieveaccess-control filter, tenant isolation
- 3Screenretrieved and tool text checked for injected instructions
- 4Modelsystem prompt, least-privilege tools
- 5Outputsafety, secrets and PII, links, schema validation
- 6Actconfirmation for risky tool calls; log everything
No single check is reliable. The point of layers is that an attack must get past all of them (defense in depth).
#Kinds of checks
| Check | Good at | Weak at | Cost |
|---|---|---|---|
| Rules and regexes | Secrets, card numbers, known phrases, URLs, length | Paraphrase, other languages | Microseconds |
| Small classifiers (topic, toxicity, injection) | Fast, tunable thresholds | New attack styles | Milliseconds on CPU |
| Guard models (an LLM fine-tuned to label content against a policy, for example Llama Guard, ShieldGemma or Granite Guardian) | Nuanced policy categories, many languages | Latency, cost, their own false positives | A model call, often 50 to 300 ms |
| Schema validation | Structured output, tool arguments | Content meaning | Microseconds |
| Human approval | Irreversible or high-value actions | Scale | Minutes |
Frameworks such as NeMo Guardrails or Guardrails AI help wire these together; the design decisions are the same either way.
#Measure guardrails like any model
A guardrail is a classifier, so measure it like one (metrics and statistics):
- Catch rate on an attack set, by attack family (jailbreak taxonomy).
- False positive rate on real, legitimate traffic. This is the number teams forget, and it decides whether users tolerate the product.
- Added latency at p95, especially for guard-model calls on the critical path. Run input checks in parallel with retrieval where you can, and stream output while checking it in chunks if your UX needs streaming (streaming).
Pro content
Keep reading with Inference Academy Pro
Unlock the rest of this lesson, plus every advanced lesson, hands-on lab, the larger playground models and completion certificates.
Still to come in this lesson
- Secure RAG
- Operating it
- Common mistakes
- Exercises
You will be able to answer
- Where should access control be enforced in a RAG system?
- Why check model output even when inputs are screened?
- A guard model blocks 98% of attacks but also 15% of normal requests. What is the main problem?
- What is a cheap way to keep tenants' data apart in a shared vector store?
- Retrieved text says 'ignore previous instructions and...'. What should the pipeline do?