AI Security and Prompt Injection: Containing the Risk in Intelligent Agents
Prompt injection is one of the most important threats to applications built on language models. This article explains its forms and the practical layers of defense.
Key takeaways
- A language model has no reliable boundary between instructions and data, so do not rely on it for security.
- Risk spikes when a model has both private data and access to tools or the network.
- Defend in layers: least privilege, output handling, human confirmation, isolation and logging.
When a language model only produces text, its worst mistake is a wrong answer. Connect it to email, files, a browser or enterprise tools, and the same model can take actions. That is where prompt injection becomes dangerous: an attacker tries to change the model's behavior using text they wrote. OWASP's list of risks for LLM applications ranks it first. This piece is educational and defensive.
The root of the problem
In traditional software, code and data are kept apart; parameterized SQL queries are the classic example. In a language model, the system prompt, the user's question and the text of a document are all one stream of tokens. Models are trained to follow instructions, but there is no airtight way to tell a legitimate instruction from text that merely looks like one. The sensible assumption is that injection will sometimes succeed, so systems should be built so that a success does limited damage.
Direct versus indirect injection
In direct injection, the user themselves tries to talk the model out of its rules. This is mostly a content-policy and service-abuse problem. In indirect injection, instructions hide in content the model reads later: a web page, an email, a shared document, a review, or a tool's output. The legitimate user did nothing suspicious; they simply asked the assistant to summarize it. This variant is more dangerous for agents and RAG systems because the source of the threat is invisible to the person using the product.
Why tools raise the stakes
Security researchers describe a risky combination, sometimes called the lethal trifecta: access to private data, exposure to untrusted content, and the ability to communicate externally. If one agent has all three, poisoned text can make it read private data and send it out through a web request, an image URL or an email. Removing any one of the three closes the leak path.
Defense in depth
- Least privilege: give each tool only the authority it needs, with narrowly scoped, read-only tokens wherever possible.
- Input handling: label the origin of every piece of text, separate untrusted content from instructions with clear delimiters, and cap unusually long or odd inputs. This helps but is never a guarantee.
- Output handling: treat model output like untrusted user input. Validate and sanitize before displaying, executing or storing it, and block or allow-list external links and images.
- Human confirmation: require explicit approval for irreversible or sensitive actions such as sending email, paying or deleting, and show the real details of the action.
- Isolation: run tools in sandboxes with restricted network egress, and keep different users' data apart.
- Logging and monitoring: record tool calls, context sources and approvals so unusual behavior and incidents can be investigated.
Testing and a review checklist
Test your system in a controlled environment: if a document contains an unwanted instruction, does the agent call a tool? Does data flow to an external destination? These checks belong in the release cycle, not in a one-off audit. Before shipping, walk through these questions:
- What data and tools can the agent reach, and is each one truly necessary?
- Which inputs can an attacker influence?
- What paths exist for sending data out?
- Which actions are irreversible, and do they require human approval?
- If an attack happened today, would the logs let you reconstruct it?
This article is sample educational content; verify against official sources before making important decisions.