How to Defend LLM Applications Against Prompt Injection
AI securityprompt injectionAI agentsLLM safety

How to Defend LLM Applications Against Prompt Injection

FFuzzyPoint Editorial Team
2026-12-01
7 min read

A practical checklist for defending LLM apps against direct and indirect prompt injection through boundaries, permissions, validation, and approvals.

Prompt injection cannot be solved by adding one more instruction to a system prompt. This practical checklist shows how to protect an LLM application by treating user messages, retrieved documents, web pages, files, and tool results as untrusted input; limiting what the model can do; validating outputs; and requiring human approval for consequential actions.

Overview

Prompt injection is an attempt to influence an AI system by placing instructions in content the application processes. A direct attack might tell a chatbot to ignore its rules. An indirect injection hides instructions inside a document, web page, email, support ticket, or search result that the application retrieves and passes to the model.

The risk becomes greater when an LLM is connected to tools. A model that only drafts text may produce an unsafe response. A model that can send email, update a record, call an API, execute code, or access private documents may turn the same manipulation into a security or operational incident.

The objective of prompt-injection defense is therefore not to make the model perfectly obedient. It is to design a system in which untrusted text has limited authority and a model mistake does not automatically become a sensitive action. This is a core concern in secure LLM applications, RAG systems, and AI workflow automation.

Start with a threat model. List every place content enters the application, every instruction the model receives, every tool it can call, and every data source it can access. Then ask what could happen if an attacker controlled one of those inputs. The result should guide technical controls, testing, and approval requirements.

For background on separating application behavior from prompt wording, see How to Write Reliable System Prompts for AI Assistants. If your application uses retrieval, pair this checklist with the architecture guidance in RAG Application Architecture.

Checklist by scenario

1. Direct user prompts

  • Define which user requests are in scope and which require refusal, escalation, or a narrower workflow.
  • Keep system and developer instructions separate from user content in the application design. Do not rely on visual formatting alone to establish trust.
  • Tell the model how to treat user-provided instructions: as task input, not as permission to change system policy or security boundaries.
  • Do not place secrets, private keys, internal prompts, or unrestricted credentials in the model context.
  • Test common override language, role-play requests, encoded text, conflicting instructions, and attempts to extract hidden context.

2. Retrieved documents and RAG

  • Label retrieved passages as reference material rather than executable instructions.
  • Preserve document identity and provenance so the application can show which source influenced an answer.
  • Apply access control before retrieval. Do not retrieve a document and expect the model to decide whether the current user is allowed to see it.
  • Filter or quarantine imported content that contains suspicious instructions, links, scripts, or requests for secrets.
  • Limit the amount of retrieved text and the number of retrieval steps. Larger context does not automatically create safer reasoning.
  • Evaluate retrieval separately from generation. A correct answer can still be unsafe if the system selected an unauthorized or poisoned source.

3. Web browsing, email, and uploaded files

  • Assume external pages, messages, PDFs, spreadsheets, images, and metadata may be hostile or misleading.
  • Separate content extraction from action selection. First parse or summarize the material; do not let an extracted instruction directly trigger a tool.
  • Restrict outbound network access and prevent the model from freely following links to internal services or unrelated domains.
  • Scan attachments and convert them into a controlled representation where practical. Treat hidden text, comments, alt text, and document metadata as untrusted.
  • Require the application to identify the source of a proposed action and retain an audit record.

4. Tool-using agents

  • Give each tool the minimum permissions required for its task. Prefer read-only access when writing is not essential.
  • Use allowlists for destinations, operations, file paths, database tables, and API methods.
  • Validate tool arguments in application code, not only in the prompt. Check types, ranges, ownership, authorization, and business rules.
  • Separate planning from execution. A model may propose an action, while a deterministic service decides whether it is permitted.
  • Place email sending, record deletion, payments, permission changes, code deployment, and other consequential operations behind explicit human approval.
  • Set limits for tool-call count, runtime, spend, recursion, and returned data so a compromised loop has a bounded impact.

5. Data leakage and output handling

  • Classify sensitive data before it enters the model context and redact or tokenize data that is not needed for the task.
  • Check generated output for secrets, personal data, unauthorized records, unsafe URLs, and instructions that downstream systems might execute.
  • Use structured output schemas and reject responses that fail validation. A valid JSON shape does not prove that the values are safe.
  • Escape model output before displaying it in HTML, SQL, shell commands, logs, or other interpreters.
  • Log security-relevant events without copying sensitive prompts or responses into broadly accessible logs.
Attack patternPrimary mitigationAdditional control
User says to ignore prior instructionsInstruction hierarchy and narrow task scopeAdversarial evaluation and refusal tests
Retrieved document contains hidden commandsMark retrieval as untrusted reference dataSource filtering, provenance, and retrieval access control
Tool arguments contain an unsafe destinationApplication-side allowlists and validationRead-only defaults and human approval
Model attempts to reveal private contextMinimize secrets in contextOutput scanning, redaction, and access controls
Agent repeats calls or follows an attack chainCall, time, and permission limitsMonitoring, circuit breakers, and manual shutdown

What to double-check

Review the trust boundaries in a diagram. Mark user input, retrieved content, tool results, system instructions, application code, databases, and external services. For each boundary, record who controls the data, whether it can contain instructions, and what validation occurs before the data crosses into another component.

Then review the permissions independently of the prompt. Ask whether the agent can access more data than the user, whether a read operation can become a write operation, and whether a single model response can combine several low-risk tools into a high-impact action. Prompt wording should support these controls, not replace them.

Check failure behavior. If the model returns malformed JSON, an unknown tool name, an unapproved URL, or an ambiguous request, the safe default should be to stop or request clarification. Test timeouts, provider errors, empty retrieval results, stale permissions, duplicate requests, and partial tool failures.

Create a repeatable red-team test case for each important workflow:

  1. Workflow: identify the assistant, data sources, tools, and intended user.
  2. Attacker control: state whether the attacker controls a message, document, web page, file, tool result, or account.
  3. Payload: record the injection text or content pattern, including obfuscation if relevant.
  4. Expected safe behavior: specify the refusal, sanitization, approval request, or blocked tool call.
  5. Observed result: capture the model output, tool arguments, permissions used, and audit events.
  6. Disposition: assign a severity, remediation owner, regression-test identifier, and retest date.

Keep model evaluation and application security evaluation distinct. Prompt optimization can improve consistency, but it cannot authorize a database query or enforce a user’s permissions. For a broader testing process, see Prompt Engineering Evaluation.

Common mistakes

  • Relying on a single system prompt: A well-written prompt helps establish behavior but is not an access-control mechanism.
  • Assuming retrieved text is trustworthy: Search results and documents can contain instructions that were never reviewed by the application owner.
  • Giving agents broad credentials: A model should not inherit an operator’s unrestricted access simply because that is convenient to implement.
  • Validating syntax but not meaning: A tool call can be valid JSON and still target the wrong account, record, environment, or recipient.
  • Skipping output encoding: Text that is safe in a chat window may be dangerous when inserted into HTML, SQL, a shell, or another automated workflow.
  • Testing only obvious jailbreaks: Indirect prompt injection may arrive through routine business content, including support tickets, documents, and web pages.
  • Ignoring observability: Without tool-call logs, source provenance, and approval records, investigating an incident becomes much harder.
  • Assuming a model update fixes the architecture: Provider safeguards can change behavior, but application-side permissions and validation remain necessary.

When to revisit

Review this prompt-injection defense checklist before launching a new LLM feature, adding a tool, connecting a new data source, changing retrieval behavior, or allowing the system to take an external action. Revisit it during security audits and before major workflow or seasonal planning cycles when automation volume, users, or business rules may change.

Also review the controls whenever the model provider, agent framework, authentication design, or deployment environment changes. New safety features may improve detection or isolation, but they should be treated as an additional layer until your own tests confirm how they behave in the exact workflow.

Your practical next step is to choose one production workflow and complete this short sign-off:

  1. List every untrusted input and mark where it enters the context.
  2. List every tool, credential, data source, and irreversible action.
  3. Reduce permissions and add deterministic validation around each tool.
  4. Add at least one direct-injection and one indirect-injection test.
  5. Require approval for consequential actions and define a safe failure path.
  6. Record the test results and schedule the next review after the next workflow or tool change.

That process provides a more durable foundation for AI agent security than trying to discover a perfect defensive prompt. In secure LLM application design, the model should help interpret a task, while the surrounding application decides what is trusted, what is permitted, and what must wait for a person.

Related Topics

#AI security#prompt injection#AI agents#LLM safety
F

FuzzyPoint Editorial Team

AI Development 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.