Compiling Policy into Code: A Deterministic Path for AI-Powered Review
Relying on a large language model to make on-the-spot compliance decisions leads to unstable output, no audit trail, and expensive rule changes. This article explains how to compile natural-language policy into a formal intermediate representation across three layers (terminology, facts, and rules), pair a rule engine for decisions with an LLM for semantic normalization behind four validation gates, emit explainable and auditable decision records, and manage policy changes through version control, golden test sets, coverage reports, and shadow runs. It also covers common pitfalls to avoid.
Introduction: Why “Let the LLM Decide on the Spot” Fails
In compliance review, content moderation, and credential verification, a common instinct is to hand the full policy text to a capable large language model and let it produce a verdict immediately. This path looks like the cheapest and fastest way to ship, but in real business scenarios it almost always runs into three problems.
The first is unstable output. The same material and the same policy can yield different conclusions across two calls; small changes in temperature, context length, or prompt wording introduce drift. The second is no traceability. When a review decision is challenged, you cannot point to “which article, which clause” it was based on, only offer a probabilistic natural-language explanation. The third is high cost of rule changes. Policies are revised every year, and maintaining them through prompts means every change requires re-running regression tests over all historical samples, and you have no test set.
The conclusion is clear: the core of policy review is not “understanding” but adjudication. Adjudication demands determinism, reproducibility, and auditability. So the right engineering direction is to “compile” policy into a program rather than have the model “guess”.
1. Treat Policy as Source Code: Three Levels of Formalization
The prerequisite for “compilation” is to first transcribe natural-language policy into a structured intermediate representation. In practice this can proceed in three progressive layers.
The first layer is the terminology layer, which binds vague expressions in the policy to computable definitions. Terms like “large amount”, “consecutive”, and “related party” must be pinned down to concrete thresholds, time windows, and decision criteria; otherwise every downstream rule is built on air. This layer produces a data dictionary, the asset most easily overlooked in the whole system yet most influential on accuracy.
The second layer is the fact layer, which abstracts the object under review into a unified fact model. Whether the input is a structured form, an uploaded document, or an external verification result, it is ultimately normalized into a set of fields with type and source markers. The source marker is critical because it determines priority when conflicts arise later.
The third layer is the rule layer, which expresses policy clauses as “condition-conclusion” production rules and explicitly declares priority, exceptions, and effective dates.
An illustrative rule expression (generic pseudocode here, not the interface of any specific product):
rule:id: R-2024-017version: 3effective_from: 2024-07-01priority: 90when:all:- fact.amount_gte: 500000- fact.related_party_ratio_gt: 0.3- fact.approval_level_lt: 2then:decision: REJECTreason_code: RELATED_PARTY_LIMITtrace: ["Clause 2.3", "Clause 5.1(b)"]exceptions:- rule_id: R-2024-031 # Public tender exemption
In this structure, the trace field is the foundation for all later explainability capabilities, and exceptions makes the common policy structure of “exceptions take precedence over general rules” explicitly modelable, rather than relying on the model to reason on the fly.
2. Hybrid Architecture: Determinism and Semantic Understanding Each Doing Their Job
Compilation does not mean completely excluding the large model. A sensible division of labor is: the model handles semantic normalization, the program handles adjudication.
The model takes on three kinds of tasks: extracting unstructured documents into fact fields; drafting natural-language clauses into rule candidates for human review before they enter the repository; and producing explanatory summaries for long-tail samples that cannot be matched. The program handles all decision output. This isolates the model’s uncertainty in the “input preparation” stage, and once it errs, it is intercepted at the fact validation step and does not directly contaminate the decision result.
When implementing, it is recommended to introduce four gates:
- Fact validation gate: checks field types, value ranges, required fields, and time consistency; if it fails, return for supplementary material rather than proceeding to adjudication.
- Rule matching gate: a rule engine evaluates by priority and effective version, outputting a decision and the matched rule chain.
- Conflict resolution gate: when multiple rules reach contradictory conclusions, arbitrate according to explicitly declared priority and exception relationships, and record the arbitration path.
- Human review gate: samples with low confidence, that hit an exception, or with insufficient rule coverage (no rule matched) are routed to humans, and are simultaneously captured as new rule candidates.
“No rule matched” must be treated as a first-class citizen. The system should rather output “cannot determine, route to human” than default to approval, this is a fundamental principle shared by compliance systems and safety systems: failure should lean conservative.
3. Explainability and Auditability: Turning the Reasoning Chain into a Data Structure
Half the value of a review system is the conclusion, and half is being able to explain how the conclusion was reached. Explainability should not be a paragraph generated after the fact, but a structured object naturally produced by the adjudication process:
{"decision": "REJECT","matched_rules": ["R-2024-017"],"superseded_rules": ["R-2023-088"],"facts_used": {"amount_gte": 620000,"related_party_ratio_gt": 0.34},"policy_version": "2024.2","engine_version": "1.9.0"}
With it, appeal handling becomes a query, a regulatory inspection becomes an export, and responsibility is defined with a clear basis. At the same time, the dual version numbers policy_version and engine_version make the question “which version of the policy was relied on at the time” always answerable.
4. Change Management and Regression Testing: The Biggest Dividend of the Compilation Approach
Policies will keep being revised, and this is precisely the biggest advantage of the compilation approach over the prompt approach. Rules are stored as text, naturally entering version control and going through a code review process: changes have diffs, approvals, and rollbacks.
Three kinds of supporting assets are needed. First, a golden sample set covering the hit, non-hit, and exception paths of every rule, with automatic full regression after rule changes; second, a coverage report tallying the trigger frequency of each rule and the distribution of unmatched samples, used to discover policy blind spots; third, shadow running, where the new rule version adjudicates in parallel with the old one, recording only differences without taking effect, and switching over after observing for a period.
In engineering, rule files, the fact model, and adjudication logic can be stored separately in object storage and configuration management components, the adjudication service deployed in a containerized manner, changes triggering cache refreshes via a message queue, and full-chain logs and metrics wired into logging services and monitoring/alerting systems, achieving canary release, observability, and fast rollback. What is described here is a general cloud architecture pattern; the specific implementation can be mapped to the capabilities of the chosen platform.
5. Common Pitfalls
- Pitfall one: A rule engine can replace the model. It cannot. Fact extraction from unstructured input still requires semantic capability; the rule engine only solves adjudication.
- Pitfall two: The finer the clauses are split, the better. Over-splitting leads to an explosion in the number of rules and out-of-control maintenance; the splitting granularity should be “independently adjudicable, independently referenceable”.
- Pitfall three: Ship first and build the test set later. A rule system without a golden sample set cannot be changed safely; the test set should be built in step with the very first rule.
- Pitfall four: Pursuing 100% automated adjudication. The reasonable goal is to clearly distinguish an automated zone, an assisted zone, and a purely manual zone, and to keep narrowing the boundary.
Conclusion
Faced with complex rules, the essence of compiling policy into a program is to trade engineering effort for determinism: normalize terminology first, validate facts first, hand adjudication to the rule engine, hand semantic understanding to the model, and leave a structured reasoning chain throughout. The reviewer produced this way does not “guess”; every conclusion it gives can point to a specific clause, a specific version, and specific facts, and it can evolve safely and regressively when policies are updated.
