Agent Governance ToolkitAgent Governance Toolkit 4.1.0 · Python 3.10+
Dashboard
0%

Writing your own policy rules

A custom rule is a function you register with the engine that reads a call's arguments and returns True to allow it, so you can enforce a limit the built-in checks do not have, such as a cap on a refund amount.

Last updated: 14 Oct, 2026 · Agent Governance Toolkit 4.1.0

The Checking a tool's arguments lesson found the built-in checks only know a few tool names, and none of them reads a refund amount. This lesson writes that rule, with one wrong turn to avoid first.

The built-ins do not cap a refund

Allow the refund tool and try a tiny refund and a huge one. Both pass, because no built-in check reads the amount argument.

ExampleNo built-in reads a refund amount
from agent_control_plane import PolicyEngine

policy = PolicyEngine()
policy.add_constraint("support", ["refund"])

print("refund 40:  ", policy.check_violation("support", "refund", {"amount": 40}))
print("refund 9999:", policy.check_violation("support", "refund", {"amount": 9999}))

The field that looks like a cap but is never read

You might reach for RiskPolicy, whose fields read like approval thresholds. Setting max_risk_score and require_approval_above to a tight 0.1 changes nothing: the engine stores them and never reads them. Only deny_above is ever consulted. A request scored 0.8, far above the 0.1 these fields name, still passes.

Examplemax_risk_score and require_approval_above do nothing
from agent_control_plane.policy_engine import RiskPolicy

# max_risk_score and require_approval_above read like a hard cap at 0.1
policy.set_risk_policy("refunds", RiskPolicy(max_risk_score=0.1,
                                             require_approval_above=0.1,
                                             deny_above=1.0))

# a request scored 0.8 is far above 0.1, yet validate_risk allows it
print("0.8 allowed:", policy.validate_risk(make_request(9999), 0.8))

Those two field names are set in the library and read nowhere, so a limit built on them is no limit at all. The mechanism that works is a rule that reads the argument itself.

A rule that reads the amount

A custom rule is a PolicyRule around a function. The function gets the request and returns True to allow. action_types decides whether the rule is consulted, so a rule listing API_CALL never sees a file-read request. The request path works on an ExecutionRequest, built here by a small helper.

python
import datetime
from agent_control_plane.agent_kernel import (ExecutionRequest, AgentContext,
                                              ActionType, PolicyRule)

# the request path works on an ExecutionRequest, not a tool name and args
def make_request(amount):
    ctx = AgentContext(agent_id="support", session_id="s",
                       created_at=datetime.datetime.now())

    return ExecutionRequest(request_id="r", agent_context=ctx,
                            action_type=ActionType.API_CALL,
                            parameters={"amount": amount},
                            timestamp=datetime.datetime.now())

With the helper in place, write the cap, register it with add_custom_rule, and check a request through validate_request, which returns a pair: whether it passed, and a reason when it did not.

ExampleThe cap allows 40 and blocks 4000, naming the rule
def small_refunds_only(request):
    return request.parameters.get("amount", 0) <= 100          # True allows the call

rule = PolicyRule(rule_id="refund-cap", name="small_refunds_only",
                  description="refunds over 100 need a human",
                  action_types=[ActionType.API_CALL],
                  validator=small_refunds_only)
policy.add_custom_rule(rule)

for amount in [40, 4000]:
    print(amount, policy.validate_request(make_request(amount)))

The reason names the rule, which is what makes a refusal readable months later when thirty rules are loaded and someone asks why a refund did not go through.

The mistake this lesson exists to prevent

Register the same rule, then ask the gate from the choke-point lesson instead of validate_request. The four-thousand refund is allowed, by an engine holding a rule that forbids exactly that.

ExampleThe gate ignores the custom rule entirely
from agent_control_plane import AgentKernel
policy.add_constraint("support", ["refund"])
kernel = AgentKernel(policy_engine=policy)

# the same 4000 refund, asked through the gate rather than validate_request
print(kernel.intercept_tool_execution("support", "refund", {"amount": 4000}))

A custom rule runs only through validate_request, not through the gate a tool-calling agent uses. For a limit on the gate path, write it as a policy-file rule or as a conditional permission, which Conditions on a tool's arguments covers.

The two paths a limit can live on

Reads a refund amountChecked by the gate
Built-in argument checkNo, no built-in reads itYes, on the interception path
RiskPolicy fieldsNo, the fields are never readNo
Custom PolicyRuleYes, your function reads itNo, only through validate_request
Conditional permissionYesYes, on the interception path

Where a custom rule fits

  • A limit the built-ins miss, such as a cap on an amount, a quantity or a date, read straight from the arguments.
  • A check you run on a request object, where your code already builds an ExecutionRequest for the quota path.
  • A rule you want named in the refusal, so an audit of a blocked call shows which rule stopped it.
Watch out. A custom rule that seems not to run is usually watching the wrong action type, or being asked on the gate path that never consults it. Rules are sorted by priority, highest first, and the first that returns False ends the check.
Try it yourself
  • Lower the cap to 10 and confirm a 40 refund now returns a refusal that names the rule.
  • Change action_types to [ActionType.FILE_READ] and watch the rule stop running on an API call.
  • Add a second rule with a higher priority that allows everything, and see whether the cap still fires.

Little by little, you're building something great.