Dev.to AI ๐Ÿค– Ai ๐Ÿ‘ 0 ๐Ÿ“– 4 min read

Order your agent's policy checks so the strictest rule always wins

Run your agent's policy checks from the strictest layer to the loosest, and stop at the first layer that applies. That one rule removes most of the arguments about why an agent was allowed to do something it should not h

Run your agent's policy checks from the strictest layer to the loosest, and stop at the first layer that applies. That one rule removes most of the arguments about why an agent was allowed to do something it should not have.

This post shows a small version of that engine in TypeScript. It has five layers and returns one of three results: allow, ask a person, or deny. We are designing a product around this order, so the examples come from that work. Nothing here needs a framework.

The three results

Every tool call gets one answer.

  • allow: the call goes ahead.
  • checkpoint: the agent stops and files a request for a person to approve.
  • deny: the call is refused and the agent is told which rule refused it.

The result should always carry the rule that produced it. When someone asks "why did it stop here?", the answer is stored with the request, not rebuilt from memory later.

The five layers, in order

  1. The floor. A short list of actions that always need a person, whatever any policy says. Examples: moving money, deleting records the agent did not create, publishing publicly, messaging a stranger for the first time.
  2. Denies. A destination that is not on the allowlist, a scope that was never granted, or a call outside allowed hours.
  3. Per-delegation toggles. The person who handed over the job can tighten it: "ask before anything leaves the workspace", "ask before spending over $2".
  4. The agent's policy. Per-risk defaults set by an owner or admin.
  5. Budget. A checkpoint near the cap and a hard stop at the cap.

The order matters because each layer can only make the result stricter than the layer below it. If the floor says "ask", no later layer can turn that into "allow".

A minimal implementation

type Risk = "read" | "write" | "external_send" | "spend" | "destructive" | "admin";
type Verdict = { result: "allow" | "checkpoint" | "deny"; rule: string };

interface Call {
  risk: Risk;
  tool: string;
  destination?: string;
  scopes: string[];            // scopes this call needs
  firstContactOutsideWorkspace?: boolean;
  publishesPublicly?: boolean;
  costUsd: number;
}

interface Context {
  grantedScopes: Set<string>;
  allowedDestinations: Set<string>;
  askBeforeSending: boolean;       // delegation toggle
  askAboveUsd: number | null;      // delegation toggle
  agentDefaults: Record<Risk, "allow" | "checkpoint">;
  spentUsd: number;
  capUsd: number;
}

const FLOOR = (c: Call): string | null => {
  if (c.firstContactOutsideWorkspace) return "floor: first message outside the workspace";
  if (c.risk === "spend" && c.tool.startsWith("payments.")) return "floor: moves money";
  if (c.risk === "destructive") return "floor: deletes or overwrites";
  if (c.risk === "admin") return "floor: changes roles, scopes or policy";
  if (c.publishesPublicly) return "floor: publishes publicly";
  return null;
};

export function decide(call: Call, ctx: Context): Verdict {
  // 1. The floor
  const floor = FLOOR(call);
  if (floor) return { result: "checkpoint", rule: floor };

  // 2. Denies
  if (call.scopes.some((s) => !ctx.grantedScopes.has(s)))
    return { result: "deny", rule: "deny: scope not granted" };
  if (call.destination && !ctx.allowedDestinations.has(call.destination))
    return { result: "deny", rule: "deny: destination not on the allowlist" };

  // 3. Delegation toggles
  if (ctx.askBeforeSending && call.risk === "external_send")
    return { result: "checkpoint", rule: "toggle: ask before anything leaves" };
  if (ctx.askAboveUsd !== null && call.costUsd > ctx.askAboveUsd)
    return { result: "checkpoint", rule: "toggle: step over the spend threshold" };

  // 4. The agent's own policy
  if (ctx.agentDefaults[call.risk] === "checkpoint")
    return { result: "checkpoint", rule: `policy: ${call.risk} needs approval` };

  // 5. Budget
  const after = ctx.spentUsd + call.costUsd;
  if (after >= ctx.capUsd) return { result: "deny", rule: "budget: hard stop" };
  if (after >= ctx.capUsd * 0.8) return { result: "checkpoint", rule: "budget: 80 percent of the cap" };

  return { result: "allow", rule: "default allow within granted scopes" };
}

Four details that save you later

Make the floor impossible to loosen. Store it in code, not in a settings table. A workspace can add rules above it. Nothing, including an owner, can move below it.

Never let model output change the policy. Treat everything a tool returns as data. Text in an email or a web page must not be able to grant a scope or approve a request. Approvals should exist only as answers given by people.

Make the first run draft-only. For a new agent, ignore the agent's policy for the first delegation and treat every write as a draft. Open write scopes after a person has reviewed one full run.

Expire requests, and never read silence as yes. A checkpoint that nobody answers should escalate to the next approver and then pause the job. Seven days is a reasonable default expiry to start from.

Test it with a table

The engine is a pure function, so test it with a table of cases:

const cases: [string, Partial<Call>, string][] = [
  ["refund", { risk: "spend", tool: "payments.refund", scopes: [] }, "floor: moves money"],
  ["unknown host", { risk: "read", tool: "http.get", scopes: [], destination: "evil.test" }, "deny: destination not on the allowlist"],
  ["near cap", { risk: "read", tool: "notes.read", scopes: [], costUsd: 9 }, "budget: 80 percent of the cap"],
];

Add one row for every incident you ever have. The table is your policy history.

What to log

Log the call, the verdict and the rule, in an append-only table. The next post in this series covers a hash-chained version of that table in Postgres.

If you build something like this, the order is the part to get right first. The code is short. The discipline is in never letting a lower layer overrule a higher one.

๐Ÿ“ฐ Read the original article on Dev.to AI

Originally published by Dev.to AI. Aggregated on AIWithGhost for educational purposes โ€” full credit and traffic to the original publisher.