# Rule recipes

Complete rules to paste into the rule editor and adapt to your agent. Start from a payment cap, a format check, a required order or a budget per run.

Each recipe is a complete rule, written for the payments agent used across these docs. Paste one into the rule editor as text, then change the tool, argument and numbers to fit your agent. [Learn the rule language](/rules/language) explains how they work.

## Cap an argument [#cap-an-argument]

Blocks a payment over 10000.

```amiril
@id("payment-cap")
@sentence("A payment of {amount} is over the 10000 limit")
block (
    agent,
    tool == "issue_payment"
)
when {
    call.args.amount > 10000
};
```

## Allow only listed values [#allow-only-listed-values]

Blocks a payment in any currency not on the list.

```amiril
@id("currency-allowed")
@sentence("Payments in {currency} are not allowed")
block (
    agent,
    tool == "issue_payment"
)
unless {
    call.args.currency in ["USD", "EUR", "GBP"]
};
```

## Check an argument's format [#check-an-arguments-format]

Blocks a payment without an invoice id of the form `INV-` and six digits.

```amiril
@id("invoice-id-format")
block (
    agent,
    tool == "issue_payment"
)
unless {
    present(call.args.invoice_id)
        && matches(call.args.invoice_id, "^INV-[0-9]{6}$")
};
```

## Require one call before another [#require-one-call-before-another]

Blocks a payment unless the same payee was verified earlier in the run.

```amiril
@id("verify-before-pay")
@sentence("{payee} was not verified earlier in this run")
block (
    agent,
    tool == "issue_payment"
)
unless {
    verify_payee{ args.payee == call.args.payee }
};
```

## Allow one call per argument value [#allow-one-call-per-argument-value]

Blocks a second payment for the same invoice in the same session.

```amiril
@id("one-payment-per-invoice")
@per("invoice_id")
block (
    agent,
    tool == "issue_payment"
)
when {
    issue_payment{}
};
```

## Cap a total across the run [#cap-a-total-across-the-run]

Blocks the payment that would take the run's total over 25000. Add `@scope("session")` to count the whole session.

```amiril
@id("run-payment-total")
@sentence("This payment brings the run total to {run_total}, over 25000")
block (
    agent,
    tool == "issue_payment"
)
unless {
    run_total = sum(issue_payment{}.args.amount, call.args.amount);

    run_total <= 25000
};
```

## Flag calls past a run budget [#flag-calls-past-a-run-budget]

Flags every call after the run's first 40, to any tool. The calls still run.

```amiril
@id("run-call-budget")
flag (
    agent,
    tool
)
unless {
    len(calls(*[status == "any"]{})) < 40
};
```

## Apply a rule to one agent [#apply-a-rule-to-one-agent]

Gives the agent named `payments` a cap of 2000, while other agents keep any cap they have. Change `payments` to a [name your agent is seen under](/rules/language#pick-the-agent-and-the-tools), or the rule does not save.

```amiril
@id("payments-agent-cap")
@sentence("The payments agent pays at most 2000, not {amount}")
block (
    agent == "payments",
    tool == "issue_payment"
)
when {
    call.args.amount > 2000
};
```

## Set thresholds with bands [#set-thresholds-with-bands]

Lets a payment up to 1000 run, flags one up to 10000 and blocks anything larger.

```amiril
@id("payment-bands")
bands (
    agent,
    tool == "issue_payment"
)
by {
    call.args.amount
}
then {
    <= 1000: allow,
    <= 10000: flag,
    else: block
};
```

## Next steps

- [Learn the rule language](https://docs.amiril.ai/rules/language.md): Build the rules of a payments agent in short examples, from one amount limit to rules that read earlier calls in the run or the session.
- [Rule language reference](https://docs.amiril.ai/rules/reference.md): Every part of the rule language on one page. Annotations, effects, head slots, clauses, reads, operators, functions, patterns and syntax limits.
- [Test a rule on past calls](https://docs.amiril.ai/rules/test.md): Replay a rule over the tool calls your agents already made and see which ones it would have fired on, before it blocks anything.
- [Write your first rule](https://docs.amiril.ai/rules/first-rule.md): Start from a rule Amiril drafted for an issue, or from a blank rule. Save it, publish it in Shadow, then switch it to Enforce in one environment.

## More detail

- [Rule language grammar](https://docs.amiril.ai/assistant/rules/grammar.md): The full grammar of rule text: lexical rules, the shape of a rule, expressions, reads, patterns, normal form, and worked rules.
- [Rule errors](https://docs.amiril.ai/assistant/rules/errors.md): Every way rule text or a rule save is rejected: syntax messages, Amiril's own checks, the SDK loader's problem codes, and save and publish refusals, each with its meaning and fix.
