Field notes
Deterministic guardrails for AI coding agents: intercepting tool calls in Rust
Why prompt instructions degrade over long agent sessions, and how a small Rust boundary intercepts tool calls, enforces safety policy, and pairs with CI entropy gates in production.
Most teams start hardening an AI agent the same way: they add another paragraph in ALL CAPS to the system prompt. Never drop a production table. Never force-push main. Never read secrets outside the workspace.
It works for the first ten turns. By turn forty, after tool outputs, stack traces, and retries fill the context window, a probabilistic model eventually treats a safety rule as a soft preference. If an action can destroy data, leak credentials, or rack up runaway spend, a prompt is not an access control.
To make AI coding and operational agents safe enough to run every day across production repositories, I moved safety enforcement out of the prompt and into a deterministic Rust binary — open-sourced as claude-harness and integrated with iron-stack.
The boundary: probabilistic reasoning, deterministic execution
An LLM is great at proposing the next step: picking a file to read, drafting a SQL migration, or composing a shell command. It is the wrong layer to judge whether that command is allowed to execute.
Instead of asking the model to police itself, every tool invocation crosses a deterministic hook before it touches the OS or database:
Agent proposes tool call (Bash / Edit / Write / MCP tool)
│
▼
Rust guardrail binary (PreToolUse stdin JSON)
├── Parse command / target path / arguments deterministically
├── Evaluate policy rules (allow / deny / require-approval)
└── Exit 0 (proceed) or return structured denial reason to agent
Because the binary is compiled Rust with no network calls and no model inference in the hot path, every check finishes in single-digit milliseconds. The agent never sees the check as latency; it simply hits a hard wall whenever it crosses a forbidden boundary and receives an actionable reason it can recover from cleanly.
Four failure modes prompts cannot stop
Running multi-model AI workflows (Anthropic, OpenAI, Google, Groq) and coding agents daily across real production services exposed four classes of failures that only deterministic checks stopped for good:
- Destructive git and filesystem shortcuts. When tests fail or a branch looks messy, an agent will occasionally try
git reset --hard,git push --force, orrm -rfon a dirty tree. Parsing the exact command tokens in Rust blocks destructive flags unconditionally unless the operator explicitly authorizes them. - Credential and environment boundary leaks. An agent debugging a deploy script can casually
cat .envor print bearer tokens into logs that get persisted in transcripts. Path-based rules deny reads on secret files before a single byte enters model context. - Silent code-quality drift. Even when tests pass, high-velocity AI edits tend to introduce copy-paste duplication, rising cyclomatic complexity, and circular module imports. Pairing the runtime guardrail with entropy-hunter — a deterministic code-health analyzer for Rust and TypeScript running as a CI regression gate — catches architectural decay before merge.
- Unbounded cost and irreversible mutations. In our production SaaS AI workflows inside Huggian, every tool-calling step runs under strict per-run cost caps and explicit human-in-the-loop checkpoints before mutating financial, inventory, or customer-facing records.
Why structured denials beat silent failures
A surprising lesson from operating claude-harness: how you block an agent matters as much as what you block.
If a hook crashes with a generic exit code, the agent retries the same broken command three times or tries to work around the error. When the Rust guardrail returns a concise, explicit policy message — naming the blocked operation and the sanctioned alternative — the model adjusts on the very next turn.
Pairing guardrails with adversarial verification
Blocking bad commands prevents disasters, but it does not prove a change is correct. In our workflow (claude-irondev), implementation and review are split across separate agent contexts:
- The implementer writes code and runs tests against real databases rather than mocked contracts.
- A fresh-context reviewer receives only the acceptance contract, diff, and raw command receipts — never the implementer’s self-assessment — and checks for contract regressions, silent fallbacks, and untested edge cases.
The combination is boring in the best way: fast AI-assisted iteration inside narrow, verifiable rails.
Where to start on your own agent stack
If your team already has an AI agent or MCP integration in staging or production, you do not need a six-month rewrite to harden it. Start with three concrete layers in one week:
- Put a deterministic policy check in front of every side-effecting tool call (shell, SQL, HTTP mutations, billing APIs).
- Enforce strict read/write path and tenant scopes outside the prompt.
- Add a CI quality gate and explicit cost/step budgets so runaway loops fail closed.
All three open-source components (claude-harness, iron-stack, and entropy-hunter) are MIT-licensed on GitHub, and if you want hands-on help hardening an agent stack quickly, I run a fixed-scope 5-day AI Agent & MCP Hardening Sprint.