Content

Policy-as-Code for AI Agents: Allow, Deny, and Approve Rules in Git

How to write AI agent rules as code: allow, deny, approval, and response policies in a versioned file, reviewed in pull requests, tested in CI, and enforced before every tool call.

AI AgentOct 6, 2026

Key takeaways

  • -Policy-as-code means the rules for what an agent may do live in a versioned file that a runtime enforces before every tool call.
  • -Rules written in a system prompt can be ignored or overridden by prompt injection. Rules enforced outside the model cannot.
  • -Each policy has four parts: an id, the tools it targets, a condition, and an action such as block, require approval, modify, or redact.
  • -Treat policy like application code: write tests next to each rule, review changes in pull requests, and fail CI when a test breaks.
  • -Roll out new rules in monitor mode first, then enforce them once the audit log shows they catch the right calls.
  • -Deployed agents should be able to read policy and never change it.

You stop an agent from calling deletes or refunds by denying those methods in policy before the request is sent. A prompt is not a control. Approval for a refund above a threshold uses the same file. The flow is in human approval for agent tool calls.

How do I stop an AI agent from calling deletes or refunds?

Policy-as-code for AI agents means writing the rules for what an agent may do in a versioned file, reviewing changes in pull requests, testing them in CI, and having a runtime enforce them before every tool call. The model never sees or controls the rules. It proposes a call, and the policy decides whether that call runs, waits for a person, gets changed, or stops.

This guide covers why prompt-based rules fall short, what a policy file contains, a Git workflow with tests and CI, and the design rules that keep policies maintainable. Checked against the Swytchcode docs in October 2026.

Why aren't rules in the system prompt enough?

"Never refund more than $500" in a system prompt is a request. The model usually follows it, until a long conversation, a confusing tool result, or a prompt injection in an email it just read pushes it the other way. The OWASP Top 10 for LLM Applications (2025) lists prompt injection as the first risk and excessive agency as the sixth, and both point to the same fix: limit what the agent can do with controls outside the model.

Prompt rules also fail the audit test. When a security reviewer asks which agent can delete production data, "we told it not to" is hard to defend. A policy file in Git, with a commit history and tests, gives a direct answer.

What does an AI agent policy contain?

Every policy has four parts: an id, a target (the tools it guards), a condition that decides when it applies, and an action. The actions cover most of what teams need:

ActionWhat happensTypical use
POLICY_BLOCKEDThe call stops before any request is sentDeletes in production, payments over a hard limit
REQUIRES_APPROVALThe call waits until a person approves it in Slack or TelegramMid-size payments, emails to external domains
MODIFYArguments are changed, then the call continuesCapping a page size or list limit
ALLOWMarks a call as permitted when the file is an allowlistApproved operations only
RATE_LIMITED, QUOTA_EXCEEDEDBlocks calls over a count or total in a time windowDaily spend caps, message limits
REDACT, TRUNCATEHides or cuts parts of the response before the agent sees itCard numbers, tokens, very large lists

Allowlist or blocklist?

A blocklist lets everything run except what you name. An allowlist blocks everything except what you name. Allowlists are safer for agents because a new tool, or a tool the model invents a use for, is blocked until someone decides otherwise. The practical path is to start with a blocklist for known dangers, watch real traffic, then switch to an allowlist once you know which calls the agent needs.

A policy file with tests

Here is a Swytchcode policies.json in version-2 format. It caps daily payment volume per currency, blocks email to personal domains, and carries tests that describe what each rule should do. Replace <canonical_id> with the ID returned by swytchcode discover.

// .swytchcode/integrations/policies.json
{
  "version": 2,
  "lists": {
    "blocked_domains": ["gmail.com", "yahoo.com", "outlook.com"]
  },
  "policies": [
    {
      "id": "cap-daily-payments",
      "target": ["<canonical_id>"],
      "when": { "operator": "sum", "field": "amount", "window": "24h", "max": 50000, "per": ["currency"] },
      "action": { "type": "QUOTA_EXCEEDED", "message": "Daily volume of {{$sum}} {{currency}} would exceed {{$limit}}" }
    },
    {
      "id": "no-personal-email",
      "target": ["*"],
      "when": { "field": "$email_domains", "operator": "in", "value": { "$list": "blocked_domains" }, "ignore_case": true },
      "action": { "type": "POLICY_BLOCKED", "message": "Emails to {{$email_domains}} are not allowed" },
      "tests": [
        { "tool": "<canonical_id>", "args": { "to": "[email protected]" }, "expect": "block" },
        { "tool": "<canonical_id>", "args": { "to": "[email protected]" }, "expect": "allow" }
      ]
    }
  ]
}

A test can expect allow, block, approval, or modify, and redact or truncate for response policies. Tests run against the whole file the way a real call is decided, so a test fails if another policy blocks the call, and the failure names that policy. Tests never send a request, count toward limits, or write to the log.

A Git workflow for agent policies

  1. Keep policies.json in the same repo as the agent, next to the code it protects.
  2. Write one or more tests for every policy: at least one call it should stop and one it should allow.
  3. Run swytchcode policy validate locally. It checks the file, then runs the tests.
  4. Open a pull request. Reviewers read the policy diff the same way they read code.
  5. CI runs swytchcode policy validate. It exits with code 1 on any error or failing test, which fails the build.
  6. Ship new rules with "mode": "monitor". Matches are recorded but nothing is blocked.
  7. Check swytchcode audit policy. When the rule catches the right calls and nothing else, remove monitor mode to enforce it.
# .github/workflows/agent-policies.yml
name: agent-policies
on: pull_request
jobs:
  validate:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - run: npm install -g swytchcode
      - run: swytchcode policy validate

APIs change too. After swytchcode sync, get, or add, the same schema checks run again, because a provider update can remove or rename a field a policy depends on. You get a warning that points to policy validate before the rule quietly stops matching.

Who is allowed to change policy?

The agent being governed should never be able to edit its own rules. Swytchcode separates this with MCP profiles. A deployed agent connects with the agent profile and can read policies without changing them. The policy-writing tools exist only in the full profile, which you switch on in your editor while building. There, a coding agent such as Claude Code or Cursor can draft policies for you, check them against sample calls, and save them, with your editor asking you to approve every save or removal. Keep those two tools off any auto-approve list.

Design rules for maintainable policies

  • One rule per policy. Several small policies are easier to test and review than one with nested conditions.
  • Use patterns like "stripe.*" for provider-wide rules, and exact IDs for rules about one operation.
  • Write a message and a hint the agent can act on, such as "Use the customer's business email address".
  • Use active_from and expires_at for temporary rules such as a holiday change freeze, and remove them when they expire.
  • Add a notify alert to rules your team should hear about, such as blocked payments posted to a Slack channel.
  • Review the policy file whenever a new integration is added.

How Swytchcode enforces policy-as-code

Swytchcode is an execution kernel between your agents and the APIs they call. Request policies run after input validation and before credentials are attached, so a blocked call never loads a secret or touches the network. Response policies run after the call returns. Every decision, including blocks, approvals, modifications, and redactions, is written to a local audit log with request arguments redacted.

  • One policy file covers every agent in the project, whether it runs on LangGraph, CrewAI, the OpenAI Agents SDK, the Anthropic SDK, the Vercel AI SDK, or a coding agent over MCP.
  • The same rules apply to legacy and internal APIs you add from an OpenAPI spec.
  • Allow and deny rules are on every plan, including Free. Human approval is available on Business and Enterprise.

FAQ

How is this different from Open Policy Agent?

Open Policy Agent is a general-purpose policy engine. Agent policies need things specific to tool calls: matching on canonical tool IDs, built-in fields such as email domains, human approval, spend limits over time, and response redaction. You can run both, with OPA for infrastructure and an agent policy file for tool calls.

Can an AI coding agent write policies for me?

Yes. With the full MCP profile, Claude Code or Cursor can draft a policy from a plain-English request, test it against sample calls, and save it after you approve. The file still goes through your normal pull request review.

What happens if a provider renames a field my policy uses?

Schema checks run again after every sync, get, or add, and warn about fields the target tool no longer has. Run swytchcode policy validate to see the full list.

Do policies replace human approval?

Human approval is one policy action. Use a hard block for actions that must never happen, approval for actions that are fine after a check, and allow for everything routine.

How do I stop an AI agent from calling deletes or refunds?

Deny those methods in a policy file before the request is sent. A sentence in the system prompt does not stop the call. A refund above a threshold can require a person, using the same policy, as described in the human-approval guide.

Swytchcode resources

More content