Guardrails for LLM Apps: Blocking Prompt Injection and Toxic Output
Guardrails for LLM apps block prompt injection and toxic output with a layered stack of input rails, output filters, tool gating and logging. How to build it.
By Galactic News Editorial · Published 2026-11-23T05:02:25+00:00 · Updated 2026-11-23T05:02:25+00:00
Guardrails for LLM Apps: Blocking Prompt Injection and Toxic Output
Guardrails for LLM apps are checks placed around the model to block prompt injection and toxic output. They filter inputs, validate outputs, limit tool access, and log every decision. Prompt injection is the top risk on the OWASP Top 10 for LLM Applications. No single guardrail stops it. A layered stack does.
The core problem is simple. A language model cannot reliably tell instructions from data. Both are text. An attacker can hide instructions in a web page, a PDF, an email, or a tool result that the model later reads. The model follows the hidden instruction as if it were yours. Guardrails assume that will happen, and limit what the model can do when it does.
What prompt injection looks like
OWASP lists prompt injection as LLM01, the number one risk in its 2025 Top 10 for LLM Applications. The list is maintained by the OWASP GenAI Security Project. The 2025 edition, also called v2.0, was released on November 18, 2024. It covers ten risks, from prompt injection to unbounded consumption.
Injection comes in two forms. Direct injection is a user typing instructions meant to override the system prompt, such as asking the model to ignore its instructions and print its configuration. Indirect injection hides instructions in content the model retrieves later: a web page, a resume, an email, or a document. When the model processes that content, the embedded instructions run.
OWASP states that it is unclear whether foolproof prevention methods exist for prompt injection. That is the honest starting point. The goal is not zero risk. The goal is to detect most attempts, and to limit the damage from the ones that get through.
The six guardrail layers
Production safety is a stack, not a toggle. Six layers cover the pipeline from input to action:
| Layer | Where it runs | What it blocks | OWASP risk |
|---|---|---|---|
| Input validation | Before the model | Jailbreaks, injection patterns, toxic prompts | LLM01 |
| Prompt hardening | Before the model | Instruction override, role confusion | LLM01, LLM07 |
| Retrieval rail | Before the model | Poisoned or adversarial RAG chunks | LLM01 indirect |
| Output filtering | After the model | PII leaks, toxic content, bad formats | LLM02, LLM05 |
| Tool-call gating | After the model | Excessive agency, unauthorised actions | LLM06 |
| Managed moderation | After the model | Harmful content scoring | LLM09 |
The two layers teams skip most often are the retrieval rail and tool-call gating. Both matter most for agents, because agents read untrusted content and take actions. A chatbot that only answers questions can live without them. An agent that reads email and sends payments cannot.
Guardrails LLM apps prompt injection: input and prompt defences
Input validation runs before the prompt reaches the model. It combines string checks with classifiers. A classifier such as Llama Guard or Lakera Guard scores the input for injection and jailbreak patterns. Simple pattern checks catch known phrases. Neither is perfect. Detection is probabilistic, so some attacks pass and some legitimate requests get blocked.
NVIDIA NeMo Guardrails is the most widely used open-source framework for this layer. It is a Python library that supports Python 3.10 to 3.13, with its latest release listed as 0.24.1 in the project README. Rails are declared in a config file. Input rails check user input. Output rails check model replies. Dialog, retrieval, and execution rails cover the rest. A basic setup uses a self-check input flow and a self-check output flow, plus a PII check that redacts entities such as social security numbers, credit cards, emails, and phone numbers.
Prompt hardening is the second input layer. State the model's role and its limits in the system prompt. Mark untrusted external content so it stays separate from user prompts. Two rules are firm: never put credentials, connection strings, or API keys in the prompt, and never rely on the prompt to enforce security. Assume an attacker can read the whole prompt.
Blocking toxic output and leaks
Output filtering runs after the model replies and before the user sees it. It has three jobs. First, redact personal data. Tools such as Microsoft Presidio and NeMo Guardrails rails scan for PII and remove it. Second, score the reply for toxicity and harmful content. Third, validate the output format with deterministic code, so a malformed reply never reaches a downstream system.
Guardrails AI is a popular framework for this layer. It wraps a model call in a Guard object with validators for toxic language, PII, and prompt injection. Each validator takes a threshold and a failure action, such as raising an exception. The pattern is simple: define the checks, attach them to the call, and let the guard reject or fix output that fails.
False positives are a real cost. A safety classifier with a 4 percent false positive rate blocks 40,000 benign requests a day on a chatbot with one million daily messages. Tune thresholds against real traffic, not against a test set. Log every block with the reason, so you can tell a real attack from a classifier mistake.
Tool-call gating and least privilege
The most dangerous injection is one that triggers an action. An agent that can send email, move money, or delete records must never take those actions on a model's say-so alone. Three controls apply:
- Least privilege. Give each tool the minimum permissions it needs. A read-only lookup tool should not be able to write.
- Allow-lists. Define the tools an agent may call. Enforce the list in code, so a call to a tool outside the list never runs.
- Human approval. Route high-risk actions to a person. A payment, a deletion, or an external message waits for approval.
Rate limits and quotas sit alongside these controls. OWASP lists them under LLM10, unbounded consumption. Limit requests per user, set timeouts, and cap queued actions. A runaway agent that loops on tool calls burns money and can denial-of-service your own systems.
How to build the stack: six steps
- Map your risks. List what your app does, what data it touches, and what actions it can take. Match each risk to an OWASP entry.
- Add input rails first. Install a classifier and pattern checks on user input. Add a retrieval rail if the app reads documents or web content.
- Harden the system prompt. Define the role, mark untrusted content, and remove every secret from the prompt.
- Add output rails. Redact PII, score toxicity, and validate formats with deterministic code before any reply reaches a user or a downstream system.
- Gate the tools. Allow-list tools, enforce least privilege in code, and require human approval for irreversible actions.
- Test like an attacker. Run red-team tools such as Garak, PyRIT, or promptfoo against the app before every release. Automate known attacks as a regression suite, and add human testers for creative ones.
Logging ties the stack together. Write every decision, allowed or blocked, to an append-only audit log. You need it for incident review, and for frameworks such as the EU AI Act, NIST AI RMF, and ISO 42001.
Bottom line
Guardrails for LLM apps work as a layered stack, not as a single filter. Input rails and prompt hardening catch most injection attempts. Output rails catch leaks and toxic replies. Tool gating limits the damage when an attack succeeds. No layer is foolproof, and OWASP says so directly. Build the stack, test it with real attacks, and design every layer as if the one before it failed.