Most “AI safety” advice for coding agents amounts to writing firmer instructions: put “never touch production” in the system prompt, add “be careful with secrets” to CLAUDE.md, and hope. That approach has a fatal flaw — it tries to make the model behave, when the whole problem is that a probabilistic system sometimes won’t, and an attacker who plants an instruction in something the agent reads is competing with your prose on equal footing. Real AI agent guardrails don’t ask the model to behave. They constrain what it can do when it doesn’t — so that a misbehaving or hijacked agent hits a wall it physically cannot argue past.
This is a Secure-pillar build sheet in our agentic loops series. It assumes you already know why agents are dangerous — the lethal trifecta at fleet scale, the skills-as-supply-chain risk. This piece is the how: the specific, deterministic controls that hold.

🧱 The Distinction Everything Rests On: Context vs. Configuration
Internalize one line and the rest follows: context is probabilistic; configuration is deterministic.
Everything you put in the model’s context — CLAUDE.md, skill prose, “be careful” instructions — is guidance the model may ignore. Worse, an injected instruction from a malicious GitHub issue or a poisoned tool description lands in that same context and competes with your rules on equal terms. The model cannot reliably tell your intent from an attacker’s, because to the model they’re both just text.
Everything enforced by configuration — sandboxes, permission allowlists, PreToolUse hooks, network egress rules, branch protection — is applied by the client and the platform regardless of what the model decides. That’s the entire game. A secure harness spends its trust budget on configuration and uses context only to reduce friction, never as a security boundary. The design goal isn’t “the agent won’t read the secret.” It’s “the agent can’t — and if it tries, there’s an audit trail and a human gate.” If a rule isn’t enforced by a sandbox, a hook, a permission, or CI, it isn’t a control. It’s a wish.
📦 Sandbox the Executor (The Highest-Leverage Guardrail)
If you do only one thing, do this. Run any agent that executes code inside an OS-enforced sandbox with two boundaries:
- Filesystem isolation — the agent reads and writes only the directories you declare. A prompt-injected agent physically cannot reach
~/.ssh,~/.aws, or your.env. - Network isolation — a proxy restricts outbound traffic to an allowlisted set of domains. This breaks the exfiltration leg of the trifecta at the kernel: even if an agent is successfully hijacked and reads something sensitive, it has nowhere to send it.
Anthropic’s Claude Code sandbox (built on Linux bubblewrap and macOS seatbelt, and covering spawned subprocesses, not just the top-level call) is the reference implementation, and its own framing is the goal to aim for: “even a successful prompt injection is fully isolated, and cannot impact overall user security.” There’s a second-order benefit that matters more than it looks: sandboxing let Anthropic cut internal permission prompts by 84% — which directly attacks approval fatigue. When the sandbox handles the routine safety, the human gates that remain are rare enough to get real attention. Treat “the agent can reach arbitrary hosts” as a P0 misconfiguration; it silently re-arms the whole trifecta.

🪝 PreToolUse Hooks: Deterministic Blocks the Model Can’t Talk Past
Hooks are the enforcement layer where your specific policies become code. A PreToolUse hook is client-side code that fires before a tool runs and can block it outright — exit code 2 denies the action, regardless of what the model wanted to do. This is where “guidance” becomes a boundary:
- Block any write outside
src/(or wherever your change is scoped). - Block edits to CI configuration, branch-protection files, or the test harness itself.
- Block outbound calls to any non-allowlisted host.
- Block reads of secret paths and destructive shell verbs before they ever execute.
The critical property is that a hook doesn’t negotiate. An injected instruction can convince the model to attempt cat ~/.ssh/id_rsa; it cannot convince the hook to let the call through. That’s the difference between a rule and a control.
🔑 Least-Privilege Allowlists
Give each agent the narrowest set of tools its job requires, and no more. The cardinal sin is the blanket Bash(*) allow — a shell wildcard is a blank check. Instead:
- Explicit allowlists, never wildcards. Enumerate the exact commands a role needs.
- Deny the destructive verbs outright:
rm -rf,git push --force, cloud-CLI delete commands, and secret paths (~/.ssh,.env,~/.aws). - Prefer
askoverallowfor anything that mutates state, so a human sees it.
Scope tools by role, too: a researcher that reads the web and the repo gets no write, push, or network-egress tool; a reviewer is strictly read-only. The narrower each role, the smaller the blast radius when one is compromised.
🚦 Approval Gates That Resist Fatigue
Human approval is a real guardrail only if humans actually read what they’re approving. Two hundred prompts a day trains people to reflexively click “approve” — which is itself an attack vector (an agent using fluent, confident output to get a harmful action rubber-stamped). So design the gates for signal, not volume:
- Put human approval on the genuinely risky verbs — merges, deploys, secret access, production mutations, mass deletion, outbound comms to new destinations.
- Auto-handle the low-risk routine inside the sandbox so it never reaches a human.
- Surface the high-risk actions with a clear diff and enough context to actually judge them.
Fewer, higher-signal gates beat more gates. The point of sandboxing isn’t just isolation; it’s buying back your reviewers’ attention for the decisions that matter.
🪪 Identity and Audit You Can’t Rewrite
You cannot govern what you cannot attribute. Two requirements:
- Distinct agent identity. Every agent runs as its own non-human identity — never a human’s token, never one shared god-account across the fleet. A shared over-scoped account destroys attribution and hands an attacker a master key.
- A Triple-Identity, append-only audit. Every consequential action logs (User, Agent-version, Tool + params) — whose authority, which agent, what it did — to a store the agent itself cannot write to. And log intent, not just state: auditing an autonomous actor means answering “what was it trying to do,” not only “what errored.” If your audit lives somewhere the agent can rewrite, it isn’t an audit.
🛑 CI and Branch Protection: The Last Deterministic Gate
The final backstop is the one your human engineers already trust: agents open pull requests; they never push to protected branches. Require human review plus green CI on every agent change, and add a security-eval gate that blocks merges introducing injection-prone patterns. This is the hard line that makes every softer guardrail survivable — even if everything upstream fails, an agent still can’t land unreviewed code in main. It’s also where this Secure work rejoins the review-culture thread of the series: the human at the PR boundary is the last deterministic gate, by design.

⚠️ Guardrails That Don’t Actually Hold
For contrast, the controls that feel safe but aren’t:
- Prose as a security control. “Never read secrets” in CLAUDE.md loses to an injected instruction. Guidance is not a boundary.
- Blanket
--dangerously-skip-permissionsfor velocity. This is exactly the permission model that real-world agent incidents exploited. Use a sandboxed auto-mode instead. - One shared, over-scoped service account. Breaks attribution; one compromise is total.
- Stacking probabilistic filters and calling it defense. Two 90%-effective injection classifiers don’t compose to safety — the attacker adapts to whatever survives. You need at least one deterministic boundary, not more filters.
- A filesystem sandbox with no network egress control. Leaves the exfiltration leg armed; isolation without egress limits is half a control.
❓ Frequently Asked Questions
What’s the single most important AI agent guardrail? Sandboxing the executor with both filesystem and network isolation. It’s the one control that makes even a successful prompt injection non-catastrophic, and it reduces approval fatigue as a bonus.
Why aren’t strong system-prompt instructions enough? Because context is probabilistic. The model may not follow your instruction, and an injected instruction competes with it on equal footing. Only configuration-level controls — sandboxes, hooks, permissions, CI — are enforced regardless of what the model decides.
How do hooks differ from just writing rules in CLAUDE.md? A CLAUDE.md rule is guidance the model can ignore or be argued out of. A PreToolUse hook is code that blocks the action before it runs (exit 2 = deny) — it doesn’t negotiate with the model or an attacker.
Do these guardrails slow the agent down too much? Done well, the opposite. Sandboxing cut Anthropic’s permission prompts by 84%, because the deterministic boundaries handle routine safety silently — freeing humans to focus on the few high-risk gates that actually need judgment.
📚 Further Reading
- Secure Agentic Harness: The Lethal Trifecta at Fleet Scale — the why behind these controls: how private data + untrusted content + external comms become an exfiltration path across a fleet.
- MCP Server Supply Chain Security — hardening the skills, servers, and plugins that run with your agent’s privileges.
- The Agentic Development Loop, Explained for Engineers — where these guardrails sit in the overall loop.
- OWASP Top 10 for Agentic Applications (2026) — the ASI category framework these controls map to.
📚 Recommended Resources
Disclosure: some links below are Amazon affiliate links. If you buy through them, I earn a small commission — at no extra cost to you — which helps keep this site running.
Building Secure and Reliable Systems
Adkins, Beyer, Blankinship, Lewandowski, Oprea & Stubblefield · Book · ~$60 on Amazon
Google’s SRE-security team on designing systems where safety is a structural property, not a hope — least privilege, defense in depth, and blast-radius thinking. The exact mindset behind “the agent can’t,” not “the agent won’t.”
Designing Secure Software
Loren Kohnfelder · Book · ~$50 on Amazon
A practitioner’s guide to threat modeling and trust boundaries — the discipline of labeling what each component can touch, which is precisely how you scope an agent’s tools and reason about its blast radius.
AI-Assisted Programming
Tom Taulli · Book · ~$40 on Amazon
The practical end of running agents in a real workflow — where the sandboxes, permission gates, and human approvals fit into an everyday development loop rather than fighting it.