causalrepair
A safety guard for AI agents that use tools.
Paper: Policy-Constrained Causal Repair for Tool-Using Agents (arXiv) · Code: github.com/vicky157/causal-repair
An AI agent can call tools. It can refund an order, send a payment or restart a service. Some of these calls are not safe. causalrepair sits between the agent and its tools. It checks every call against rules that you write.
When a call breaks a rule, the guard does not only say “no”. It repairs the call. It can:
- fix a wrong argument, for example a refund that is too large;
- ask the agent to get a clear “yes” from the user first;
- show the agent a trusted record to check;
- tell the agent which rule the call broke.
The agent reads the guard’s answer as the tool’s result, and it can try again.
The guard works with any agent whose tool calls you can catch. You do not need a special framework, model or company. The guard itself is plain Python. It does not call a language model and it does not send data anywhere.
Install
pip install git+https://github.com/vicky157/causal-repair.git
You need Python 3.10 or newer. The only other packages it needs are numpy and scipy.
A first example
A shop agent can refund orders. We want two rules: the user must say “yes” first, and a refund may not be more than the customer paid.
from causalrepair import Guard, Policy, require_confirmation
orders = {42: {"paid": 60.0}}
def refund(order_id, amount):
return f"refunded ${amount:.2f} on order {order_id}"
policy = Policy(guarded={"refund"}) # the guard checks every call to "refund"
policy.add(require_confirmation()) # rule 1: the user must say "yes" first
@policy.rule(tools={"refund"})
def not_more_than_paid(call, trace): # rule 2: no refund above what was paid
if call.args["amount"] > orders[call.args["order_id"]]["paid"]:
return "the refund is more than the customer paid."
guard = Guard(policy) # the default mode needs no data
session = guard.session() # one session for each conversation
tools = session.tools({"refund": refund}) # wrapped tools: every call now passes the guard
session.user("Please refund order 42.") # tell the guard what the user says
print(tools["refund"](order_id=42, amount=60))
session.user("Yes, go ahead.")
print(tools["refund"](order_id=42, amount=80))
print(tools["refund"](order_id=42, amount=60))
It prints:
GUARD: not executed. Before calling refund, list the exact action details to the user and obtain an explicit 'yes'. Then you may call the tool again.
GUARD: not executed. The call violates policy:
- [not_more_than_paid] the refund is more than the customer paid.
refunded $60.00 on order 42
The first call did not run, because the user had not said “yes”. The second call did not run, because $80 is more than the $60 that was paid. The third call broke no rule, so it ran. In a real agent, you give each of these answers back to the model as the tool’s result.
Where to go next
| I want to … | Read |
|---|---|
| set up a guard step by step | Get started |
| understand the main ideas and words | How it works |
| write my own rules | Write a policy |
| connect my agent (OpenAI style, Anthropic, async) | Connect your agent |
| choose how the guard decides | Choose a mode |
| learn from data which repair is safest | Learn from logs |
| prove a risk limit in a simulator | Certified plans |
| add my own checks, options and messages | More options |
| run the ready-made examples | Examples |
| run the paper’s benchmarks | Run the benchmarks |
| look up a class or function | API reference |
| fix a problem | Questions and fixes |
| see how the repository is organized | Repository guide |
Cite
If you use causalrepair, please cite the paper:
@article{singh2026causalrepair,
title = {Policy-Constrained Causal Repair for Tool-Using Agents},
author = {Singh, Vikash and Ganguly, Debargha and Chen, Weicong and Goel, Aman and Zhang, Yanyan and
Hariri, Mohsen and Sankar, Sreehari and Song, Chaoda and Shao, Yuting and Nemecek, Alexander and
Zafar, Osama and Ye, Kai and Zhang, Biyao and Li, Xinpeng and Wang, Shouren and Yang, Wang and
Ayday, Erman and Ma, Jing and Chaudhary, Vipin},
journal = {arXiv preprint arXiv:2612.00565},
year = {2026}
}