Article

Runtime policy enforcement for AI agents: tools, code and comparison

How to put a deterministic policy gate in front of every tool call, with working code in Python, Rego and Cedar and a comparison of approaches.

Published Updated 3 min read
Contents

A model cannot be relied on to refuse a harmful action every time. A policy gate can. The idea is simple: every tool call the agent proposes is turned into a structured request, evaluated against rules you wrote, and either allowed, denied or sent to a person. The model never touches the tool directly.

The shape of a policy gate

A gate needs four things:

  1. A structured request. Who is acting, on whose behalf, which tool, which arguments, in what context.
  2. A policy. Rules that map a request to a decision.
  3. An enforcement point. Code that sits on the only path to the tool and obeys the decision.
  4. A record. Every request and decision, logged.

The decision should have at least three outcomes: allow, deny and escalate (ask a human). Many teams add allow_with_limits, for example capping an amount.

A minimal gate in Python

Start without any policy engine. A plain function, a default-deny rule and a log line already put you ahead of most deployments.

from dataclasses import dataclass, field
from typing import Any, Literal
import json, logging, time

Decision = Literal["allow", "deny", "escalate"]
log = logging.getLogger("policy")

@dataclass(frozen=True)
class ToolRequest:
    agent_id: str
    on_behalf_of: str
    tool: str
    args: dict[str, Any]
    context: dict[str, Any] = field(default_factory=dict)

ALLOWED_TOOLS = {"search_docs", "read_ticket", "draft_reply", "issue_refund"}
REFUND_AUTO_LIMIT = 50  # in account currency

def decide(req: ToolRequest) -> tuple[Decision, str]:
    if req.tool not in ALLOWED_TOOLS:
        return "deny", f"tool {req.tool!r} is not on the allowlist"
    if req.context.get("tainted") and req.tool in {"issue_refund"}:
        return "escalate", "action influenced by untrusted content"
    if req.tool == "issue_refund":
        amount = float(req.args.get("amount", 0))
        if amount <= 0:
            return "deny", "refund amount must be positive"
        if amount > REFUND_AUTO_LIMIT:
            return "escalate", f"refund {amount} exceeds auto-approve limit"
    return "allow", "default rules passed"

def enforce(req: ToolRequest, call_tool, ask_human):
    decision, reason = decide(req)
    log.info(json.dumps({"ts": time.time(), "decision": decision, "reason": reason, **req.__dict__}))
    if decision == "deny":
        raise PermissionError(reason)
    if decision == "escalate" and not ask_human(req, reason):
        raise PermissionError(f"rejected by reviewer: {reason}")
    return call_tool(req.tool, req.args)

Note the tainted flag. Tracking whether untrusted content entered the context window, and tightening policy when it did, is one of the most effective injection defences available.

Moving rules into a policy engine

Once rules multiply, or once compliance wants to read them, move them out of application code. Two open-source engines are common choices.

Open Policy Agent (Rego)

package agent.tools

import rego.v1

default decision := {"result": "deny", "reason": "not allowed by any rule"}

allowed_tools := {"search_docs", "read_ticket", "draft_reply", "issue_refund"}

decision := {"result": "allow", "reason": "read-only tool"} if {
	input.tool in {"search_docs", "read_ticket", "draft_reply"}
}

decision := {"result": "allow", "reason": "small refund"} if {
	input.tool == "issue_refund"
	input.args.amount > 0
	input.args.amount <= 50
	not input.context.tainted
}

decision := {"result": "escalate", "reason": "large or tainted refund"} if {
	input.tool == "issue_refund"
	input.args.amount > 0
	some cond in [input.args.amount > 50, input.context.tainted == true]
	cond
}

Cedar

Cedar models the same rules as principal, action and resource, which maps naturally onto agent, tool, target.

permit (
  principal in AgentGroup::"support-agents",
  action == Action::"issue_refund",
  resource
)
when { context.amount > 0 && context.amount <= 50 && !context.tainted };

forbid (
  principal,
  action,
  resource
)
when { context.tainted && action in ActionGroup::"writes" };

Comparison of approaches

ApproachWhere rules liveLatencyWho can read the rulesGood forWatch out for
In-process checksApplication codeNegligibleEngineersFirst deployment, few toolsRules scattered across services
Policy engine library (embedded)Policy files in repoSub-millisecondEngineers, reviewersMany tools, testable rulesKeeping input schema stable
Policy engine sidecar or serviceCentral policy repo~1 ms on local networkEngineers, security, complianceMany agents, shared rulesOne more service to run
Tool gateway or proxyGateway configLowPlatform teamEnforcing on tools you don’t ownCoarse context about intent
Model-based judgeA prompt100 ms+Anyone, looselyFuzzy checks on contentNon-deterministic; not a hard boundary

A model-based judge is useful as an additional signal, for example to flag content for review, but should not be the only thing standing between an agent and an irreversible action.

Testing policies

Policies are code. Test them the same way, with a table of requests and expected decisions, run on every change:

- name: small refund is allowed
  request: { tool: issue_refund, args: { amount: 20 }, context: { tainted: false } }
  expect: allow
- name: tainted refund needs a human
  request: { tool: issue_refund, args: { amount: 20 }, context: { tainted: true } }
  expect: escalate
- name: unknown tool is denied
  request: { tool: delete_account, args: {}, context: {} }
  expect: deny

What to log

For every decision, record the full request, the decision, the reason, the policy version and, for escalations, who approved and when. That record is what an auditor, or you at 2 a.m., will need.

Get the next piece by email

New articles and whitepapers on controlling AI agents, with a short note on what changed in regulation and tooling. No promotions.

One email every two weeks. Unsubscribe in one click.