Securing AI Agents and Tool Use
Constraining autonomous agent actions.
Securing AI Agents and Tool Use is a free Cyber Security Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Cyber Security Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What Makes Agents Risky
An AI agent is an LLM wired to tools and a loop: it reasons, calls functions (search, code execution, APIs, file access), observes results, and repeats until a goal is met. This autonomy is powerful and dangerous.
The core security shift: with a plain chatbot, a bad output is just text. With an agent, a bad decision becomes a real action: a deleted record, a sent email, a spent dollar, a leaked secret.
Because untrusted content can enter the reasoning loop, every tool the agent holds is an attack surface for prompt injection.
Least Privilege for Tools
The single most important control is least privilege. Give each tool the narrowest scope that still does the job.
- Prefer read-only over read-write; scope reads to the current user's data.
- Split broad tools into narrow ones (a
get_invoicetool, not a raw SQL tool). - Bind tool credentials to the end user's identity, not a shared service account, so the agent inherits only what the user may do.
# Scope queries to the authenticated user, never raw SQL
def get_invoice(invoice_id: str, *, user_id: str):
return db.query(
"SELECT * FROM invoices WHERE id=%s AND owner=%s",
(invoice_id, user_id),
)Human-in-the-Loop Checkpoints
For high-impact or irreversible actions, require explicit human approval before execution. The agent proposes; a person confirms.
- Sending external email or messages.
- Financial transactions or purchases.
- Deleting or overwriting data.
- Deploying code or changing infrastructure.
Show the user the exact action and arguments in plain language so they can catch an injected or hallucinated command before it runs.
Sandboxing Code and Commands
Agents that run code or shell commands must do so in an isolated sandbox, never on the host.
- Use ephemeral containers or microVMs with no host mounts.
- Drop network access by default; allow only an explicit egress allowlist.
- Set CPU, memory, and time limits to contain runaway or malicious code.
- Run as a non-root, unprivileged user with a read-only root filesystem.
docker run --rm \
--network none \
--read-only \
--user 1000:1000 \
--memory 256m --cpus 0.5 \
--pids-limit 64 \
agent-sandbox:latest python /work/task.pyBreaking the Lethal Trifecta
An agent becomes a data-exfiltration tool when it simultaneously has access to private data, exposure to untrusted content, and the ability to communicate externally. That combination is the lethal trifecta.
Design to remove at least one leg in any given workflow:
- Isolate sessions that touch untrusted content from those holding sensitive data.
- Restrict outbound network egress to a strict allowlist.
- Require approval before any external send when private data is in context.
Untrusted Tool Output
Tool results re-enter the model's context, so tool output is untrusted input. A web page, a fetched file, or an API response can contain injected instructions aimed at the next reasoning step.
- Wrap tool output in clear delimiters and label it as data, not commands.
- Strip or neutralize hidden text (HTML comments, zero-width characters, off-screen CSS).
- Cap the size of injected content to limit payload room.
Never let raw tool output silently dictate the next tool call without policy checks.
Action Allowlists and Policy Enforcement
Do not rely on the model to police itself. Enforce a policy layer in code between the agent and every tool.
- Validate each tool call against an allowlist of permitted actions and argument shapes.
- Reject calls that fall outside the current task's scope.
- Apply per-tool, per-user rate limits and budgets.
This deterministic gate runs regardless of what the model decides, so a successful injection still hits a hard wall.
def authorize(call):
if call.name not in ALLOWED_TOOLS:
raise PolicyError("tool not allowed")
if not SCHEMA[call.name].validate(call.args):
raise PolicyError("bad arguments")
if exceeds_budget(call):
raise PolicyError("rate limit")Limiting the Loop
Autonomous loops can spiral: infinite retries, recursive tool calls, runaway spend (denial-of-wallet). Bound them.
- Cap the maximum number of steps and total tokens per task.
- Set wall-clock timeouts on the whole run.
- Track cumulative cost and abort on threshold.
- Detect loops (repeated identical calls) and break out.
These limits also blunt DoS and unbounded-consumption attacks (OWASP LLM10).
Memory and Multi-Agent Risks
Persistent agent memory and multi-agent systems add new surface:
- Memory poisoning: an injection written to long-term memory in one session influences later sessions. Validate and scope what gets persisted.
- Cross-agent trust: one compromised agent can inject into another. Treat inter-agent messages as untrusted.
- Confused deputy: a privileged agent acting on a lower-trust agent's request. Carry the original principal's authorization through the chain.
Logging and Observability
You cannot secure what you cannot see. Instrument the full agent trace:
- Log every tool call, its arguments, and its result.
- Record the reasoning context and any retrieved content for forensics.
- Alert on anomalies: unexpected egress, privilege use, repeated denials, cost spikes.
- Keep an immutable audit trail tied to the acting user.
Good telemetry turns a silent compromise into a detectable, investigable incident.
A Layered Agent Architecture
Putting it together, a defensible agent stack looks like:
- Identity: actions run as the end user with least-privilege scopes.
- Policy gate: deterministic allowlist and schema validation on every tool call.
- Sandbox: isolated execution with constrained network and resources.
- Human checkpoints: approval for irreversible actions.
- Limits: step, token, time, and cost budgets.
- Observability: full audit logging and anomaly alerts.
Assume the model can be hijacked; make sure that, when it is, the blast radius stays small.
Quick Check
Test your understanding of agent security controls.
Recap
Securing AI agents and tool use:
- Agents turn bad outputs into real actions, so every tool is attack surface.
- Apply least privilege per tool and bind credentials to the end user.
- Require human approval for irreversible actions and sandbox code execution.
- Break the lethal trifecta; treat tool output as untrusted input.
- Enforce a deterministic policy gate (allowlists, schema validation) in code, not in the prompt.
- Bound the loop (steps, tokens, time, cost) and log every tool call for detection.
Frequently asked questions
Is the “Securing AI Agents and Tool Use” lesson free?
Yes — the full text of “Securing AI Agents and Tool Use” is free to read here on the web, and the Cyber Security Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Cyber Security Academy course, upgrade to CoddyKit PRO.
What will I learn in “Securing AI Agents and Tool Use”?
Constraining autonomous agent actions. You practise Cyber Security Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Cyber Security Academy?
No prior experience is required. Cyber Security Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Securing AI Agents and Tool Use” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Cyber Security Academy lesson?
Yes. Every Cyber Security Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Prompt Injection and Jailbreaks
- The OWASP LLM Top 10
- Securing AI Agents and Tool Use
- Model, Data and Supply-Chain Risks