Choose a mode

The mode says how the guard picks an option from the menu. You set it when you make the guard: Guard(policy, mode="nearest").

mode what the guard does when a call breaks a rule needs data?
off runs the call anyway (it still records which rules were broken) no
block denies the call no
feedback tells the agent which rules the call broke no
nearest picks the nearest repair: the closest option on the menu (the default) no
collect picks an option at random and records the exact chance of each pick no
learned picks with a guard that was fitted on collect logs yes

When no rule is broken, off, block, feedback and nearest run the call. collect and learned may still pick another option, such as showing the agent a record to check first.

See the difference

The same call under four modes. The user has not said “yes” yet:

from causalrepair import Guard, Policy, ToolCall, require_confirmation

policy = Policy(guarded={"refund"}).add(require_confirmation())
call = ToolCall("refund", {"order_id": "A1", "amount": 50})

for mode in ("off", "block", "feedback", "nearest"):
    session = Guard(policy, mode=mode).session()
    session.user("Refund order A1.")
    d = session.review(call)
    print(f"{mode:9s} picks {d.intervention:14s} runs: {d.allowed}")

It prints:

off       picks execute        runs: True
block     picks generic_deny   runs: False
feedback  picks rule_feedback  runs: False
nearest   picks confirm_first  runs: False

Which mode should I use?

Start with nearest. It needs no data, it never runs a call that breaks a rule, and it helps the agent go on with the task (it asks for a “yes”, fixes an argument, or explains the rule).

Use the others for these jobs:

  • off: measure how your agent does with no guard. The records still show which calls broke a rule (executed_violation is True for them).
  • block and feedback: simple baselines to compare with.
  • collect and learned: when some allowed options are safer than others and you want data to tell which one. See Learn from logs.

The two learned rules

A learned guard can pick in two ways. Choose with Guard(..., mode="learned", rule=...):

  • rule="risk" (the default): the closest option whose predicted harm is within a limit. The limit is the average harm that the fit predicts for the nearest repair.
  • rule="matched": stay as close as the nearest repair, and among options predicted to be no more harmful, pick the one with the best predicted safe completion.

Options offered only once

Some options make little sense twice in a row. With once=, an option is offered at most once for each tool in an episode. This limit applies to the nearest repair, to a learned guard and to the menu that a chooser gets. collect still picks from the full menu, but it records the limited menu, which is what the fit uses.

By default, the limit is verify_first only, or for a learned guard the setting saved in its artifact. The nearest repair still asks for a missing “yes” again: this limit never removes the step that the policy itself needs.

guard = Guard(policy, mode="nearest", once=["verify_first", "confirm_first"])