Skip to content
Inference Academy

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

17 min · verified 2026-11Pro
Share

"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

  1. 1
    Inputauthenticate, rate-limit, length cap, topic and safety check
  2. 2
    Retrieveaccess-control filter, tenant isolation
  3. 3
    Screenretrieved and tool text checked for injected instructions
  4. 4
    Modelsystem prompt, least-privilege tools
  5. 5
    Outputsafety, secrets and PII, links, schema validation
  6. 6
    Actconfirmation 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

  1. Secure RAG
  2. Operating it
  3. Common mistakes
  4. 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?