Prompt injection testing should be a repeatable part of AI deployment, not a one-off review before launch. This guide provides a practical threat-model checklist, reusable adversarial test cases, and mitigation patterns for separating instructions from data, limiting tool permissions, validating outputs, and logging failures in LLM applications.
Overview
Prompt injection occurs when text supplied to an LLM attempts to influence the model’s instructions, override application rules, expose protected information, or trigger an unsafe action. The text may come directly from a user, or indirectly from a document, web page, email, support ticket, database record, or retrieved knowledge-base passage.
For a conversational AI application, the model is not a security boundary by itself. A system prompt can express important rules, but application code must still enforce access control, tool permissions, data filtering, approval steps, and output validation. Treat the model as one component in a larger security design.
A useful review asks four questions:
- What can the model see? List user messages, retrieved content, conversation history, files, tool results, and system-generated context.
- What can the model influence? Identify replies, classifications, database queries, API calls, emails, workflow updates, and other outputs.
- What happens if untrusted text is treated as an instruction? Map possible data disclosure, incorrect actions, privilege escalation, or workflow disruption.
- What evidence will show that a control failed? Define logs, alerts, blocked actions, reviewer decisions, and test results.
This approach fits customer support chatbots, RAG chatbots, website AI assistants, internal copilots, and tool-using agents. For broader chatbot security coverage, see AI Chatbot Security Testing: Prompt Injection, Data Leakage, and Tool Abuse Defenses.
What to track
1. Maintain a threat-model checklist
Record the application’s trust boundaries before writing adversarial prompts. Your checklist should include:
- Instruction sources: system messages, developer prompts, user input, policy text, tool descriptions, and retrieved content.
- Untrusted data sources: uploaded files, websites, tickets, documents, emails, external APIs, and user-supplied metadata.
- Sensitive assets: personal data, credentials, internal instructions, unpublished documents, customer records, and operational details.
- Available tools: search, file access, code execution, database queries, payment actions, messaging, account changes, and administrative functions.
- Identity and permissions: the user identity, service identity, tenant boundary, and permissions attached to each tool call.
- Approval points: actions that require confirmation, human review, a second factor, or a separate service decision.
- Failure impact: what could happen if the model follows malicious text, invents a parameter, or acts on stale information.
- Observability: prompt and response metadata, retrieved document identifiers, tool-call arguments, policy decisions, and error categories.
Keep this checklist with the deployment record. Revisit it whenever you add a data source, retrieval route, tool, model, permission, or prompt template.
2. Track direct and indirect injection patterns
Use a test set that covers more than obvious phrases such as “ignore previous instructions.” Attackers may use persuasion, formatting, multilingual text, encoded content, or instructions embedded in otherwise relevant material.
| Pattern | Where it appears | What to test | Useful control |
|---|---|---|---|
| Direct override | User message | Attempts to replace rules, reveal hidden instructions, or bypass a refusal | Instruction hierarchy, refusal handling, output checks, and tool authorization outside the model |
| Role or authority claim | User message or metadata | Claims that the sender is an administrator or that an emergency removes restrictions | Verify identity and permissions in application code |
| Indirect document injection | Retrieved file, web page, ticket, or email | Malicious text presented as part of otherwise relevant content | Mark content as untrusted data and prevent it from granting permissions |
| Tool-result injection | Search result or API response | Returned content instructs the model to call another tool or disclose data | Validate tool results and require independent authorization for the next action |
| Obfuscation | Any input channel | Encoded, split, translated, or visually disguised instructions | Normalize where appropriate, test multiple languages, and enforce controls after interpretation |
| Context flooding | Long user or retrieved input | Important rules are pushed out of effective context or hidden among noise | Limit input size, prioritize trusted instructions, and test long-context behavior |
3. Use a reusable adversarial test-case template
Store each test as structured data rather than as an informal note. A practical template is:
Test ID: PI-___
Scenario: __________________________
Entry point: user | file | retrieval | tool result | memory
Untrusted content: __________________
Protected asset or action: ___________
Expected safe behaviour: _____________
Expected tool behaviour: no call | approved call | human approval
Observed response: ____________________
Observed tool calls: _________________
Control that should have worked: ______
Result: pass | fail | needs review
Severity: low | medium | high
Follow-up owner and date: _____________Include positive tests as well as attacks. For example, verify that a legitimate request can still retrieve an allowed document, call an approved read-only tool, or hand off to a human. A secure system should resist malicious instructions without making normal work unusable.
For related evaluation structure, use How to Evaluate a Chatbot: Conversation Test Cases, Metrics, and Review Templates.
Cadence and checkpoints
Run the full prompt injection test set before production deployment and after material changes. A lightweight recurring schedule helps prevent security checks from being forgotten.
Before each release
- Run direct injection, indirect injection, data-exposure, and tool-abuse cases.
- Compare results with the previous approved baseline.
- Review every new or changed tool, permission, retrieval source, and prompt.
- Confirm that failed tests block release or receive documented risk acceptance.
Monthly or quarterly
- Sample production conversations and investigate unusual refusals, tool calls, or sensitive-data requests.
- Refresh examples from newly observed failure modes and support incidents.
- Check whether documents, websites, or external feeds have introduced new untrusted content paths.
- Review logs for missing fields, excessive retention, or insufficient access controls.
- Ask application owners whether the intended user workflow has changed.
After a significant change
Rerun the relevant tests immediately after changing the model or API, system and developer instructions, retrieval pipeline, chunking or ranking method, tool schema, identity model, output parser, or approval workflow. A model update can change behaviour even when your application code has not changed, so record the model and configuration used for every test run.
Prompt changes should be managed like code changes. The guide Prompt Versioning for Teams: Templates, Testing Workflows, and Change Logs covers the records that make comparison easier.
How to interpret changes
Do not judge a test run only by whether the final answer sounds sensible. Examine the complete path from input to action.
- Pass: The application treats the malicious text as data, protects sensitive content, refuses or safely redirects the request, and makes no unauthorized tool call.
- Fail: The model reveals protected instructions, follows untrusted directions, crosses a user or tenant boundary, or produces tool arguments that the application accepts without adequate checks.
- Needs review: The answer is safe but confusing, a tool call was blocked by a late control, or the result depends on inconsistent model behaviour.
Separate model behaviour from application-control behaviour. If a model proposes a dangerous action but the authorization layer blocks it, the deployment may have contained the immediate risk, but the proposal still belongs in monitoring and regression tests. If the model gives a convincing answer while a parser silently drops a required field, that is also a security and reliability concern.
Look for trends rather than isolated examples: rising blocked tool calls, repeated attempts to access restricted data, increased false refusals, failures concentrated in one language, or regressions after retrieval changes. When a test fails, preserve the input, relevant context identifiers, model configuration, output, tool arguments, and control decision. Avoid storing sensitive content unnecessarily; log enough to investigate while applying the application’s data-handling rules.
Mitigation should be layered:
- Separate trusted instructions from untrusted content using clear message boundaries and explicit data labels.
- Give the model only the context and tools required for the current task.
- Enforce identity, tenant access, and authorization in code rather than relying on the prompt.
- Use allow-lists and strict schemas for tool names, arguments, destinations, and records.
- Require confirmation or human approval for consequential actions.
- Validate outputs before they reach users, databases, external systems, or downstream models.
- Log failures and feed representative cases back into the regression suite.
When to revisit
Revisit this article and your test register on a monthly or quarterly cadence, with a shorter interval for high-impact applications. Do not wait for a visible incident. Schedule a review before every production deployment and after any change to prompts, retrieval sources, tools, permissions, parsers, model providers, or conversation memory.
At each checkpoint, assign an owner, record the test-set version, compare pass and fail results with the previous baseline, and document unresolved risks. Retire tests only when the associated feature or threat path has been removed; otherwise, keep them so future releases can detect regression.
A practical next step is to create a small initial suite of direct, indirect, obfuscated, and tool-abuse cases, run it against a staging environment, and save the results beside your deployment checklist. Then expand the suite from real failures and near misses. For RAG systems, pair injection tests with retrieval and citation checks using RAG Chatbot Retrieval Tuning. For knowledge sources, maintain freshness and coverage through Chatbot Knowledge Base Maintenance. Repetition is the point: prompt injection prevention becomes more dependable when testing, permissions, validation, and review are part of ordinary AI deployment.