Prompt Injection Prevention Guide: Patterns for Safer LLM Applications
prompt securityLLM securityAI agentsRAG securityapplication security

Prompt Injection Prevention Guide: Patterns for Safer LLM Applications

DDescribe.cloud Editorial Team
2026-11-14
6 min read

A practical checklist for preventing prompt injection in RAG systems, AI agents, and LLM workflows through isolation, access control, and testing.

Prompt injection prevention requires more than a stronger system prompt. This guide provides a reusable threat-modeling worksheet, scenario-based checklist, and release-review process for separating trusted instructions from untrusted content in RAG systems, tool-using agents, and LLM-powered workflows.

Overview

Prompt injection attacks occur when content supplied to an LLM influences the model to ignore, reinterpret, or override the instructions that should govern an application. The content may be direct user input, a retrieved document, a web page, an email, a code comment, or data returned by another tool. The risk becomes more serious when the model can call tools, access private information, change records, or trigger external actions.

A useful security assumption is that every input not created and controlled by the application is untrusted. Delimiters, XML tags, role labels, and phrases such as “ignore previous instructions” can help organize context, but they are not a security boundary. LLM security best practices therefore combine prompt design with authorization, data isolation, tool restrictions, validation, monitoring, and human review.

Before reviewing a workflow, document four boundaries:

  • Trusted instructions: application rules, developer-controlled configuration, and approved task definitions.
  • Untrusted content: user messages, retrieved text, uploaded files, web content, emails, and third-party API responses.
  • Sensitive resources: secrets, personal data, internal documents, databases, and privileged APIs.
  • Permitted actions: what the model may read, create, modify, send, or execute.

For a broader testing process, pair this guide with the Prompt Injection Testing Checklist and the LLM Evaluation Checklist. Prompt testing should measure both task quality and the system’s behavior when inputs conflict with its intended instructions.

Checklist by scenario

1. Direct user prompts

Direct injection is visible in the user’s message. A user may ask the assistant to reveal hidden instructions, bypass a workflow rule, or perform an action outside the stated task.

  • Define which requests are allowed, refused, or routed to a human.
  • Keep authorization decisions outside the model wherever possible.
  • Do not place secrets, API keys, or privileged business rules in text the model can reproduce.
  • Validate structured output against a schema before using it in application logic.
  • Require confirmation for consequential actions such as sending messages, changing records, or publishing content.
  • Log the request, decision, tool calls, and final result without unnecessarily storing sensitive content.

In a retrieval-augmented generation workflow, an attacker can place instructions inside a document that later appears relevant to a user’s question. The model may treat the retrieved text as an instruction rather than evidence.

  • Label retrieved passages as reference material, not as commands.
  • Store document identity, source, tenant, permissions, and retrieval time with each passage.
  • Apply access controls before retrieval, not only after the answer is generated.
  • Use separate fields for document text, metadata, and application instructions.
  • Limit retrieval to the minimum sources needed for the task.
  • Check whether an answer is supported by permitted sources before displaying it.

The RAG Workflow Guide can help structure ingestion, retrieval, citation, and answer validation as separate stages.

3. Tool-using agents

Agent security depends primarily on the permissions granted to tools. A model that can read a database, execute code, or send email should be treated as an untrusted decision-maker operating inside a controlled environment.

  • Give each tool a narrow purpose and a strict input schema.
  • Use separate credentials and least-privilege permissions for each integration.
  • Block arbitrary URLs, shell commands, file paths, and database statements unless they are explicitly required and constrained.
  • Require an independent application check before high-impact tool calls.
  • Set time, token, result-size, and call-count limits to contain loops and unexpected costs.
  • Return only the minimum tool result needed for the next step.

For every tool, record its owner, permitted inputs, side effects, credentials, failure behavior, and approval requirement. This turns an informal agent workflow into an auditable application workflow.

4. Imported files, email, and web content

Indirect prompt injection is especially difficult because the attack text may never appear in the user’s message. It can arrive through a PDF, spreadsheet, support ticket, repository, webpage, or email that the application summarizes or analyzes.

  • Treat imported content as hostile input, even when it comes from a normally trusted source.
  • Scan files for unexpected links, embedded instructions, macros, and unusual content before processing.
  • Use a restricted fetcher for web content rather than giving the model unrestricted browsing.
  • Prevent retrieved content from selecting tools, changing system configuration, or redefining the task.
  • Keep file-processing environments isolated from production credentials and networks.
  • Show users which sources influenced an answer when that context is important to decision-making.

What to double-check

Use this short threat-modeling worksheet before a release or workflow change:

  1. What can enter the context? List every user field, retrieved source, tool response, file type, and external feed.
  2. Which content is trusted? Mark the exact fields controlled by the application. Do not classify content as trusted merely because it has been retrieved from a database.
  3. What could be exposed? Identify secrets, personal information, tenant data, internal instructions, and hidden tool results.
  4. What can the model cause? Enumerate read, write, send, execute, and publish capabilities.
  5. Where is authorization enforced? Confirm that permissions are checked by application code or the relevant service, rather than inferred from model output.
  6. What happens on ambiguity? Define a safe refusal, escalation, or confirmation path.
  7. How will you detect abuse? Choose logs and alerts for unusual tool sequences, repeated refusals, sensitive-data requests, and unexpected output formats.

Review the system prompt as part of the design, but do not rely on prompt wording as the primary defense. Clear instructions, explicit output formats, and examples improve reliability; they do not replace isolation and access control. A versioned prompt library, such as the approach described in How to Build a Reusable Prompt Template Library, makes changes easier to compare and test.

Common mistakes

  • Relying on “ignore instructions” detection: Attackers can use indirect, encoded, or ordinary-looking language. Pattern matching can be one signal, not the complete defense.
  • Assuming delimiters create isolation: Delimiters improve readability but do not prevent the model from following text inside them.
  • Giving agents broad permissions: A capable model with unrestricted tools turns a prompt failure into an application-level incident.
  • Mixing data and commands: Separate instructions, user content, retrieved evidence, and tool output in both the application data model and the prompt.
  • Skipping negative tests: Test conflicting instructions, malicious documents, irrelevant retrievals, malformed tool arguments, and partial outages.
  • Logging too little or too much: Without useful event records, investigation is difficult; without redaction and retention rules, logs can become a new source of exposure.
  • Treating a refusal as proof of safety: A model may refuse one visible attack while still leaking information through a summary, tool call, or later conversation turn.

When to revisit

Revisit this checklist before each security release, after changing a system prompt or model, when adding a tool or data source, and whenever a RAG pipeline changes its ingestion or retrieval rules. It is also worth reviewing before seasonal planning cycles if your organization imports new documents, opens a new tenant, changes approval responsibilities, or increases automation.

Use a practical update trigger list:

  • A new model, provider, SDK, parser, browser, connector, or agent framework is introduced.
  • The workflow receives a new type of file, webpage, email, or third-party response.
  • A tool gains write access, broader records access, or the ability to contact external systems.
  • Users report unexpected answers, unexplained tool calls, data leakage, or repeated refusals.
  • Prompt, retrieval, authorization, or logging code changes.

Finish each review by assigning an owner, recording the tested workflow version, preserving representative attack cases, and tracking unresolved risks. Review related operational controls in the LLM API Error Handling Guide and document prompts, inputs, outputs, and owners using the AI Workflow Documentation Template. Prompt injection prevention is not a one-time prompt optimization task; it is a recurring part of designing, testing, and operating safer AI development workflows.

Related Topics

#prompt security#LLM security#AI agents#RAG security#application security
D

Describe.cloud Editorial Team

AI Development Workflows Editor

Senior editor and content strategist. Writing about technology, design, and the future of digital media. Follow along for deep dives into the industry's moving parts.