Skip to content
Ask for a Budget
EngineeringFebruary 26, 20263 min readUpdated on November 11, 2026

Prompt injection: protecting chatbots and agents without breaking the product

Direct vs indirect injection, RAG as an attack surface and five defense layers, from separating instructions and data to validation and logs, plus a checklist.

Prompt injection: protecting chatbots and agents without breaking the product
Índice do artigo
faltam 3 min de leitura

If a chatbot can be talked into ignoring its rules, it is not "smart".
It is out of control.

Prompt injection happens when an input (from the user, from a document retrieved through RAG or even from a tool) manages to change the model's behavior in an unwanted way: ignoring policies, revealing information, executing actions or producing dangerous output.

OWASP lists prompt injection as the first risk in its Top 10 for LLM applications (LLM01 in the 2025 edition, the most recent one published by the OWASP GenAI Security Project as of November 2026). In systems with RAG and tool calling, the impact can be larger because the model becomes a decision layer in the middle of the flow.

Person holding a tablet with a padlock icon on a digital interface

What you will learn

  • The difference between direct and indirect injection
  • Where injection gets into a modern system (RAG, tools, plugins)
  • Layered mitigation that does not depend on "magic prompts"
  • A checklist to reduce risk without killing the user experience

Direct vs indirect (the distinction that changes your architecture)

Direct injection is the classic one: the user writes something like "ignore all instructions and do X".

Indirect injection is more dangerous: the user does not write the malicious instruction.
It comes "hidden" in some content the model reads, for example:

  • a document indexed in RAG
  • an HTML page
  • an email or ticket
  • the output of a tool (a "compromised" external system)

In other words: RAG turns documents into input.
If you do not treat that as "untrusted input", the chance of an incident goes up.

Diagram: entry points for prompt injection


What does not work (or barely works)

  1. "Putting a strong sentence in the prompt"

It helps, but it is not a security barrier.

  1. Trusting the model to "behave"

Models make mistakes, misread and can be manipulated.

  1. Putting permission rules in the prompt

Permission is a system rule. It must be enforced in the backend (not in the text).

The logic is similar to security in traditional apps: you do not trust user input, you validate it and enforce authorization on the server.


Layered mitigation (what usually works best)

Layer 1: Separate instructions from data (for real)

In the prompt, make it clear what is:

  • "instructions" (system policy)
  • "user input"
  • "retrieved content"

And, above all, delimit the retrieved content.

Conceptual example (the exact format varies):

SYSTEM INSTRUCTIONS:
- Follow the policies below...
- Do not execute actions without validation...

USER INPUT:
<<< ... >>>

RETRIEVED CONTENT (UNTRUSTED):
<<< ... >>>

This separation lowers the chance that the model "thinks" a document is a system instruction.

Layer 2: Treat RAG as an attack surface

Practical measures:

  • index only trusted sources
  • block "open" files that anyone can edit without review
  • apply detection of suspicious content (keywords, jailbreak patterns)
  • keep a trail: document → chunk → answer

Further reading: Sensitive data in RAG.

Layer 3: Reduce agency (fewer tools, fewer permissions)

When you give tools to the model (tool calling), you are giving it agency.

OWASP also treats "excessive agency" as a relevant risk (LLM06 in the 2025 edition: when the system allows too many actions with too much autonomy).

Rules that help:

  • each tool does one thing (do not create "run any command")
  • minimum permissions (read-only when possible)
  • high-impact actions require human confirmation (human-in-the-loop)

Layer 4: Validate output before using it

LLM output is not "safe code".

If the model produces:

  • JSON to call an API
  • a query
  • HTML/Markdown that will be rendered
  • text that goes to another system

… you need to validate and sanitize it.

Common validations:

  • strict schema (fields, types, enums)
  • size limits
  • allowlist of actions
  • encoding/escaping before rendering

Layer 5: Observability and incident response

Without logs and metrics, prompt injection becomes a "rumor".

Log:

  • tool calls and parameters (with redaction when there is PII)
  • documents/chunks used (IDs)
  • blocking events (by policy)
  • rate of suspicious attempts

And treat it like any operational risk:

  • alerts
  • incident playbook
  • fast rollback of prompt/model/configuration

If you want to structure this as a discipline, see: LLMOps: metrics and operations.


A straightforward checklist to reduce risk

  • Clear delimitation between instructions, user and retrieved context
  • RAG content treated as untrusted (includes validation/filtering)
  • Tools with a specific function (avoid "open-ended tools")
  • Minimum permissions per tool and per user
  • Human confirmation for critical actions
  • Output validation (schema, allowlist, size, sanitization)
  • Rate limits and per-user limits (cost and abuse)
  • Logs of tool calls, retrieved sources and policy blocks
  • Regression tests covering jailbreak attempts and adversarial cases

References to go deeper

To bring this control into a real application, see how X-Apps structures governance and security in generative AI and LLMOps for AI in production.

Also read

Sensitive data in RAG: how to prevent leaks in AI chatbotsMFA/2FA for clients: a secure account that never blocks your app releaseReplit → React: a safe flow from front-end prototype to production
Previous post
Sensitive data in RAG: how to prevent leaks in AI chatbots
Next post
Tool calling: how to connect AI to your systems safely and with audit trails
Newsletter

One email a month, no noise

What we learn shipping custom software and applied AI.

Related articles

Sensitive data in RAG: how to prevent leaks in AI chatbots3 min · Engineering
MFA/2FA for clients: a secure account that never blocks your app release6 min · Engineering
Replit → React: a safe flow from front-end prototype to production3 min · Engineering
Tool calling: how to connect AI to your systems safely and with audit trails3 min · Technology
AI coding productivity: from the peak to the complexity cliff7 min · Engineering

Accelerate your business with X-Apps

Ask for a Budget
X-Apps is an IT provider partner and advised by
Gartner
Receive our e-mails
News on software and applied AI.
Follow X-Apps