How it works

This page explains what the guard does with each tool call, and the words the rest of these pages use.

What happens to one tool call

  1. The agent proposes a tool call, for example refund(order_id="A1", amount=60).
  2. If the tool is not guarded, the call runs at once.
  3. Otherwise the guard checks the call against every rule of the policy. The result is a list of broken rules (it can be empty).
  4. The guard makes a menu: the options that break no rule.
  5. The guard picks one option from the menu. How it picks depends on the mode.
  6. If the option runs the call, the tool runs. If not, the agent gets a message in place of the tool’s result.
  7. The guard writes a record of what it decided.

The options

These are the six built-in options. You can add your own (More options).

option what happens runs the call? distance
execute the call runs as the agent proposed it yes 0
arg_repair the call runs with fixed arguments yes 0.5
confirm_first the agent is told to get a clear “yes” from the user first no 2
verify_first the agent is shown a trusted record to check first no 2
rule_feedback the agent is told which rules the call broke no 2
generic_deny the agent is told only that the call was denied no 2

The distance says how far an option moves away from what the agent asked for. Running the call as asked is the closest (0). Fixing an argument is a small change (0.5). Not running the call is the biggest change (2).

The menu

The menu holds only the options that break no rule:

  • execute is on the menu only when no rule is broken;
  • arg_repair is on the menu only when every broken rule offers a fix;
  • verify_first is on the menu only when you give the guard a trusted read (Guard(verify=...));
  • confirm_first, rule_feedback and generic_deny never run the call, so they are always on the menu (unless you leave them out with Guard(options=...)).

So whatever the guard picks, a call that breaks a rule never runs. The one exception is mode="off", which runs every call and is only meant for measuring how the agent does with no guard. (A repaired call is checked again before it runs. A policy built with recheck_repairs=False skips that check: use it only when every repair passes every rule by construction.)

The nearest repair

The nearest repair is the closest option on the menu. It is the default way to pick, and it needs no data:

  1. run the call, if no rule is broken;
  2. else run it with fixed arguments, if every broken rule offers a fix;
  3. else, if the only problem is a missing “yes”, ask for confirmation;
  4. else tell the agent which rules it broke.

Look at one decision

session.review(call) shows what the guard decides, without running anything:

from causalrepair import Guard, Policy, ToolCall, require_confirmation

policy = Policy(guarded={"refund"}).add(require_confirmation())
session = Guard(policy).session()
session.user("Refund order A1, please.")

d = session.review(ToolCall("refund", {"order_id": "A1", "amount": 60}))
print("broken rules:", [v.rule for v in d.violations])
print("menu:", d.menu)
print("picked:", d.intervention, "| runs:", d.allowed)
print("situation:", d.stratum)

It prints:

broken rules: ['explicit_confirmation']
menu: ['confirm_first', 'rule_feedback', 'generic_deny']
picked: confirm_first | runs: False
situation: confirm_only

review also writes a record. Use it on its own only when you then run the call yourself or drop it (Connect your agent).

The words

Tool call (ToolCall). A tool name and its arguments: ToolCall("refund", {"order_id": "A1", "amount": 60}).

Guarded tool. A tool that the guard checks. You list them in Policy(guarded={...}). With guarded=None, every tool is checked.

Policy, rule, violation. A policy is a list of rules. A rule is a function rule(call, trace) that returns nothing (fine) or a message (broken). A broken rule is a violation (Violation). It has the rule’s name, a message, a category and, if the rule can fix the call, the fixed arguments.

Trace. What the guard knows about the conversation: the user’s messages, the calls that ran, their results, and its own decisions. The guard trusts the user’s messages, the tool results and its own records. It never takes what the agent says as a fact.

Context. Facts that your application knows, such as who is logged in. You set them when you start a session: guard.session(customer_id="ann"). Rules read them as trace.context.

Status of a result. Each result is ok, error, or unknown. unknown means the call may or may not have taken effect, for example after a timeout.

Situation (stratum). A short label for the kind of decision. The guard uses it to learn from data. It is made of these parts, joined by |:

  • retry (written retry| at the start), when the guard already stopped an earlier call to the same tool;
  • the domain, if you set one (Guard(domain="shop"));
  • the tool class, if you set one (Guard(tool_class=...));
  • after_unknown, when the last call to the same tool had an unknown result;
  • the violation class: what kind of rules are broken.

Violation class. One word for the broken rules:

class meaning
none no rule is broken
confirm_only only the “yes” is missing
repairable_arg only argument rules (and maybe the “yes”) are broken, and every argument rule offers a fix
identity, eligibility, other, … otherwise: the first of identity, eligibility, other among the broken rules’ categories, or else the category name that comes first in alphabetical order

Episode. One full conversation or task. When it ends, you can close it with session.finish(harm=..., success=...).

Harm, success, safe completion. The outcome of an episode, each between 0 and 1. Harm is 1 if something bad happened (for example, a customer was charged twice). Success is 1 if the task was done. Safe completion is success * (1 - harm): the task was done and nothing bad happened.

Veto. Your own extra check that runs after the guard has decided, such as a human approval. A veto can stop a call. It can never run a call that the guard stopped. See More options.