Reviewed 27 November 2026
How to test a chatbot or AI agent for prompt injection.
A prompt injection test shows whether text from a user, a document or a tool can make your AI break its rules: reveal its instructions, leak data or do something it must not do. Only test systems you own or have written permission to test, ideally in a test environment, and ask for findings you can reproduce.
What is prompt injection?
Prompt injection means that text the model reads makes it break its own rules. The OWASP Top 10 for LLM Applications lists it as risk number one, LLM01:2025 Prompt Injection. It comes in two forms:
- Direct: a user types something in the chat that makes the model change role, reveal its instructions or do something it must not do.
- Indirect: the instruction sits in content the model reads, such as a web page, a PDF, an email, a ticket or a document in a knowledge base. The user does not even have to see it.
The risk grows with what the system is allowed to do. A chatbot that only answers can leak text. An agent that can call tools can send, change or delete.
Before you test: scope and written permission
Only test systems you own, or that the owner has given you written permission to test. Testing someone else's service without permission can be illegal. Write this down before the first test:
- Which system, which environment and which test accounts are in scope. Ideally a test environment with the same model, system prompt and tools as production.
- What must not be touched: real customers, outgoing email, payments and production data.
- Test data. Use made-up people and documents, never real personal data.
- Who can stop the test, and how serious findings are reported at once.
- The time window, and who may read the report.
Four areas to test
1. Direct injection in the chat
Check that the system keeps its role when a user tries to rewrite the rules: role play, claimed authority ("I am an administrator"), instructions in other languages or in encoded form, and long conversations where the rule gets forgotten. A pass means the system says no and goes on doing its job.
2. Indirect injection through documents and web pages
Put a harmless instruction, a canary, into a test document: for example a sentence that asks the model to write a given check word in its answer. Then let the system read the document the normal way. If the check word shows up, you have shown that content can steer the model, without anything harmful happening. Test every way in: uploaded files, the knowledge base, web search, email and tickets.
3. Tools, agents and MCP
For an agent the question is not what it says but what it does. Test with mock tools or in a sandbox:
- Can injected text make the agent call a tool it should not, or with other parameters than the user asked for?
- Can it reach another user's data?
- Does the system stop for a human approval before it sends, pays or deletes?
- Does the agent read tool descriptions from an MCP server you do not control? That text is input too, and it can carry instructions.
4. Data leaks
Check whether the system can be made to show its system prompt, keys or internal addresses, documents the user has no access to, or other users' conversations. Also test whether an answer can make the browser load an external image or link with data in the address. Use made-up secrets as canaries, so that a leak shows without anything real leaking.
Make the tests repeatable
A test that runs once tells you little. Store each test case as data: input, expected behaviour, and pass or fail. Run the suite again whenever the model, the system prompt, a tool or a data source changes, ideally in your pipeline. Combine automated runs with manual attempts, because a person finds paths a list misses.
The evidence you should get after a test
Whether you test yourself or hire someone, the report should hold:
- Scope, permission, environment, model and version, and the test dates.
- For each finding: OWASP category, severity, steps to reproduce, the actual response and what it means for you.
- A fix plan in risk order: least privilege, approval steps, layered filters, logging.
- The test cases, so you can run them again.
- The result of a re-test after the fixes.
- What was not tested.
Warning signs: a guarantee that the system is "prompt-injection proof", a report with screenshots and no steps to reproduce, or a test without a written scope.
What a test cannot promise
Nobody can promise today that a language model never follows injected text. So the goal is to limit what happens when it does: give the model as little access as possible, let a person approve actions that are hard to undo, filter in layers, and log so you can see what happened. A test shows where the defences fail today. It is not a certificate.
Test yourself or get help
An internal chatbot with no tools and no sensitive documents you can often test yourself with the steps above. Get help when an agent can change data or send messages, when the system reads outside content or customer data, before a launch to customers, or when you need a report for management, audit or technical documentation for the EU AI Act.
Royan AB does this as an AI agent and chatbot security test with a fixed scope and a written quote. Read more about our AI security testing. It is not a certified penetration test of your network and infrastructure, and we do not give legal advice.
Further reading
The founder, AmirHossein Teymoori, has written about this in depth:
- How LLM chatbots get hacked: system prompt leaks, model fingerprinting, tool discovery and indirect injection, with a checklist for testing your own chatbot.
- LLM guardrails that survive production: a layered defence against prompt injection, leaked personal data and agents that go too far.
- Sandbox AI coding agents: why agents that run code need a sandbox without network and without credentials.
FAQ
Questions about prompt injection testing.
Is it allowed to test for prompt injection?
On systems you own, or with written permission from the owner, it is a normal part of security work. Testing someone else's service without permission can be illegal. We do not give legal advice; ask your lawyer if you are unsure.
Can we test in production?
Preferably not. Use a test environment with the same model, system prompt and tools. If you must test in production: use test accounts, made-up data, an agreed time window, and turn off or require approval for tools that write, send or pay.
How often should we test?
Every time the model, the system prompt, a tool or a data source changes, ideally automatically in your pipeline. Run a larger manual test before launch and after big changes.
Can a system be fully protected against prompt injection?
No, not today. You can lower the risk and limit the damage: least privilege, human approval for actions that are hard to undo, layered filters and logging. A test shows where the defences fail.