Examples
The examples/ folder has five complete programs. They run offline: none of them needs a model server or an API key. Run them from the root of the repository, after pip install -e ..
| file | what it shows |
|---|---|
01_quickstart.py | a guard in front of a refund tool, with no data |
02_learn_from_logs.py | collect logs, fit a learned guard, deploy it |
03_certified_plans.py | prove the risk of repair plans after a payment timeout |
04_openai_style_agent.py | the guard inside an OpenAI-style tool-calling loop |
05_anthropic_async_agent.py | async tools, the Anthropic message format, and a veto |
1. Quickstart
python examples/01_quickstart.py
A support agent refunds orders. Refunds need the user’s “yes”, may not pass $100, and may not be more than was paid. The guard first asks for a “yes”, then explains that $80 is more than was paid, then runs the $60 refund:
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 would exceed the $60.00 paid.
refunded $60.00 on order 42
refund {'order_id': 42, 'amount': 60.0} -> confirm_first stratum=confirm_only
refund {'order_id': 42, 'amount': 80.0} -> rule_feedback stratum=retry|eligibility
refund {'order_id': 42, 'amount': 60.0} -> execute stratum=retry|none
2. Learn from logs
python examples/02_learn_from_logs.py
A checkout agent retries a payment after a timeout. The rules allow the retry, so the nearest repair lets it run, and the customer is charged twice when the first attempt had gone through. The learned guard shows the agent the payment record first:
after a timeout, predicted harm per option: {'execute': 0.478, 'confirm_first': 0.436, 'verify_first': 0.215} | limit 0.193
nearest harm 0.195 safe completion 0.805
learned harm 0.000 safe completion 0.802
It writes the fitted guard to payment_guard.json in the folder you run it from. Learn from logs walks through the same program.
3. Certified plans
python examples/03_certified_plans.py
It bounds the risk of four repair plans after a payment timeout, with 300, 3,000 and 30,000 draws, and picks the closest certified plan. Certified plans explains the output.
4. An OpenAI-style agent
python examples/04_openai_style_agent.py
A telecom agent tries to resume a line whose contract has ended. The guard tells the agent the broken rule, and the agent hands the user to a person. A scripted model stands in for the language model. To use a real one, replace the model function with your client call.
user I paid my bill. Please go ahead and resume my line.
assistant Let me resume your line.
tool GUARD: not executed. The call violates policy:
- [contract_ended] the line's contract has ended.
assistant I can't resume it: the contract ended. Transferring you.
tool Transfer successful
assistant Done.
account: {'status': 'suspended', 'contract_end': '2025-01-31'}
5. An async Anthropic-style agent
python examples/05_anthropic_async_agent.py
The tools are async functions and the messages use tool_use and tool_result blocks. A veto adds a daily refund limit that is not part of the policy. The first refund runs. The veto stops the second one:
decisions: [('A7', 'execute'), ('B2', 'daily_limit')]
orders: {'A7': {'paid': 40.0, 'refunded': 40.0}, 'B2': {'paid': 300.0, 'refunded': 0.0}}