Policy as code is a familiar idea in infrastructure: access rules and compliance checks live in version control, are reviewed in pull requests, and are tested before they ship. Applying policy as code to AI agents solves a problem most teams hit within months — limits scattered across prompts and tool wrappers, with nobody sure which version is live. It's the engineering side of an AI agent policy.

The problem policy as code solves

Without it, agent limits end up in three places:

  • System prompts — "never refund more than $100" — which the model may ignore.
  • Tool wrappers — if amount > 100: raise — copied and slightly different in each tool.
  • People's heads — the support lead knows the real rule; the code doesn't.

Changing a limit means finding every copy, editing code and redeploying. And when something goes wrong, nobody can say which rule was in force at the time.

What policy as code for AI agents looks like

  • Rules live in one place, separate from agent code and prompts.
  • Rules are readable, so process owners can review them — not just engineers.
  • Every change is a version, with an author, a reviewer and a reason.
  • Decisions reference versions, so every allow or deny can be traced to the exact rule.
  • Rollback is one step, not a deploy.

A versioned change, end to end

  1. Finance asks to lower the auto-approve limit for refunds from $200 to $100.
  2. An engineer edits the rule and opens a change for review.
  3. A dry-run replays the last month of refunds and shows which ones would now be held — see how to test AI agent guardrails.
  4. The finance lead and engineer both approve.
  5. Version 2.4 goes live; version 2.3 stays one step away.
  6. Every decision from now on records refunds@2.4 in the audit trail.

General policy engines vs. agent-specific policy

General-purpose policy engines (Open Policy Agent is a well-known example) are excellent for authorisation in applications and infrastructure. Teams using them for agents usually find they still need to build: human approval routing with timeouts, replay of past agent activity, rules non-engineers can read, and a decision log linked to approvals. Whether you build that or use a dedicated layer, the principles are the same. We compare approaches on our why page.

Testing rules like code

Treat rule tests like unit tests: named examples that must always produce the same answer ("$150 refund → approval", "bulk export → deny"). Run them on every change, alongside a dry-run on real traffic. That combination catches both logic errors and unexpected impact.

Where Gaten fits

Gaten gives you policy as code for AI agents without building it: readable versioned rules, review, dry-run, one-step rollback, and every decision linked to the version that made it. See how policies work.

Frequently asked questions

What is policy as code?

Managing rules — access, compliance, limits — as versioned, reviewable, testable code instead of documents or hand-written checks.

Can I use Open Policy Agent for AI agents?

You can use a general policy engine to evaluate agent actions. You'll typically need to add approval workflows, replay testing and readable rules for business owners on top.

Who reviews agent policy changes?

The process owner (for example finance for refunds) and an engineer — one checks the limit is right, the other that it's implemented and tested correctly.