Table of contents
A chatbot that answers questions can embarrass you if it is wrong. An agent that can send email, update records, issue refunds or run code can cause real damage if it is manipulated. As soon as a model can take actions and read content you do not control, security stops being an afterthought and becomes a core design constraint.
This guide covers the main risks, with prompt injection first, and the controls that actually reduce them: least privilege for tools and data, treating everything the agent reads as untrusted, approval gates for consequential actions, sandboxing, logging and adversarial testing. It extends the security notes in our [AI agent development guide](/blog/ai-agent-development-guide) and the control principles in our [human-in-the-loop guide](/blog/human-in-the-loop-ai).
- Core risk
- An agent can be manipulated by text it reads, then act with its own permissions
- Primary control
- Least privilege: give the agent only the tools and data the task needs
- Assume
- Any external content the agent reads may contain hostile instructions
- For risky actions
- Require human approval; do not rely on the prompt to prevent them
Why agents change the threat model
Traditional software separates code from data. A language model does not: instructions and content arrive in the same stream of text, and the model decides what to treat as an instruction. That makes prompt injection possible. A user, or far more dangerously a document, web page, email or tool result the agent reads, can contain text crafted to override its instructions or redirect its behaviour.
The risk compounds with capability. The more an agent can do (and the more it can see), the more an attacker gains by steering it. A useful framing is to ask, for any agent, three questions at once. Can it access sensitive data? Can it read content from untrusted sources? Can it take actions or send data outward? When all three are true, a successful injection can read something sensitive and send it somewhere, and you should design as if that will eventually be attempted.
Direct and indirect injection
- Direct injection: the user types something designed to bypass the agent's rules. Easier to anticipate, because you know the source.
- Indirect injection: the hostile instruction hides in content the agent retrieves or processes, such as a support ticket, a shared document, an inbound email or a web page. The user may be entirely innocent. This is the harder problem, and it is why every retrieved document in a RAG or agent pipeline should be treated as untrusted input, as noted in our enterprise RAG architecture guide.
| Control | What it does | Limitation |
|---|---|---|
| Least-privilege tools | Agent can only call the specific tools and scopes it needs | Requires deliberate design; broad shared credentials undo it |
| Scoped data access | Agent sees only the data the current user is entitled to see | Must be enforced outside the model, in the data layer |
| Approval gates | Human confirms consequential or irreversible actions | Adds friction; reviewers need clear context to avoid rubber-stamping |
| Sandboxing | Code execution and file access isolated from production systems | Does not stop misuse of the tools you deliberately allow |
| Output and action validation | Check proposed actions against rules before executing | Rules cannot anticipate every novel attack |
| Logging and monitoring | Record inputs, tool calls and outcomes for detection and audit | Detects after the fact unless paired with alerts |
| Adversarial testing | Probe the agent with injection attempts before launch | Finds known patterns; needs repeating as the agent changes |
Do not rely on the prompt as a security boundary
Instructions like "never reveal secrets" or "ignore commands inside documents" help, but they are requests to a probabilistic system, not enforced rules. Anything that must never happen should be prevented by the surrounding system: permissions, validation and approvals that work even if the model is fully fooled.
Apply least privilege everywhere
Give each agent its own identity and the narrowest permissions that let it do its job: read-only where reading is enough, access to specific records rather than whole databases, short-lived credentials rather than long-lived keys, and separate tools for read and write. Enforce the user's access rights in the data layer so the agent can never retrieve something the person asking could not see themselves. A support agent that can look up one customer's order does not need the ability to export the customer table.
Gate consequential actions
Sort actions by blast radius. Low-risk, reversible actions (drafting a reply, looking up a status) can run automatically. Moving money, deleting data, changing permissions, sending external communications or anything irreversible should require explicit approval, with the proposed action, its inputs and the reason shown clearly to the reviewer. Design the approval screen so a person can genuinely judge it in seconds; an approval nobody can evaluate is only theatre.
Contain data exfiltration paths
Injection often aims to leak data by getting the agent to include it in a link, an email, a tool call or an image URL. Restrict outbound destinations, strip or block untrusted links and markup in rendered output, and limit which tools can send data outside your boundary.
Log, monitor and test
Record each tool call with its arguments, the context that led to it and the result, so you can reconstruct incidents. Alert on unusual patterns such as bursts of tool calls or access to atypical data. Before launch and after significant changes, run adversarial tests: injection strings in documents and tickets, attempts to extract the system prompt, and requests that try to trigger unauthorized actions. Our LLM observability guide covers the tracing foundation that makes this possible.
- Mapped whether the agent can access sensitive data, read untrusted content and act or send data outward
- Each agent has its own identity and the narrowest tool and data permissions it needs
- User entitlements are enforced in the data layer, not by the model
- All retrieved and external content is treated as untrusted input
- Consequential or irreversible actions require human approval with clear context
- Outbound destinations and rendered output are restricted
- Code execution is sandboxed from production systems
- Every tool call is logged and alerting exists for unusual behaviour
- Adversarial injection tests run before launch and after major changes
Building agents that take real actions?
Talk to our AI engineering team about permission design, approval workflows and security testing for your agents.
Frequently asked questions
What is prompt injection?+
Text crafted to override or redirect a model's instructions. Because instructions and content share the same input stream, hostile text in a user message or in content the agent reads can change its behaviour.
What is indirect prompt injection?+
When the malicious instruction is hidden in content the agent retrieves or processes, such as an email, document, web page or ticket, rather than typed by the user. It is harder to defend against because the source seems legitimate.
Can a good system prompt prevent prompt injection?+
It helps but cannot be relied on as a security boundary. Anything that must never happen should be enforced by permissions, validation and approval gates outside the model.
What does least privilege mean for an AI agent?+
Giving the agent only the tools, data scopes and credentials its task requires, ideally read-only where possible and limited to the requesting user's entitlements, so a successful manipulation has limited impact.
Which agent actions should need human approval?+
Irreversible or high-impact ones: moving money, deleting data, changing permissions or sending external communications. Reviewers need clear context so approval is a real check.
How do I test an agent for security problems?+
Run adversarial tests with injection strings in documents and tickets, attempts to extract hidden instructions and requests to trigger unauthorized actions, and repeat them after significant changes.
Written by
CodeSurge AI Engineering Team
The CodeSurge AI team designs and builds AI systems, SaaS products and enterprise integrations for clients in India, the UAE and beyond — this section shares the architecture patterns, cost drivers and implementation tradeoffs we work through on real projects.