← All news

News / Learn / AI agents

How do you keep AI agents safe?

You'll be able to limit what an AI agent can do, so one bad output can't cause serious harm.

An AI agent takes actions, such as sending emails, editing files, or calling APIs. Its model can be wrong or tricked, so you cannot rely on it to behave safely. Safety comes from the system around the model: limits on tools, permissions, and checks on actions.

Agents act on real systems and real data. A single wrong tool call can delete records, leak customer information, or spend money. Products that ship without limits on agent actions face these risks on day one, and users lose trust quickly.

Start with least privilege. Give the agent only the tools and data it needs for the task. Scope each tool narrowly. A tool that reads one folder is safer than a tool that reads the whole disk. Use separate credentials with minimal rights, and never give the agent your admin keys.

Separate reading from acting. Content the agent reads, such as web pages, emails, or documents, can contain instructions written by an attacker. This is called prompt injection. Treat anything the agent reads as untrusted input. Do not let that text alone trigger a high-impact action.

Add a human check for risky actions. Actions that spend money, delete data, send messages to outside parties, or change permissions should need approval. Show the user exactly what the agent plans to do before it runs. Log every tool call so you can review what happened.

Validate tool inputs and outputs in code, not in the prompt. Check argument types, value ranges, and allowed targets before a tool runs. Set hard limits such as step counts, time limits, and spending caps so a loop cannot run forever. Test the agent against adversarial inputs, and use evals to track failures over time.

A guard function checks a tool call in code before it runs. The model never decides these rules.

ALLOWED_DIRS = ("/srv/app/reports",)
MAX_STEPS = 20
NEEDS_APPROVAL = {"send_email", "delete_file", "transfer_funds"}

def check_call(name, args, step, approved):
    if step >= MAX_STEPS:
        raise RuntimeError("step limit reached")
    if name == "read_file":
        path = args["path"]
        if not any(path.startswith(d + "/") for d in ALLOWED_DIRS):
            raise PermissionError(f"path outside allowed folders: {path}")
    if name in NEEDS_APPROVAL and not approved:
        raise PermissionError(f"{name} needs human approval")
    return True

A short sketch to show the idea. Check the official docs of the tool you use.

  • Trusting the system prompt as a security control A prompt is a suggestion, not a boundary. Enforce limits in code, such as permission checks, allowlists, and approval gates.
  • Giving the agent broad credentials Create scoped keys for the agent with only the rights its tasks need. Rotate them and revoke them when unused.
  • Letting untrusted content drive actions Treat web pages, documents, and emails as data, not commands. Require approval before acting on instructions that came from them.
  • No limits on loops or spending Set maximum steps, timeouts, and budgets. Stop and report to the user when a limit is hit.

Study the OWASP guidance on LLM applications, which lists common risks such as prompt injection and excessive agency. Learn threat modeling for your own product. Build a red-team test set and run it on every change.

Can I make an agent fully safe?

No system is perfectly safe. Aim to limit the damage a failure can cause. Combine least privilege, approvals, logging, and tests so that failures are small and visible.

Is a stronger model enough to keep agents safe?

A better model reduces some errors but cannot guarantee safe behavior. Keep the same controls in place regardless of which model you use.

What is prompt injection?

It is when text the agent reads contains instructions that try to override your intent, such as telling it to send data to an outside address. Defend by isolating untrusted input and gating high-impact actions.

Should every tool call need approval?

No. Approval for every step makes the agent slow and annoying. Reserve approval for actions that are costly, irreversible, or affect other people.

Written with AI by CapyLaunch. Spotted a mistake? Email hello@capylaunch.com.