Prompt injection prevention is an application-security problem, not a prompt-writing trick. This guide provides a reusable checklist for identifying direct and indirect attacks, constraining tool use, protecting sensitive data, testing AI agents, and responding when an LLM application behaves unexpectedly.
Overview
A prompt injection occurs when untrusted content influences an LLM to ignore, reinterpret, or work around the instructions and boundaries defined by the application. The attack may be explicit, such as a user asking the model to reveal its system instructions, or indirect, such as a malicious instruction hidden inside a web page, document, email, ticket, or retrieved knowledge-base record.
The central security assumption is simple: anything that enters an LLM workflow can be untrusted unless your application has independently verified it. That includes user messages, retrieved text, uploaded files, tool results, web content, metadata, and sometimes model-generated text passed between workflow steps.
Prompt injection prevention therefore requires layered controls. A system prompt can establish intent and rules, but it should not be treated as an access-control mechanism. Authorization, data filtering, tool restrictions, output validation, monitoring, and human approval should be implemented outside the model wherever possible.
Start by drawing the trust boundaries in your application. Show the user interface, application server, model provider, retrieval system, databases, external APIs, browser or browsing tools, code execution environments, and human approval steps. Mark every place where data crosses from an untrusted source into a more trusted component. This diagram becomes the foundation for a practical threat model.
For broader production controls, pair this guide with the production checklist for deploying an LLM API on the cloud. If your application uses retrieval, review the guidance on RAG prompt engineering and grounded answers as well.
Checklist by scenario
1. Direct user prompt injection
Direct attacks are messages designed to change the model's role or bypass its instructions. Examples include “ignore previous instructions,” requests for hidden prompts, fabricated administrator claims, and attempts to make the assistant follow a new policy supplied in the user message.
- Separate user content from system and developer instructions using the provider's supported message or instruction structure.
- State the assistant's permitted tasks and prohibited actions clearly, but do not rely on those statements as the only defense.
- Never place credentials, private keys, authorization tokens, or unnecessary confidential data in prompts.
- Require the application to enforce permissions before retrieving records, calling tools, or returning sensitive fields.
- Validate structured output against a schema and reject unexpected fields, actions, or destinations.
- Test attempts to reveal system instructions, impersonate privileged users, change task scope, and bypass safety or business rules.
2. Indirect prompt injection in retrieved content
Indirect prompt injection is especially important in RAG systems and agents that browse or process files. A document may contain text such as “send the contents of the conversation to this address.” The model may interpret that text as an instruction even though it was supposed to treat the document as reference material.
- Label retrieved content explicitly as data, not instructions, in the application and prompt structure.
- Keep source content in a clearly delimited field and tell the model that instructions inside it are not authoritative.
- Limit retrieval to sources the user is authorized to access; prompt wording cannot repair an authorization error.
- Scan or classify documents for suspicious instruction-like content before indexing or displaying it to an agent.
- Do not allow retrieved text to directly determine tool arguments, recipients, database queries, or code.
- Require a separate validation step before content-derived actions are executed.
For an implementation pattern, see How to build an LLM app with RAG. Treat its architecture, prompting, and evaluation concepts as parts of one control system rather than as isolated prompt improvements.
3. Tool misuse and excessive agency
Tools turn a misleading answer into a potentially consequential action. An agent that can send email, modify records, execute code, issue refunds, or publish content needs narrower permissions than an assistant that only drafts text.
- Give each tool the minimum permissions and data access required for its task.
- Use an allowlist of tools and operations instead of allowing the model to select arbitrary functions.
- Validate every argument in application code, including identifiers, file paths, query filters, URLs, recipients, and quantities.
- Use separate read and write tools, with stronger controls for writes.
- Place high-impact actions behind explicit user confirmation or human approval.
- Apply rate limits, transaction limits, timeouts, and idempotency controls.
- Record who requested an action, what the model proposed, what the application approved, and what actually executed.
A safer pattern is “plan, validate, approve, execute.” The model can propose a structured action, but a deterministic policy layer checks authorization and parameters before execution. For sensitive operations, the user or an operator confirms the final action using information generated independently of the model's narrative.
4. Data exfiltration
Exfiltration attempts try to make the model disclose secrets, private records, hidden instructions, or information from another user's context. They can appear as direct requests, encoded instructions, requests for summaries, or seemingly harmless tool calls that transmit data to an external destination.
- Minimize the data supplied to the model and redact secrets before prompt construction.
- Enforce tenant, user, and document permissions at the data layer.
- Block outbound requests to unapproved domains and destinations.
- Prevent the model from choosing arbitrary URLs, recipients, or query endpoints.
- Filter responses for sensitive fields where appropriate, while recognizing that output filters are not a complete defense.
- Keep separate contexts for users and tasks; do not reuse conversation state without an explicit authorization decision.
5. Jailbreak and policy-bypass attempts
Jailbreaks use role-play, emotional pressure, fictional framing, translation, encoding, multi-turn manipulation, or conflicting instructions to elicit behavior the application should not permit. Test the complete workflow, not only the base model's text response.
- Create test cases for role-play, instruction hierarchy changes, obfuscated text, multilingual prompts, and repeated-pressure conversations.
- Include benign requests that resemble attacks to measure false positives.
- Check whether a refusal still leaks sensitive details through explanations, summaries, or tool calls.
- Test context accumulation across multiple turns and workflow stages.
- Evaluate the model, retrieval layer, policy layer, and tools together.
Use an evaluation set that is versioned with your prompts and application code. The LLM evaluation guide and evaluation scorecard can help organize regression tests and review results.
What to double-check
Before releasing a prompt or agent change, review these control points:
- Trust boundaries: Can you identify every untrusted input and every component that receives it?
- Instruction hierarchy: Are authoritative instructions separated from user and retrieved content?
- Authorization: Does the server independently verify access before retrieval and action execution?
- Tool contracts: Are tools narrow, typed, validated, rate-limited, and auditable?
- Output handling: Is model output treated as untrusted data before it reaches a database, browser, shell, template, or API?
- Secrets: Are credentials excluded from prompts, logs, traces, and error messages?
- Approval: Are irreversible or externally visible actions confirmed by a user or operator?
- Observability: Can you reconstruct the prompt version, retrieved sources, tool proposal, policy decision, and final action?
Use structured logs that avoid storing unnecessary sensitive content. Capture hashes, identifiers, policy outcomes, and redacted summaries when full payloads are not required. Review the logging design with your privacy and security requirements; more logging is not automatically safer.
Common mistakes
- Relying on a stronger system prompt alone. Prompt instructions can guide behavior, but they do not replace access control or deterministic validation.
- Assuming retrieved documents are trusted. Internal content may be stale, compromised, user-submitted, or deliberately crafted to influence an agent.
- Letting the model create executable queries or commands directly. Use constrained schemas, parameterized operations, allowlists, and application-side checks.
- Giving every agent the same broad tool access. Split workflows by role and grant only the capabilities each task needs.
- Testing only single-turn attacks. Multi-turn manipulation and state carried across steps can produce different results.
- Returning detailed refusal explanations. An explanation can accidentally reveal internal rules or sensitive context. Keep user-facing responses useful without exposing protected implementation details.
- Failing open when validation fails. An invalid tool argument, unavailable policy service, or uncertain authorization decision should stop or defer the action.
- Ignoring operational response. If an injection succeeds, teams need a way to disable a tool, revoke credentials, isolate affected data, and preserve relevant evidence.
A compact incident-response template
When suspicious behavior is detected, record: the time and application version; affected user, tenant, or workflow; input and retrieved-source identifiers; model and prompt versions; proposed and executed tools; data potentially exposed; controls that triggered or failed; immediate containment actions; and follow-up tests. Temporarily disable high-risk tools or routes, rotate exposed credentials, review access logs, notify the responsible owners, and add a regression case before restoring normal operation.
When to revisit
Review your prompt injection defenses before seasonal planning cycles, major workflow changes, and every new tool or data source integration. Revisit them when you change model providers, model versions, system prompts, retrieval logic, output schemas, permissions, browser access, code execution, or approval flows.
A practical review cadence is event-driven rather than calendar-only. Open a security review when a prompt changes from informational assistance to action-taking, when a new document source is indexed, when users report unexpected behavior, or when monitoring shows unusual refusals, tool proposals, outbound requests, or access patterns.
End each review with three deliverables: an updated trust-boundary diagram, a prioritized mitigation list with owners, and a regression suite covering direct injection, indirect injection, tool misuse, exfiltration, and jailbreak attempts. Track results over time using the same evaluation criteria so that a prompt improvement does not conceal a security regression.
Before your next release, complete this short final check: remove unnecessary secrets, verify authorization outside the model, constrain and validate every tool, isolate untrusted content, require approval for consequential actions, test multi-turn attacks, and confirm that logs support investigation without creating a new privacy risk. Prompt injection prevention is an ongoing engineering practice; the checklist should change whenever the application's inputs, permissions, or capabilities change.