agentage.wiki / Articles / Guide
Defending AI Agents Against Prompt Injection
Separate instructions from data, gate sensitive actions, and test your agents with hidden commands to prevent unauthorized data access.
Direct Versus Indirect Injection
Direct prompt injection occurs when an attacker inserts malicious commands directly into the user prompt. These commands overwrite or confuse the system instructions, forcing the AI to ignore its original programming. An attacker might type a request that tells the model to reveal private keys or bypass safety filters immediately within the chat window.
Indirect prompt injection happens when the malicious instructions hide inside external data sources like websites, emails, or files. The AI reads this data as part of its context, unaware that the text contains hidden commands. For example, a security tool analyzing a log file might execute a command embedded in the log text itself, leaking sensitive information without the user ever typing it.
This distinction matters because the defense strategy changes based on the attack vector. Direct injection requires strict input validation and prompt hardening at the point of entry. Indirect injection demands that the system treats all retrieved content as raw data that cannot influence the agent's core logic or decision-making process.
Organizations often underestimate indirect injection because the threat comes from trusted sources like internal documents or partner websites. The vulnerability lies in the assumption that external data is safe. Attackers exploit this trust by embedding instructions in plain sight, turning standard data retrieval into a pathway for system compromise.
Treat Tool Output as Data
The most effective defense against indirect injection is to treat every piece of tool output as data, not as instructions. When an agent retrieves information from a website or a database, that content must never be allowed to modify the system prompt. The model should parse the content for facts while ignoring any text that attempts to issue commands.
Architectural patterns like Agent Self-Defense enforce this separation by isolating the instruction layer from the data layer. This ensures that even if an attacker embeds a command like 'ignore previous instructions' inside a retrieved document, the agent recognizes it as text to analyze rather than a directive to follow. The system remains bound to its original developer-specified goals.
Failure to separate instruction from data allows the AI to be hijacked by the very content it is meant to process. In real-world scenarios, this has led to agents echoing private tokens or executing unauthorized code simply because the text contained the right phrasing. The solution is a rigid boundary where user input and tool output are processed as passive information.
This approach requires careful design of the prompt structure. You must explicitly define which parts of the input are commands and which are data. By labeling external content clearly, you reduce the risk of the model confusing a malicious sentence in a file with a legitimate order from the user or the system.
Gate Sensitive Actions
Sensitive actions such as sending emails, transferring funds, or accessing private keys require a confirmation gate before execution. Even if an injection attack succeeds in convincing the agent to perform an action, the system should pause and request human approval. This creates a final barrier that stops automated attacks from causing real-world damage.
Implementing these gates means defining a list of high-risk operations that trigger a manual review process. The agent can prepare the action and explain why it is needed, but it cannot execute the command until a human verifies the request. This prevents the agent from acting on a hidden instruction found in a malicious email attachment.
Security frameworks suggest restricting the execution of potentially impactful commands entirely. If the system involves running generated code, it should do so only in a virtual machine separated from the main infrastructure. This isolation limits the damage if the code turns out to be malware or if the agent is tricked into executing harmful scripts.
Without these gates, a successful injection can lead to immediate data exfiltration or system compromise. The cost of a single breach often far exceeds the effort required to set up a confirmation workflow. Human oversight acts as the ultimate check against the AI's inability to distinguish between a genuine request and a cleverly disguised attack.
Allow-Lists for Domains and Recipients
Restricting the agent to a specific set of allowed domains and recipients limits the scope of potential attacks. If the agent is designed to summarize news, it should only fetch content from pre-approved websites. This prevents the agent from visiting a malicious site injected with commands that target the system.
Similarly, email agents should only send messages to addresses on a verified allow-list. This stops an attacker from tricking the agent into sending sensitive data to an external server or a personal email account. The system rejects any attempt to communicate outside the defined boundaries, regardless of the instruction's phrasing.
Allow-lists act as a structural defense that reduces the attack surface. By narrowing the range of valid inputs and outputs, you minimize the opportunities for an attacker to find a vulnerable path. This is particularly important for agents that interact with external APIs or public-facing web resources.
Maintaining these lists requires regular updates as business needs change, but the security benefit is significant. A strict allow-list ensures that even if the agent is compromised, it cannot reach the targets an attacker desires. It forces the system to operate within a safe, known environment.
Testing with Planted Instructions
Regular testing with planted instructions is essential to verify that your defenses are working. You should create test cases where hidden commands are embedded in documents, emails, or web pages that the agent processes. If the agent executes these commands, your system has a vulnerability that needs immediate fixing.
This adversarial testing mimics the tactics of real attackers who try to bypass safety protocols. By simulating these scenarios, you can identify weak points in your prompt engineering or architectural design. It is not enough to assume your system is secure; you must prove it by attempting to break it.
Organizations should integrate these tests into their development pipeline. Every time a new tool is added or a prompt is updated, run a suite of injection tests. This ensures that changes do not inadvertently introduce new vulnerabilities or weaken existing protections against indirect attacks.
Continuous testing builds confidence in the system's resilience. It shifts the mindset from hoping the agent is safe to knowing it is safe through evidence. This proactive approach is critical for maintaining trust in AI systems, especially when they handle sensitive corporate or personal data.
Key takeaways
- Separate system instructions from user data to prevent the AI from executing hidden commands.
- Treat all tool outputs and external content as untrusted data that cannot modify system behavior.
- Require human confirmation for sensitive actions like sending emails or accessing private keys.
- Use allow-lists to restrict the domains and recipients the agent can interact with.
- Test your agents regularly by planting malicious instructions in documents and web pages.
Related questions
What is the difference between direct and indirect prompt injection?
Direct injection happens when an attacker types malicious commands directly into the prompt. Indirect injection occurs when the malicious instructions are hidden inside external data sources like websites, emails, or files that the AI reads and processes as part of its context.
How can I prevent an AI agent from executing hidden commands in a document?
You must treat the document content as data rather than instructions. Ensure the system architecture separates the instruction layer from the data layer so that text within the document cannot override the agent's original programming or trigger unauthorized actions.
Is it necessary to have human approval for every action an AI agent takes?
Human approval is not required for every action, but it is essential for sensitive operations like sending emails, transferring funds, or accessing private keys. This confirmation gate acts as a final barrier to stop automated attacks from causing real-world damage.
AI-assisted: drafted with a language model from the author's research notes, then edited and checked before publication. Author: Zoltán Varga, AI visibility researcher and AI ecosystem architect, Budapest.