# Rule language reference

Every part of the rule language on one page. Annotations, effects, head slots, clauses, reads, operators, functions, patterns and syntax limits.

Look up any part of the rule language here. [Learn the rule language](/rules/language) teaches the same parts with examples. A rule is written in this order:

```text
@annotation("value")
effect (head)
when or unless { condition };
```

A rule may also put a guard, `when { guard }`, before that last line. See [clauses](#clauses).

## Annotations [#annotations]

A rule takes each annotation at most once, before its effect.

| Annotation | Meaning |
| --- | --- |
| `@id("payment-cap")` | The rule's id. Lower-case letters, digits and single hyphens, up to 64 characters. Fixed once the rule is published |
| `@sentence("...")` | The sentence when the rule fires. `{name}` fills in an argument the rule reads or a named value |
| `@sentence_pass("...")` | The sentence when the call passes |
| `@scope("session")` | Earlier calls come from every run in the session. Without it, from this run |
| `@per("invoice_id")` | Earlier calls with the same `invoice_id`, from any run in the session |

A rule takes `@scope` or `@per`, not both.

## Effects [#effects]

| Effect | What happens to the call |
| --- | --- |
| `block` | It does not run. The model gets the rule's sentence in place of the tool's result |
| `flag` | It runs, and is flagged for review |
| `bands` | The rule measures the value in `by { }`. Each band in `then { }` acts with `allow`, `flag` or `block` |

A bands rule ends with `then { <= 1000: allow, <= 10000: flag, else: block }`. The numbers rise from the first band, and `else` takes the rest.

## Head slots [#head-slots]

The head comes in parentheses after the effect. Its slots pick the agents, the tools and the moment a rule checks.

| Slot | Covers |
| --- | --- |
| `agent` | Every agent. Leaving the slot out means the same |
| `agent == "payments"` | One agent, by a name Amiril already has for it |
| `agent in ["a", "b"]` | Several agents |
| `tool` | Every tool |
| `tool == "issue_payment"` | One tool |
| `tool in ["a", "b"]` | Several tools |
| `at == "before_call"` | Checked before the tool runs. Leaving the slot out means the same |
| `at == "after_call"` | Checked after the tool returns. `flag` only |
| `at == "run_end"` | Checked once when the run ends. `flag` only. No `tool` slot, and no reads of `call.args` or `call.result` |

## Clauses [#clauses]

| Clause | The rule fires when |
| --- | --- |
| `when { C }` | `C` is true |
| `unless { C }` | `C` is false |
| `when { G }` before a second clause | `G` is true and the second clause fires. A call where `G` is false is not checked at all |

A named value such as `payee = call.args.payee;` goes first inside the last clause's braces, before the condition. The lines below it can use it.

## Values a rule can read [#values-a-rule-can-read]

| Read | Value |
| --- | --- |
| `call.args.amount` | An argument of the call being checked. `call.args.payee.iban` reads a nested field |
| `call.tool_id` | The call's tool |
| `call.status` | `pending` before the tool runs. After it returns, `ok` or `error` |
| `call.result.x` | A field of what the tool returned, in a rule checked after the call |
| `prev.tool_id`, `prev.status`, `prev.args.x` | The call just before this one |
| `ctx.agent_id`, `ctx.agent_version` | The calling agent and its version |
| `ctx.run_id`, `ctx.session_id` | The run and the session of the call |
| `ctx.env.region` | A tag you pass to the SDK's `init` with `env`. See [SDK configuration](/reference/sdk-config) |
| `ctx.now` | The time of the call, in seconds since 1970 |
| `name` | A named value defined above |

## Operators [#operators]

| Operator | Meaning |
| --- | --- |
| `\|\|` `&&` `!` | Or, and, not |
| `==` `!=` `<` `<=` `>` `>=` | Comparisons. They do not chain, so write `0 < x && x < 10` |
| `in` | In a list such as `["USD", "EUR"]`. Lists take fixed values only |
| `+` `-` `*` `/` | Arithmetic |
| `( )` | Grouping |

## Functions [#functions]

| Function | Returns |
| --- | --- |
| `present(x)` | Whether `x` has a value. False when it is missing or `null` |
| `len(x)` | The length of a string or list |
| `abs(x)` | The absolute value |
| `min(a, b, ...)`, `max(a, b, ...)` | The smallest or largest value |
| `matches(x, "^INV-[0-9]{6}$")` | Whether the whole of `x` matches the regular expression |
| `starts_with(x, "INV-")` | Whether string `x` starts with the second string |
| `contains(x, "refund")` | Whether string `x` has the second string anywhere in it |
| `hour_utc(x)` | The UTC hour, 0 to 23, of a time such as `ctx.now` |
| `weekday(t, "Europe/London")` | The day of the week of time `t` in that time zone, from `"Mon"` to `"Sun"` |
| `hour(t, "Europe/London")` | The hour, 0 to 23, of time `t` in that time zone |
| `date(t, "Europe/London")` | The date of time `t` in that time zone, as `"2026-12-03"` |
| `time(t, "Europe/London")` | Time `t` in seconds since 1970, like `ctx.now`. `t` may be a string such as `"2026-12-03T17:30"`, and one without an offset is read in that time zone |
| `minutes(n)`, `hours(n)`, `days(n)` | That many minutes, hours or days, counted in seconds. `ctx.now + hours(2)` is two hours from now |
| `all(list, cond)`, `any(list, cond)` | Whether every item, or any item, meets `cond`. `it` is the item |
| `is_number(x)` | Whether `x` is a number |
| `is_string(x)` | Whether `x` is a string |
| `is_boolean(x)` | Whether `x` is `true` or `false` |
| `is_null(x)` | Whether `x` is `null` |
| `is_list(x)` | Whether `x` is a list |
| `is_object(x)` | Whether `x` is an object |

An argument the call leaves out or sends as `null` never makes a condition true. So `when { call.args.fee > 20 }` lets that call run, and `unless { call.args.fee <= 20 }` blocks it. Only `present()`, the `is_` functions, `==` and `!=` test for `null`.

`weekday()`, `hour()`, `date()` and `time()` need SDK 0.1.0 or later in either language. An older SDK skips a rule that uses them. If the rule is in Enforce and its **If the SDK cannot check a call** setting is **Stop the call**, the older SDK blocks every call the rule checks.

## Patterns [#patterns]

A pattern matches earlier calls. It names a tool, then optional slots in square brackets and a condition in braces. Inside the braces, a bare field such as `args.payee` or `result.registered` reads the earlier call. `call.` reads the current call.

### Tools in a pattern [#tools-in-a-pattern]

| Pattern | Matches earlier calls |
| --- | --- |
| `verify_payee{}` | To `verify_payee` |
| `verify_payee{ args.payee == call.args.payee }` | To `verify_payee` that meet the condition |
| `verify_payee\|lookup_payee{}` | To either tool |
| `*{}` | To any tool |
| `"Verify Payee"{}` | To a tool whose name is not a bare word |

### Pattern slots [#pattern-slots]

A slot goes in square brackets after the tool, as in `issue_payment[status == "error"]{}`. Without a status slot, a pattern matches only calls that succeeded.

| Slot | Matches |
| --- | --- |
| `[status == "error"]` | Only calls that failed |
| `[status == "blocked"]` | Only calls a rule blocked |
| `[status == "any"]` | Calls whatever their status |
| `[same == "invoice_id"]` | Only calls with the same `invoice_id` as the current call |

### Order, counts, totals and lookups [#order-counts-totals-and-lookups]

| Form | Value |
| --- | --- |
| `T{}` | Whether a matching call happened |
| `A{} -> B{}` | Whether every call matching `A` has a later call matching `B` |
| `latest(T{}).result.x` | A field of the most recent match. `first(T{})` takes the earliest |
| `every(T{}).args.x` | That field of every match, as a list |
| `calls(T{})` | The matches, for `len`, `all` and `any` |
| `count(T{})` | How many calls match |
| `sum(T{}.args.amount)` | The total of a field. Extra arguments, as in `sum(T{}.args.amount, call.args.amount)`, are added to it |
| `min(T{}.f)`, `max(T{}.f)`, `count_distinct(T{}.f)` | The smallest, the largest, and the number of different values |

## Syntax and limits [#syntax-and-limits]

- Strings take double quotes.
- Comments are not supported.
- A rule is at most 8,000 characters.
- Amiril saves the rule in one layout, so your text may come back spaced differently.

## 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 recipes](https://docs.amiril.ai/rules/recipes.md): 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.
- [How rules work](https://docs.amiril.ai/rules.md): A rule checks each tool call your agent makes and lets it run, flags it or blocks it. Rules run in your agent, and Shadow mode shows what one would do first.

## 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.
- [Rule settings, modes and publishing](https://docs.amiril.ai/assistant/rules/modes.md): Everything about a rule besides its text: mode, fail mode, if_missing, on_error, ids, versions, publishing, shadow records, backtests, permissions, and the MCP tools and routes for each.
