How to secure LLM agents that call tools: a threat model, a checklist, and a tested policy gate you can drop into your own code. Defensive guidance for systems you build or operate.
An LLM agent is a program whose control flow is decided by text that attackers can influence (web pages, emails, documents, tool output). So:
Never rely on the model to enforce security. Put deterministic code between the model and every tool.
The model proposes. Code decides.
| Threat | How it happens | Impact |
|---|---|---|
| Direct prompt injection | the user types instructions that override yours | policy bypass |
| Indirect prompt injection | instructions hidden in a page, email, file or tool result the agent reads | the attacker steers the agent without ever talking to it |
| Excessive agency | the agent has more tools or permissions than the task needs | one injection becomes real damage |
| Data exfiltration | injected instructions make the agent send data out (URLs, markdown images, email, tool calls) | secrets and private data leak |
| Confused deputy | output of a low-trust tool flows into a high-privilege tool | privilege escalation across tools |
| Tool supply chain | a malicious or compromised MCP server or plugin | code and data access with your permissions |
| Unbounded consumption | loops, huge outputs, repeated calls | cost and denial of service |
| Sensitive output handling | model output rendered or executed unsafely (HTML, shell, SQL) | XSS, RCE, injection |
Mapped to the OWASP Top 10 for LLM Applications (2025 edition): prompt injection, sensitive information disclosure, supply chain, data and model poisoning, improper output handling, excessive agency, system prompt leakage, vector and embedding weaknesses, misinformation, unbounded consumption.
An agent is in serious danger when it has all three at once:
- access to private data,
- exposure to untrusted content,
- a way to communicate outward (HTTP, email, rendering remote images).
Remove any one leg and the exfiltration path breaks. Design for that: if an agent reads untrusted web pages, it should not also hold your secrets and unrestricted network access.
| Control | Concretely |
|---|---|
| Least privilege tools | only the tools the task needs; read-only by default |
| Allowlist, not blocklist | unknown tool or host is denied |
| Validate arguments in code | types, ranges, path roots, URL schemes and hosts |
| Human approval for risky actions | writes, sends, payments, deletes, anything irreversible |
| Separate trust levels | never pass untrusted content to a tool with high privilege |
| Egress control | allowlisted hosts only; block private and metadata IP ranges |
| Output handling | treat model output as untrusted: escape, sanitize, never eval or shell=True |
| Budgets | max steps, max tokens, max calls per tool |
| Audit log | every proposed call and the decision, so you can investigate |
| Isolation | run tool processes in a container or low-privilege user |
| Secrets | short-lived, scoped tokens; never put secrets into the prompt |
agent_guard.py is a ~90-line dependency-free gate. 11 tests pass (python3 -m unittest).
from agent_guard import Guard, ToolSpec, safe_path, safe_url
ROOT = "/srv/agent-workspace"
guard = Guard(tools={
"read_file": ToolSpec("read_file", validate=lambda a: safe_path(a, ROOT), max_calls=10),
"fetch": ToolSpec("fetch", validate=lambda a: safe_url(a, {"docs.example.com"})),
"send_email": ToolSpec("send_email", risk="high"), # always needs a human
}, max_total_calls=50)
decision = guard.check(tool_name_from_model, args_from_model)
if decision.verdict.value == "allow":
run_tool(...)
elif decision.verdict.value == "needs_human_approval":
ask_human(decision.reason)
else:
refuse(decision.reason)What it enforces: tool allowlist, argument validation, per-tool and total call budgets, high-risk approval, and an audit log. Validators block path traversal (including symlink escapes), non-HTTPS URLs, non-allowlisted hosts, and hosts resolving to private, loopback or link-local addresses (the cloud metadata range).
What it does not do: detect injections in text, sandbox code execution, or replace your authorization checks. It is one layer.
See the companion prompt-injection-playground for why text filters alone fail.
- Pin server versions; read tool descriptions (they are prompt text and can contain injections).
- Give each server its own scoped credentials.
- Do not chain a server that reads the web into a server that can write or send.
- Monitor network behavior of server processes. Wiretap shows per-process outbound connections, so an MCP server that contacts an unexpected host becomes visible.
More in awesome-mcp-servers.
- Can you list every tool each agent can call? 2. Can you disable one instantly? 3. Do you log decisions and arguments? 4. Can you revoke the agent's credentials in one step? If any answer is no, fix that before adding features.
ai-agent-security | prompt-injection-playground | awesome-mcp-servers | awesome-ai-agents | Wiretap - observe what your agent processes actually send out.