Agent guardrails
Sandboxing, permissions, hooks and input handling that bound what an agent can do: the sandbox vs approval-policy split, the lethal trifecta and Rule of Two, SDK permission pipelines and their gaps, scoped credentials and egress control.
Contents
Agent guardrails are the limits on what an agent is allowed to do. The more capable an agent is, the more they matter. Good guardrails are what let autonomy increase over time instead of requiring step-by-step supervision. They are enforced by the harness around the model, not by the model: a model’s trained preference for its system prompt over the text it reads is a tendency, not a security boundary (Wallace et al.; see system prompts). Text-level checks on inputs and outputs are a separate layer, covered in guardrail tooling.
Layers
- Sandboxing. Run code in isolated environments (containers, microVMs) so mistakes stay contained.
- Permissions. Read-only actions run freely. Writes, deletions and anything public-facing need approval or an explicit policy. See tools.
- Treating inputs as data. Web pages, emails, tool results and MCP responses can contain text that looks like instructions. An agent that obeys what it reads is open to prompt injection. Fetched content should inform the agent, never command it.
Synthesis: the diagram places the layers on this page along one tool call.
flowchart LR M["Model proposes tool call"] --> H["Hooks<br/>(deterministic, exit 2 blocks)"] H --> P["Permission rules / approval policy<br/>(allow, ask, deny)"] P -->|"ask"| U["Human approval"] P -->|"allow"| S["Sandbox<br/>(filesystem + network boundary)"] U -->|"approved"| S S --> E["Egress proxy<br/>(allowlist + injected credential)"] S --> R["Tool result"] R --> G["Input checks on result<br/>(often missing)"] G --> M
Sandbox vs approval policy
Codex separates two questions: what commands can technically do (the sandbox: read-only, workspace-write, danger-full-access) and when the agent must stop and ask (the approval policy: on-request asks before leaving the sandbox, using the network or running untrusted commands; never is for CI and only alongside a sandbox). Network is off by default and writes are confined to the workspace (Codex). Anthropic makes the same split in Claude Code: an OS-level sandbox (bubblewrap on Linux, Seatbelt on macOS) lets the agent act freely inside a pre-defined boundary, which cut permission prompts by 84% internally and reduces approval fatigue (Claude Code sandboxing). Both layers are needed: without network isolation a compromised agent exfiltrates keys, and without filesystem isolation it can escape the sandbox and reach the network (Claude Code sandboxing). See human-in-the-loop for the approval side.
The lethal trifecta and the Rule of Two
The design rule for injection is architectural. Willison’s lethal trifecta: an agent with access to private data, exposure to untrusted content and a way to communicate externally can be made to steal that data (Willison). Meta’s Agents Rule of Two turns it into a per-session limit: at most two of untrustworthy input, sensitive data or systems, and state change or external communication. If all three are needed, the agent must not run autonomously (Meta). Classifiers do not substitute for this: a detector catching 95% fails in application security because attackers retry (Willison), and Anthropic’s hardened browser agent still showed 1.4% attack success against an adaptive attacker (Anthropic). OWASP moved Excessive Agency up to LLM03 in its 2026 list, reflecting agent deployments (OWASP 2026; see OWASP Top 10 for LLM, LLM security risks).
Scoped credentials and egress control
A sandbox removes the exfiltration leg only if it controls the network. Use default-deny egress with domain or CIDR allowlists, and time-varying policy: allow the package registry during setup, then cut it before untrusted code runs (Vercel, Codex). Keep secrets out of the sandbox and out of the model: a proxy outside the sandbox attaches the real credential only on requests to the approved destination, as Claude Code on the web does with its git proxy (Claude Code sandboxing, Vercel). The IETF agent-auth draft states the rule directly: the model must not have access to agent or tool credentials (agent-auth draft). Details: code execution sandboxes, agent authentication and authorization.
Hooks: deterministic enforcement
Hooks run code the harness controls at lifecycle events (PreToolUse/BeforeTool, PostToolUse, UserPromptSubmit, Stop). Claude Code, Codex and Gemini CLI share a contract: the hook gets JSON on stdin, and exit 2 blocks the action with stderr returned to the model as the reason (Claude Code hooks, Codex hooks, Gemini hooks). Pitfalls:
- In Claude Code and Gemini CLI any other non-zero exit is only a warning and the action proceeds (Claude Code hooks, Gemini hooks).
- A failing or timed-out Claude Code hook fails open unless a
command/httphook setsonFailure: "block"(v2.1.295+; no effect onStop,SubagentStopor async hooks) (Claude Code hooks). - Codex’s
PostToolUsecannot undo a command that already ran; it only replaces the result the model sees (Codex hooks).
SDK permission pipelines and their gaps
- Claude Agent SDK. Every tool call passes hooks, then deny rules, ask rules, the permission mode, allow rules, and finally the
canUseToolcallback.allowed_toolspre-approves rather than restricts; inbypassPermissionsonly deny rules, ask rules and hooks still apply; auto-approved calls never reachcanUseTool, so audit every call with aPreToolUsehook. For headless agents the docs recommend an explicit allow-list plusdontAsk(Agent SDK permissions; see Claude Agent SDK). - OpenAI Agents SDK. Input guardrails run only on the first agent, and by default in parallel with it, so a tripwire can fire after tools already ran (
run_in_parallel=Falseblocks first). Output guardrails run only on the final agent. Tool guardrails skip handoffs, hosted tools, computer and shell tools, andas_tool()agents (OpenAI guardrails; see OpenAI Agents SDK, agent handoffs).
Managed guardrails often skip tool traffic
Indirect injection arrives through tool results, and that path is the one managed services check least. Bedrock content filters and its prompt-attack filter do not evaluate tool results, tool specs or tool-call arguments (Bedrock). Azure Prompt Shields can scan agent tool responses only in preview, and Foundry’s document-attack detection is off by default (Azure). OpenAI inline moderation checks tool arguments and outputs, but not tool names, descriptions or schemas (OpenAI). See guardrail tooling.
Memory is an input too
Agent memory is a persistent injection channel. In MINJA, ordinary queries planted records later retrieved as demonstrations, with 98.2% injection and 76.8% attack success reported in early versions; a follow-up argues these come from idealised settings, so treat them as an upper bound (Dong et al.). OWASP 2026 widens LLM01 to persistent payloads in memory or RAG corpora (OWASP 2026). Treat memory as untrusted input with per-record provenance and per-user isolation.
Computer use
When an agent drives a screen, Claude may return several actions in one turn, so human approval must inspect each action before it runs, not the turn as a whole: one batch can complete a multi-step action (Anthropic computer use). See computer-use agents.
Human-in-the-loop
At predefined checkpoints the agent pauses for a person to approve, correct or supply input. This is the standard choice for high-stakes, safety-critical or subjective decisions, such as approving the release of a redacted health dataset. It improves safety at the cost of building and maintaining the review system (Google Cloud).
For multi-agent systems, the same guide recommends controlling agent traffic, giving each agent least-privilege identity, and registering trusted tools and subagents. It names Google products for each of these.
Approvals do not scale per step: sandboxing removes most prompts so attention goes to actions that leave the boundary (Claude Code sandboxing), and Codex can route approval requests to a reviewer agent that fails closed (Codex).
Related
Agent loop: the harness that runs the loop is where guardrails are enforced. Harness engineering covers the harness as a whole; coding agents show these layers in daily use.
Going deeper
- Prompt injection: direct and indirect injection, the lethal trifecta and the Agents Rule of Two.
- Lethal trifecta: the three capabilities that together enable data theft.
- Human-in-the-Loop: approval gates, interrupts and escalation triggers in practice.
- Code Execution Sandboxes: isolation options, egress control and the sandbox vs approval-policy split.
- Agent hooks and middleware: deterministic lifecycle hooks and framework middleware.
- Agent Authentication and Authorization: scoped credentials and keeping secrets out of the model.
- Guardrail tooling: runtime input/output checks, including which paths (e.g. tool results) they skip.
- LLM security risks: the wider threat model (OWASP LLM Top 10, data exfiltration, memory poisoning).
- Computer-Use Agents: per-action approval and allowlists when the agent drives a screen.
- PII and Data Privacy: PII detection and redaction as a guardrail layer.
Sources
- The Anatomy of a Modern AI Agent
- Choose a design pattern for your agentic AI system (Google Cloud)
- Willison: the lethal trifecta
- Meta: Agents Rule of Two
- Claude Code sandboxing (Anthropic)
- Codex approvals and security (OpenAI)
- Vercel: a sandbox without a network boundary
- Claude Code hooks docs
- Codex hooks docs
- Gemini CLI hooks docs
- Claude Agent SDK permissions
- OpenAI Agents SDK guardrails
- Amazon Bedrock Guardrails documentation
- Azure AI Content Safety documentation
- OpenAI moderation guide
- Anthropic: prompt injection defenses for browser use
- MINJA memory injection (Dong et al.)
- OWASP Top 10 for LLM Applications 2026
- AI Agent Authentication and Authorization draft
- Anthropic computer use tool docs
- The Instruction Hierarchy (OpenAI)