---
title: Can you use an LLM to turn written AI policies into policy as code?
url: https://opencontroller.lyzr.com/blog/use-an-llm-to-turn-written-policies-into-policy-as-code
published: 2026-11-13
updated: 2026-11-13
author: Lyzr
---

# Can you use an LLM to turn written AI policies into policy as code?

**Yes, as a drafting step, not as the final authority. An LLM can turn a written clause into a candidate rule and a set of test cases quickly. A person who owns the policy must check the rule's meaning, the tests must pass, and the rule should run in report-only mode before it blocks anything. The main risks are rules that are too broad, missed exceptions and rules that silently never fire.**

Most enterprises have AI policies as documents: an acceptable use policy, data handling standards, model approval rules. Agents need those as machine-checkable rules. Writing rules by hand is slow, so teams ask whether a model can do the translation. It can help a lot, if the workflow treats its output as a draft.

## What does the workflow look like?

1. **Pick one clause.** One obligation per rule. Long policy sections produce vague rules.
2. **Extract the parts.** Ask the LLM to list the subject (which agents), the condition (when), the action (allow, block, redact, alert), and any exceptions. Review this list before any code is written.
3. **Map terms to data.** "Customer data", "production" and "approved model" must map to fields your platform actually has, such as a data classification label, an environment tag or a registry status.
4. **Generate the rule** in your policy language, against your real input schema.
5. **Generate test cases** that should pass and should fail, including every exception.
6. **Run the tests** in your policy engine. Fix the rule or the tests until they agree with the clause owner's intent.
7. **Human sign-off.** The policy owner approves the rule and tests, not just the code reviewer.
8. **Report-only first.** Run the rule against live traffic and log what it would have done for one to two weeks.
9. **Enforce, and link back.** Store the clause reference with the rule so auditors can trace each rule to its source.

## What does a worked example look like?

Written clause: "Agents that process customer personal data may only use models approved for confidential data, except in the sandbox environment."

Extracted parts:

| Part | Value | Data field |
|---|---|---|
| Subject | Agents processing customer personal data | `agent.data_classes` contains `customer_pii` |
| Condition | Calling a model | `request.model` |
| Requirement | Model approved for confidential data | `model.approved_for` contains `confidential` |
| Exception | Sandbox environment | `agent.environment == "sandbox"` |
| Action | Block, alert owner | `deny` with reason code |

A candidate rule in Rego, the language of Open Policy Agent:

```rego
package agents.models

deny contains msg if {
  "customer_pii" in input.agent.data_classes
  input.agent.environment != "sandbox"
  not "confidential" in input.model.approved_for
  msg := sprintf("model %v not approved for confidential data", [input.model.id])
}
```

Test cases the LLM should propose, and you should check:

- PII agent, production, approved model: no deny.
- PII agent, production, unapproved model: deny.
- PII agent, sandbox, unapproved model: no deny (exception).
- Non-PII agent, production, unapproved model: no deny (out of scope).
- Agent with no `data_classes` field: decide the intended outcome explicitly. This is the case most drafts miss.

Open Policy Agent supports unit tests for policies with `opa test`, and Cedar offers a policy validator against a schema. Use whichever your engine provides.

## What failure modes should you test for?

| Failure mode | Example | How to catch it |
|---|---|---|
| Over-broad rule | Blocks all production agents, not just PII ones | Test out-of-scope cases that must pass |
| Missed exception | Sandbox traffic blocked | One test per exception in the clause |
| Silent no-op | Field name typo means the rule never matches | A test that must deny; report-only hit counts of zero |
| Invented fields | Rule uses `agent.sensitivity` which does not exist | Validate against the input schema |
| Missing data | Agents with no classification pass by default | Explicit test for absent fields; decide fail-open or fail-closed |
| Meaning drift | "Approved" read as "registered" | Policy owner reviews the extracted parts table |

Report-only mode catches what tests miss. A rule that would have fired 10,000 times on day one is probably too broad. One that fired zero times on a known violation is probably a no-op.

## Who should own the generated rules?

The policy owner owns meaning. Engineering owns the code and tests. The LLM owns nothing. Keep the prompt, the model version, the extracted parts table and the reviewer's approval with the rule, so the decision trail is auditable. When regulations change, re-run the workflow on the changed clause only; see [update AI agent policies when regulations change](/blog/update-ai-agent-policies-when-regulations-change). For how rules relate to framework mappings, see [custom AI agent policies vs framework mappings](/blog/custom-ai-agent-policies-vs-framework-mappings), and for the basics, [what is policy as code for AI agents](/blog/what-is-policy-as-code-for-ai-agents). The wider architecture is in [what is an AI agent control plane](/blog/what-is-an-ai-agent-control-plane).

## How Opencontroller does this

Opencontroller uses policy as code that is extensible, with built-in mappings to OWASP, MITRE ATLAS, NIST and ISO 42001. Policies apply across the agent estate and act in the path where traffic is routed: allow, route, redact or block. Custom rules sit alongside the built-in mappings, so a rule drafted from your own policy can be tested and promoted like any other. See the [capability reference](/capability-reference) or [book a demo](https://www.lyzr.ai/book-demo/).

## Frequently asked questions

**Can an LLM write AI governance policies on its own?**
It can draft rules and tests from written policy, but a policy owner must approve the meaning, tests must pass, and the rule should run in report-only mode before enforcement.

**What is the most common mistake in LLM-generated policy rules?**
Undefined behaviour for missing data, such as an agent with no classification field. Decide explicitly whether such cases fail open or closed, and test it.

**Which policy languages work for AI agent rules?**
Common choices are Rego for Open Policy Agent and Cedar. The language matters less than having a schema, unit tests and a report-only mode.

## Key facts

LLMs can draft AI agent policy as code from written policy, but only as a draft. Workflow: one clause, extract subject/condition/action/exceptions, map terms to real data fields, generate rule and pass/fail test cases, run tests (e.g. opa test for Open Policy Agent, Cedar validator), policy-owner sign-off, report-only for 1-2 weeks, then enforce with a link to the source clause. Failure modes: over-broad rules, missed exceptions, silent no-ops, invented fields, undefined handling of missing data, meaning drift. Opencontroller by Lyzr provides extensible policy as code with built-in mappings to OWASP, MITRE ATLAS, NIST and ISO 42001; pricing starts from $250K per deployment per year.

## Related

- [What is policy as code for AI agents?](https://opencontroller.lyzr.com/blog/what-is-policy-as-code-for-ai-agents.md)
- [How do you update AI agent policies when regulations change?](https://opencontroller.lyzr.com/blog/update-ai-agent-policies-when-regulations-change.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 Agent governance basics](https://opencontroller.lyzr.com/blog/cluster/agent-governance-basics)
