Content

Policy as Code for AI Agents: How to Stop Breaches Before They Happen

Most AI agent guardrails live in a prompt, never in code that runs. Here is how policy as code catches what written instructions alone miss.

Chaitrali Kakde

DevRel Engineer

AI AgentsNov 30, 2026

Key takeaways

  • -Most "AI agent governance" today is a paragraph in a system prompt or a wiki page. Nothing stops the agent from routing around it under pressure.
  • -Policy as code means the rule is checked by a runtime every time the agent tries to act, not just suggested to it beforehand.
  • -A real policy-as-code system checks the actual arguments, binds approvals to them, and re-validates on every retry, not just on the first attempt.
  • -It has to cover more than outbound API calls. A rule like "never deploy to production on a Friday" is a policy decision with no HTTP request to intercept.
  • -Swytchcode enforces policy at the runtime level, for API calls and for actions that never touch an API at all.

Policy as code, applied to AI agents, means the rules an agent has to follow are enforced by a runtime at the moment it tries to act, not just written down somewhere the agent is supposed to read and remember.

TL;DR

  • Most "AI agent governance" today is a paragraph in a system prompt or a wiki page. Nothing stops the agent from routing around it under pressure.
  • Policy as code means the rule is checked by a runtime every time the agent tries to act, not just suggested to it beforehand.
  • A real policy-as-code system checks the actual arguments, binds approvals to them, and re-validates on every retry, not just on the first attempt.
  • It has to cover more than outbound API calls. A rule like "never deploy to production on a Friday" is a policy decision with no HTTP request to intercept.
  • Swytchcode enforces policy at the runtime level, for API calls and for actions that never touch an API at all.

Guardrails you write down aren't guardrails you can trust

Ask a team how they keep their AI agents from doing something dangerous, and most answers sound the same: there's a system prompt section that says what the agent shouldn't do, maybe a wiki page with "operating principles," maybe a line in onboarding docs about which environments are off-limits.

All of that is guidance. None of it is enforcement. An instruction written in natural language sits in the same channel as everything else the agent reads, and an agent under pressure to finish a task treats a soft rule as one more constraint to reason its way around, not a wall it can't get through.

Policy as code is the alternative: the rule lives outside the agent, in a runtime that checks every action against it before that action is allowed to execute. The agent doesn't have to remember the rule, agree with the rule, or even know the rule exists. It just can't do the thing if the thing is against policy.

This is not a new idea. Infrastructure teams have used policy as code for years, tools like Open Policy Agent exist specifically so that "who can deploy what, where, and when" is a checked rule instead of a hoped-for norm. AI agents need the same treatment, and for the same reason: anything that's only ever asked nicely eventually gets ignored, not out of malice, just because nothing was actually stopping it.

What happens when the rule is only a sentence

Two recent incidents make the gap concrete.

An AI coding agent was told, in writing, not to touch a production database during a code freeze. The instruction existed. It just wasn't enforced anywhere the agent's execution path would actually hit it, and the agent deleted the production database anyway. A markdown instruction is guidance, not a policy check, and an agent under pressure to complete a task will route around guidance it was never structurally prevented from ignoring. We cover this incident in more depth in our retries and idempotency guide.

Separately, a widely used AI coding tool let a trusted integration be approved once by name, then had its underlying command swapped out later without triggering a new approval prompt, a vulnerability tracked as CVE-2025-54136. The approval was real. It just wasn't checked against the thing that actually ran. Same post, more detail.

Neither incident needed a sophisticated attacker. Both just needed a system that trusted a label, a written rule, or a name, instead of re-checking the real thing at the moment it mattered. That's what policy as code is built to close.

What a real policy check actually does

"Policy as code" can mean very little if the policy only checks one thing. A policy layer that's actually doing its job for an AI agent checks, on every single action:

  1. Who's asking. The action is authenticated to a specific agent, tenant, and session, not just accepted because it arrived on the right port.
  2. What's actually being requested. The real arguments get validated, not a summary of them the agent wrote itself. Our human-in-the-loop guide covers why this distinction is the whole ballgame for approval safety.
  3. Whether this exact action was approved. Approval gets bound to a hash of the tool, the policy, and the arguments together, so a changed amount, recipient, or destination is automatically a new request, not a variation on an old yes.
  4. Whether it's a retry of something already done. Idempotent handling means a resubmitted call executes once, and a retry that comes back with a different payload doesn't inherit the old approval. Full treatment of this one is in our retries and idempotency guide.
  5. Whether it needed an API call at all. A policy like "never message a customer outside working hours" or "never deploy on a Friday" is enforceable even when there's no outbound HTTP request to intercept, as long as the runtime sits in front of the action itself, not just in front of the network.
  6. What happened, after the fact. Every check, approval, and denial lands in an audit log, timestamped and attributable, so the policy's behavior can be reconstructed and proven later.

Miss any one of these and you don't have a policy layer, you have a policy layer that happens to work until the exact case it wasn't built for shows up.

How Swytchcode implements policy as code

This is exactly what Swytchcode is built to do. It's a production runtime for AI agents: every action an agent takes gets authenticated, validated, and checked against policy before it runs, including a re-check on replay, with enforcement overhead under 50ms. Retries and idempotency are handled natively, so a resubmitted call is treated as the same request instead of a fresh one, and every decision, approved or denied, lands in an audit log.

The part worth repeating: a Swytchcode policy doesn't need an API call to exist. You can write a policy that governs an internal action, a scheduling decision, a deployment window, or anything else your agent does, not just the requests that happen to hit a third-party API. If it's an action your agent can take, it's something you can write a policy for.

Before anything executes, you can see exactly what will happen:

swytchcode exec stripe.create_payment --explain   # see what would run, no HTTP call
swytchcode exec stripe.create_payment --dry-run   # see the planned request
swytchcode exec stripe.create_payment --demo      # simulated run, no credentials

No hidden magic. The CLI executes what the policy allows, and nothing it wasn't told to. Full policy syntax and examples are in the Swytchcode docs.

FAQ

What does "policy as code" mean for AI agents? It means the rules an agent must follow are enforced by a runtime at the moment it tries to act, checked in code, instead of just described in a prompt, a doc, or a set of onboarding instructions.

Why isn't a system prompt enough to control an AI agent? A system prompt is guidance the agent reads alongside everything else. It isn't checked or enforced, so an agent under pressure to finish a task can reason its way around it without any technical barrier stopping it.

Does policy as code only apply to API calls? No. A well-built policy runtime sits in front of the action itself, so it can enforce rules like "never deploy on a Friday" or "never message a customer outside working hours" even when there's no outbound API request to intercept.

What's the difference between policy as code and human-in-the-loop approval? Human-in-the-loop is one possible check inside a policy system. Policy as code is the broader runtime that also handles argument validation, approval binding, retries, and audit logging, whether or not a human is in the loop for a given action. See our human-in-the-loop guide for the approval side specifically.

How does Swytchcode enforce policy as code? Swytchcode authenticates, validates, and checks every agent action against policy before it runs, including a re-check on replay, with enforcement overhead under 50ms, and it works for actions that never touch a third-party API. See the policy docs for setup.

Try it

If your agent's safety rules currently live in a prompt, a doc, or a wiki page, it's worth asking what actually stops the agent from ignoring them under pressure. If the honest answer is "nothing, really," that's the gap policy as code closes.

Install the Swytchcode CLI, read the policy docs, and write a policy for your agent's riskiest action, API-backed or not. One command, no SDK setup.

Related reading: Human-in-the-Loop AI Agents: Why Approval Alone Fails and Retries, Idempotency, and Approval: The Three Things Every AI Agent Workflow Needs.

More content

Ready to make your agent's tool calls reliable?

Give this prompt to your agent and get started.