Prompt injection defense is a design and testing problem, not a single prompt-writing trick. This guide gives teams a reusable checklist for protecting chatbots, retrieval-augmented generation (RAG) systems, and tool-using agents from instructions hidden in user input or external content.
Overview
A prompt injection occurs when text supplied to an LLM changes, competes with, or attempts to override the instructions the application intended the model to follow. The text may come directly from a user, or indirectly from a web page, uploaded document, email, database record, search result, or tool response.
Direct injection is visible in the user’s message. For example, a user may ask a support chatbot to ignore its rules and reveal internal instructions. Indirect injection is harder to notice: a retrieved document might contain text telling the model to disclose confidential context, call a tool, or disregard the user’s original task. In both cases, the model receives data that looks like language and may struggle to distinguish trusted instructions from untrusted content.
The practical goal is not to make an LLM perfectly resistant to every malicious phrase. The goal is to limit what an injection can change and what the model can do if it succeeds. Treat the model as one component in a larger security boundary. Access control, tool permissions, data filtering, output validation, logging, and human approval should carry responsibilities that should not depend on prompt wording alone.
Before reviewing a prompt, map the application’s data and action paths. Identify which inputs are trusted, which are user-controlled, which are retrieved from external sources, and which tools can create side effects. Teams building a RAG system can pair this review with the RAG application architecture tutorial, while teams preparing a release can use the AI feature launch checklist as a broader delivery reference.
Checklist by scenario
1. User-facing chatbots
For a general chatbot, assume users can deliberately test boundaries. Review the following before release:
- Define the assistant’s permitted tasks in plain language, including tasks it must refuse or redirect.
- Keep secrets, credentials, private system instructions, and sensitive customer records out of the model context whenever possible.
- Separate authentication and authorization from the conversation. A user’s claim in chat should never grant access.
- Place rate limits and abuse monitoring outside the prompt so they cannot be disabled by model output.
- Test requests to reveal hidden instructions, impersonate administrators, bypass content boundaries, or force the model to produce unrestricted output.
- Validate generated actions and structured fields before they reach downstream systems.
A useful prompt pattern is to describe the assistant’s role and decision rules, then state that user messages are requests to assess rather than higher-priority instructions. This can improve consistency, but it should be treated as a layer of defense rather than an authorization mechanism.
2. RAG applications
RAG systems introduce an indirect injection surface because retrieved content may contain adversarial or simply irrelevant instructions. Apply this checklist:
- Track the origin, owner, timestamp, and access scope of each document or record entering retrieval.
- Apply document-level permissions before content is added to the context, not after the answer is generated.
- Label retrieved text as reference material and clearly distinguish it from the application’s instructions.
- Instruct the model to extract relevant facts from documents rather than follow commands found inside them.
- Limit retrieval to the minimum sources needed for the task and avoid passing unnecessary metadata or hidden fields.
- Scan outputs for requests to disclose unrelated context, secrets, personal data, or internal system details.
- Test documents containing phrases such as “ignore previous instructions,” fake system messages, encoded text, and instructions aimed at tools.
Retrieval quality and security are connected. A document that is technically relevant but untrusted may still be unsafe to place beside high-priority instructions. For a deeper implementation plan, see the step-by-step RAG architecture guide.
3. Tool-using agents
Agents require the strictest controls because a successful injection may lead to an external action. Review each tool as if the model were an untrusted operator:
- Give each tool the narrowest possible permissions and separate read operations from write or delete operations.
- Use explicit schemas, type checks, range limits, and allowlists for tool arguments.
- Require confirmation for irreversible, financial, privileged, or externally visible actions.
- Keep credentials in the tool layer, not in prompts, retrieved documents, or model-visible instructions.
- Prevent a tool response from automatically becoming an instruction to call another sensitive tool.
- Set action budgets, timeouts, recursion limits, and per-task destinations outside the model.
- Record the user request, selected tool, validated arguments, approval state, and result for investigation.
Do not assume that a model’s refusal to call a dangerous tool is sufficient protection. The application should reject unsafe arguments even when the model produces a plausible explanation or appears confident.
4. Document and email workflows
Uploaded files and inbound messages should be treated as untrusted input. Before allowing an LLM to summarize, classify, or route them:
- Define whether the content is merely summarized or can influence a workflow decision.
- Strip or isolate content that could be interpreted as application instructions.
- Use separate fields for document text, user instructions, and system policy in the request sent to the model.
- Require a deterministic rule or human review before sending messages, changing records, or approving transactions.
- Test attachments with visible instructions, hidden text, unusual formatting, long context, and conflicting requests.
What to double-check
Use this threat-model worksheet during design and security review:
| Question | What to record |
|---|---|
| What can the model see? | User messages, retrieved documents, tool results, conversation history, metadata, and system instructions. |
| Which inputs are untrusted? | Users, public web pages, uploaded files, external APIs, third-party records, and generated text from another model. |
| What can the model influence? | Text shown to a user, classifications, database queries, tool arguments, notifications, or external actions. |
| What is the worst credible outcome? | Data disclosure, unauthorized access, destructive changes, misleading output, excessive cost, or workflow disruption. |
| What control stops that outcome? | Authorization, validation, isolation, approval, rate limiting, monitoring, or a safe failure path. |
Then double-check the boundaries between components. A prompt may say “never reveal private information,” while a retrieval service passes private records into the context for a user who is not authorized to see them. A model may be told to use a calendar tool only for scheduling, while the tool itself accepts arbitrary URLs or unrestricted account identifiers. Security depends on the enforceable boundary, not only on the instruction.
Build a small regression set for prompt injection testing. Include direct override attempts, role-play and impersonation, fake system messages, instructions embedded in retrieved content, encoded or obfuscated commands, conflicting documents, attempts to extract secrets, and requests to trigger tools. For each case, define the expected behavior: answer safely, refuse, ask for clarification, omit the unsafe content, or request human approval.
Run these cases after changing the system prompt, model, retrieval settings, tool schema, document parser, or access-control logic. An evaluation workflow should measure more than whether the model says “no.” Check whether it disclosed context, selected an unsafe tool, produced invalid arguments, or created an unreviewed side effect. The LLM evaluation framework guide can help structure broader accuracy, safety, and reliability testing.
Common mistakes
- Relying on a stronger prompt alone. Clear production prompt design helps, but it cannot replace authorization, validation, and tool isolation. Use the production prompt template library as a starting point, then enforce critical rules in code.
- Treating retrieved text as trusted because it came from an internal system. Internal sources can be stale, misconfigured, user-editable, or compromised. Track provenance and apply permissions.
- Giving an agent broad permissions for convenience. Start with read-only, narrowly scoped tools. Expand access only when a specific use case requires it.
- Testing only obvious attack phrases. Include polite requests, indirect wording, long context, multilingual input, formatting tricks, and instructions hidden in realistic business content.
- Logging too little or too much. Without useful traces, investigations become difficult. At the same time, logs should not casually store secrets or sensitive user content. Review the LLM logging and privacy checklist before enabling detailed tracing.
- Confusing a refusal with a secure result. A response can refuse the visible request while still leaking fragments of context or making an unsafe preliminary tool call. Inspect the full execution trace.
- Failing open during errors. If validation, authorization, retrieval filtering, or human approval fails, the safe default should be to stop or provide a limited response rather than continue with unverified input.
When to revisit
Prompt injection defense should be reviewed whenever the application’s inputs, permissions, model behavior, or actions change. At minimum, revisit the checklist before a seasonal planning cycle, a major release, or a new business workflow. Also schedule a review when you add a retrieval source, connect a new tool, change document parsing, adopt a different model, modify the system prompt, or expand the assistant’s user population.
Keep a versioned threat model and injection test set with the application code or evaluation workflow. Assign an owner for reviewing failed cases and record whether the fix belongs in the prompt, application logic, access-control layer, tool schema, data pipeline, or user experience. This prevents teams from repeatedly applying prompt edits to problems that require stronger controls.
For the next review, complete these five actions:
- List every untrusted input and every side-effecting capability.
- Run direct and indirect injection cases against the current production configuration.
- Inspect tool arguments, retrieval permissions, outputs, and logs—not just final answers.
- Confirm that sensitive actions require independent authorization or approval.
- Record the result, owner, deadline, and regression tests for any unresolved risk.
As your workflow changes, this checklist should change with it. The most reliable secure AI applications combine careful prompt engineering with ordinary security practices: least privilege, input handling, explicit authorization, bounded actions, monitoring, and repeatable testing.