---
title: What is policy as code for AI agents?
url: https://opencontroller.lyzr.com/blog/what-is-policy-as-code-for-ai-agents
published: 2026-11-13
updated: 2026-11-13
author: Lyzr
---

# What is policy as code for AI agents?

**Policy as code for AI agents means writing the rules agents must follow (which models, tools, data and spend they may use) as versioned files that a machine evaluates on every request, instead of as a PDF that people interpret. Each rule has an owner, test cases and a change history, so you can prove what was enforced and when.**

It is the same idea infrastructure teams adopted for cloud: a rule lives in Git, gets reviewed in a pull request, and runs in the request path. For agents, the "request path" is the model call, the tool call or the agent-to-agent call.

## Why do agents need policy as code rather than written policy?

Written AI policies tell people what to do. Agents are not people. They call models and tools thousands of times a day, and nobody reads a policy document before each call.

Three things break when the policy stays on paper:

1. **No enforcement point.** A rule such as "no customer PII to external models" is only true if something checks each prompt.
2. **No evidence.** An auditor asks "show me the rule was applied in March". A document cannot answer that. A decision log can.
3. **No consistency.** Ten agent platforms interpret one policy ten ways. A single policy file evaluated in one place gives one answer.

## What does an agent policy look like in plain form?

Most policy engines use a declarative language. [Open Policy Agent](https://www.openpolicyagent.org/docs/latest/) uses Rego; [Cedar](https://www.cedarpolicy.com/) is an open-source language that AWS uses in Amazon Verified Permissions. Whatever the syntax, a good agent policy reads like this in plain form:

| Policy | Condition | Decision | Severity |
|---|---|---|---|
| Approved models only | Agent calls a model not on the approved list | Block | High |
| PII to external model | Prompt contains a payment card or national ID pattern and target is a third-party API | Redact, then allow | High |
| Tool scope | Agent in risk tier 3 calls a tool that writes to a system of record | Block and alert | Critical |
| Budget | Agent version exceeds its monthly token budget | Route to cheaper model | Medium |
| Unregistered agent | Traffic from an identity with no registry entry | Allow, flag for review | Low |
| Groundedness | Response scores below threshold on a groundedness check | Alert owner | Medium |

Notice that the decision is not always "block". Agent policies usually need at least four outcomes: allow, route, redact and block. Some also need "alert only" for a first rollout.

## How do you test an agent policy before it goes live?

Treat every policy as code with unit tests. For each rule, write at least three cases:

1. **A should-pass case.** An approved agent calling an approved model with a clean prompt.
2. **A should-fail case.** The same agent sending a test card number to an external model.
3. **An edge case.** A prompt that mentions "card" but contains no card number, so you catch false positives.

Then run the policy in **observe mode** against real traffic for a set period (one to two weeks is common) and compare what it would have blocked against what actually happened. Only switch to enforce once the false-positive rate is acceptable to the agent owners.

See [how to set severity levels for AI agent policies](/blog/how-to-set-severity-levels-for-ai-agent-policies) for a way to decide which rules block and which only alert.

## What does the review workflow look like?

A workable workflow has six steps:

1. **Propose.** Anyone opens a pull request that adds or changes a policy file and its tests.
2. **Map.** The author tags the policy with the framework controls it supports, for example an OWASP agentic risk or a NIST AI RMF function.
3. **Review.** A second person from security or risk approves. The author cannot approve their own change.
4. **Test.** CI runs the unit tests and a replay against a sample of recent traffic.
5. **Roll out.** Deploy in observe mode, then enforce, per environment.
6. **Record.** Every decision the engine makes is logged with the policy version that made it.

Step 6 is what turns policy into evidence. When an auditor asks how a rule behaved on a given date, you show the policy version and the decision log for that day.

## Where should agent policies be enforced?

Policy as code only matters if something sits where it can act. Typical enforcement points are:

- **LLM gateway** for model choice, prompt redaction, budgets and routing.
- **Agent or MCP gateway** for tool calls and agent-to-agent calls.
- **The agent platform itself** where it exposes controls, through a connector.
- **Developer devices** for coding agents and local MCP servers.
- **Quarantine and kill controls** for agents that break policy repeatedly. See [how to stop a rogue AI agent](/blog/how-to-stop-a-rogue-ai-agent).

If a policy has no enforcement point, label it "detect only" so nobody assumes it blocks anything. Our post on [documenting AI traffic paths before enforcement](/blog/document-ai-traffic-paths-before-enforcement) explains how to find those points.

## How Opencontroller does this

Opencontroller supports policy as code that is extensible and comes with built-in mappings to OWASP, MITRE ATLAS, NIST and ISO 42001. Policies apply across the agent estate and are enforced in the path through the LLM Gateway and Agent Gateway (allow, route, redact, block), or through per-agent Block and Quarantine controls that act via the connected platform. How deep enforcement goes depends on the connector and traffic path: registering an agent is not the same as taking over its runtime. For the wider model, read [what an AI agent control plane is](/blog/what-is-an-ai-agent-control-plane), or [book a demo](https://www.lyzr.ai/book-demo/).

## Frequently asked questions

**Is policy as code the same as guardrails?**
No. Guardrails are checks on content, such as groundedness or prompt injection detection. Policy as code is the rule layer that decides what happens when a check fires, plus rules that have nothing to do with content, such as model allow-lists and budgets.

**Which policy language should we use for agents?**
Use whatever your enforcement points support. Rego (Open Policy Agent) and Cedar are both open and widely documented. Consistency matters more than the language: one source of truth, versioned and tested.

**How many policies should we start with?**
Start with five to ten that cover approved models, sensitive data, tool scope, budgets and unregistered agents. Run them in observe mode first, then add rules as incidents and audits show gaps.

## Key facts

Policy as code for AI agents means expressing rules for agents (approved models, sensitive data handling, tool scope, budgets) as versioned, tested files evaluated on every model, tool or agent-to-agent call. Decisions include allow, route, redact and block. Open Policy Agent (Rego) and Cedar are common open policy languages. Opencontroller by Lyzr supports extensible policy as code with built-in mappings to OWASP, MITRE ATLAS, NIST and ISO 42001, enforced through its LLM Gateway and Agent Gateway or per-agent Block and Quarantine controls. Pricing starts from $250K per deployment per year.

## Related

- [How should you set severity levels for AI agent policies?](https://opencontroller.lyzr.com/blog/how-to-set-severity-levels-for-ai-agent-policies.md)
- [How do you document which AI traffic paths your gateway actually controls?](https://opencontroller.lyzr.com/blog/document-ai-traffic-paths-before-enforcement.md)
- [Should you write custom AI agent policies or rely on framework mappings?](https://opencontroller.lyzr.com/blog/custom-ai-agent-policies-vs-framework-mappings.md)
- [More on Runtime control](https://opencontroller.lyzr.com/blog/cluster/runtime-control)
