Rules reference
A condition normally asks an agent to choose which way the playbook goes. A condition can instead decide by rules: you write, on each line leaving it, a short expression that is true or false for the data the run was started with. The run follows the lines whose rule is true. No agent is involved, so the same input always gives the same answer, and the choice costs no credits.
This page is the whole definition. It is written to be read by a person or by an AI that is writing rules for you: the syntax, what happens when data is missing, the limits, and every message you can get with its fix.
In one minute
- Set
"decide_by": "rules"on a condition. It has no agent and needs no text. - Every line leaving it has either a
whenrule or"otherwise": true. - A rule is an expression over
input, the data the run was started with, for exampleinput.amount > 500 && input.currency == "RSD". - A line is followed when its rule is true. Any number of lines may be.
otherwiseis followed only when no rule is true. - A rule that cannot be checked, such as one that reads a field the input does not have, fails the run. It never quietly takes
otherwise.
{
"nodes": [
{ "id": "read", "kind": "instruction", "title": "Read the invoice", "text": "Read the invoice.", "agent_uuid": "..." },
{ "id": "route", "kind": "condition", "title": "Where does it go?", "decide_by": "rules" },
{ "id": "sef", "kind": "instruction", "title": "Send to SEF", "text": "Send the invoice to SEF.", "agent_uuid": "..." },
{ "id": "pdf", "kind": "instruction", "title": "Make a PDF", "text": "Make a PDF of the invoice.", "agent_uuid": "..." }
],
"edges": [
{ "from": "read", "to": "route" },
{ "from": "route", "to": "sef", "name": "sef", "when": "input.amount > 500 && input.currency == \"RSD\"" },
{ "from": "route", "to": "pdf", "name": "pdf", "otherwise": true }
]
}
When to use rules instead of the agent
| Use rules when | Use the agent when |
|---|---|
| The answer follows from fields you know in advance: an amount, a country, a flag, a status. | The answer needs judgement: tone, urgency, whether two documents describe the same thing. |
| It must come out the same every time for the same data. | A small difference in wording should be allowed to change the answer. |
| You want no cost and no wait for the choice. | The deciding information is in text the agent has to read, or in what earlier steps produced. |
A rule sees only the run's input. It does not see what earlier steps wrote. If the deciding fact is produced by a step, have that step's agent decide (a condition with an agent), or start the run with the fact in the input.
How the input reaches rules
input is the JSON you give when you start a run. It is the same input the agents read as data.
With the toolkit: ptkl kortex playbook run -u <uuid> --input invoice.json.
- Inside a rule,
inputis that JSON. Read a field asinput.amount, a nested field asinput.customer.country, a list item asinput.items[0]. - A run started with no input has an empty
input: every field is missing (see below). - Rules read the input as it was when the run started. It does not change during the run.
- JSON numbers are numbers, text is text,
trueandfalseare booleans, lists and objects keep their shape. A date is text in JSON, so it compares as text.
Writing a rule
Rules use CEL syntax. A rule must give true or false. The language only reads and calculates: it cannot call anything outside, change data, or loop forever.
Comparing
| Operator | Meaning |
|---|---|
==, != |
Equal, not equal. Text is compared exactly, including capitals. |
<, <=, >, >= |
Order, for numbers and for text. |
Whole and decimal numbers compare with each other: input.amount > 500 works whether the amount is 800 or 500.5.
Combining
| Operator | Meaning |
|---|---|
&& |
And. |
\|\| |
Or. |
! |
Not. |
( ) |
Group. && binds tighter than \|\|. |
a ? b : c |
If a is true, b, otherwise c. |
Lists, fields and size
| Expression | Meaning |
|---|---|
input.country in ["RS", "ME"] |
The value is one of the list. |
"vip" in input.tags |
The list contains the value. |
has(input.vat_id) |
The field exists. See Missing fields. |
size(input.items) |
The number of items in a list, characters in text, or fields in an object. |
input.items[0] |
One item of a list, counting from 0. |
input.items.exists(i, i.price > 100) |
At least one item matches. |
input.items.all(i, i.qty > 0) |
Every item matches. |
input.items.filter(i, i.price > 100) |
The items that match, as a list. |
input.items.map(i, i.qty) |
A list built from each item. |
Text
| Expression | Meaning |
|---|---|
input.email.endsWith("@example.com") |
Ends with. |
input.name.startsWith("An") |
Starts with. |
input.note.contains("urgent") |
Contains. |
input.code.matches("^[A-Z]{2}[0-9]+$") |
Matches a regular expression. It matches anywhere in the text unless you anchor it with ^ and $. |
input.first + " " + input.last |
Joins text. |
There are no functions for changing capitals or splitting text. To compare without regard to capitals, use matches("(?i)urgent").
Numbers and conversions
+, -, *, / and % work on numbers. Convert with int(x), double(x), string(x) and bool("true"). type(x) gives the type of a value.
A number in the input arrives as a decimal number, so int(input.amount) % 2 == 0 is the way to test for an even whole amount.
Dates and durations
Dates in JSON are text. timestamp(input.due) reads an ISO 8601 date and time such as "2026-11-30T10:00:00Z", and duration("24h") reads a length of time. Timestamps compare with < and >, and have functions such as getFullYear(), getMonth() (counting from 0) and getDayOfWeek(). A rule cannot read the current time, so it cannot ask whether a date has passed. Pass what it needs in the input.
Missing fields
A field the input does not have is an error, not false.
| Rule | Input | Result |
|---|---|---|
input.amount > 500 |
{"amount": 800} |
true |
input.amount > 500 |
{} |
the run fails: the input has no field "amount" |
has(input.vat_id) |
{} |
false |
has(input.vat_id) |
{"vat_id": "1"} |
true |
has(input.vat_id) && input.vat_id != "" |
{} |
false |
So guard every field that may be absent with has(). has() tests that the field is there; it does not test its value. It also works on a run started with no input at all (false).
A field that is there but has the wrong type is an error too: input.amount > 500 on {"amount": "lots"} fails with a field of the input has a different type than the rule expects.
Because a rule that errors fails the run, check your rules against realistic inputs, including ones with optional fields missing. See Try a rule.
Otherwise
"otherwise": true marks the one line followed when no rule on the other lines is true. It has no when.
- At most one line out of a condition may be
otherwise. - A line cannot have both
whenandotherwise. - It is followed only if no other line was chosen. If one or more rules are true,
otherwiseis not followed. - A condition without an
otherwiseline, where no rule is true, follows no line: everything beyond it that can only be reached through it is skipped, as when an agent chooses none.
What a run does
When the run reaches a condition that decides by rules, it checks each rule in the order of the lines, against the input, and follows every line whose rule is true. If none is true, it follows the otherwise line, if there is one.
The run records the choice as a step of kind rule. Its output shows what each rule gave:
{
"lines": [
{ "name": "sef", "when": "input.amount > 500 && input.currency == \"RSD\"", "result": true }
],
"chosen": ["sef"],
"otherwise": false
}
lines lists the rules checked (the otherwise line has none), chosen the names of the lines followed, and otherwise is true when no rule was true and the otherwise line was followed instead. See Deploy and run for steps in general.
When a rule fails
A rule that cannot give true or false ends the run: its status is failed and its stop reason is rule_failed. The error names the line and the reason:
It never takes otherwise. Silently taking a default path on bad data would hide the problem.
| Reason | Cause |
|---|---|
the input has no field "<name>" |
The rule reads a field the input does not have. Guard it with has(), or fix what starts the run. |
a field of the input has a different type than the rule expects |
For example, comparing text with a number. |
the rule did not give true or false |
The rule works out to something else for this input. |
the rule took too long to check |
The rule does too much work. Simplify it. |
the rule needs too much work to check |
The rule loops over too much data. Simplify it. |
If a rule fails, the whole run fails, even when another line's rule would have been true. Fix the rule or the input and start a new run.
Limits
| Limit | Value |
|---|---|
| Length of one rule | 1000 characters |
| Lines out of one condition | 20 |
| Sample input for trying a rule | 64 KB |
A rule is also limited in how much work it may do on one input and how long it may take; a rule that loops over large lists inside other loops can be stopped (see the last two reasons above).
Try a rule
Only the server can read a rule's syntax. Validating a playbook version checks every rule; to try one while you write it, check it by itself, with or without a sample input.
POST /workforce/rules/check
{ "when": "input.amount > 500 && input.currency == \"RSD\"", "input": { "amount": 800, "currency": "RSD" } }
| Answer | Meaning |
|---|---|
{ "valid": false, "message": "..." } |
The rule is not valid. The message says why. |
{ "valid": true } |
The rule is valid. No input was given. |
{ "valid": true, "result": true } |
Valid, and true (or false) for that input. |
{ "valid": true, "error": "the input has no field \"amount\"" } |
Valid, but it gives no answer for that input. This is what would fail a run. |
A rule over 1000 characters, or an input over 64 KB, is refused with 400. The caller needs the workforce::manage permission.
From the command line: ptkl kortex playbook rule-check --when 'input.amount > 500' --input sample.json. In the SDK: kortex.checkWorkforceRule({ when, input }). Through MCP: the workforce_rule_check tool.
The offline ptkl kortex playbook validate checks the shape of rules conditions (a rule on every line, one otherwise, no agent), but it cannot read a rule's syntax.
Examples
The owner's example: SEF or PDF
An invoice over 500 in RSD goes to SEF. Any other goes out as a PDF.
{ "from": "route", "to": "sef", "name": "sef", "when": "input.amount > 500 && input.currency == \"RSD\"" },
{ "from": "route", "to": "pdf", "name": "pdf", "otherwise": true }
| Input | Followed |
|---|---|
{"amount": 800, "currency": "RSD"} |
sef |
{"amount": 100, "currency": "RSD"} |
pdf |
{"amount": 800, "currency": "EUR"} |
pdf |
{"currency": "RSD"} |
The run fails: the input has no field "amount" |
Several lines can be true
Each line is checked on its own, so one input can start several branches.
{ "from": "route", "to": "notify", "name": "big", "when": "input.amount > 10000" },
{ "from": "route", "to": "review", "name": "foreign", "when": "input.country != \"RS\"" },
{ "from": "route", "to": "file", "name": "rest", "otherwise": true }
An optional field
Send the VAT number to the check step only when there is one.
{ "from": "route", "to": "vat", "name": "has vat id", "when": "has(input.vat_id) && input.vat_id != \"\"" },
{ "from": "route", "to": "skip", "name": "no vat id", "otherwise": true }
Lists and text
{ "from": "route", "to": "domestic", "name": "domestic", "when": "input.country in [\"RS\", \"ME\", \"BA\"]" },
{ "from": "route", "to": "internal", "name": "internal", "when": "has(input.email) && input.email.endsWith(\"@example.com\")" },
{ "from": "route", "to": "bulk", "name": "bulk", "when": "size(input.items) > 20 || input.items.exists(i, i.qty > 100)" }
Problems and fixes
These are the problems validate and deploy report for a rules condition, with the messages as they appear. <step> is the step's title, or its id when it has none; <line> is the line's name in quotes, or to "<id>" when it has no name.
| Code | Message | How to fix it |
|---|---|---|
rules_need_when |
The line <line> out of Step "<step>" has no rule; give it a rule or mark it "otherwise". |
Give the line a when, or set otherwise to true on it. |
rules_when_invalid |
The rule on line <line> out of Step "<step>" is not valid: <why>. |
Fix the rule as <why> says. Try it with rule check. The reasons include the rule is longer than 1000 characters, the rule gives <type>, not true or false (for example a rule that is only a number or text), undeclared reference to '<name>' (the only name you can use is input), and a syntax error with its position. |
rules_otherwise_twice |
More than one line out of Step "<step>" is "otherwise"; only one can be. |
Keep otherwise on one line. Give the others a rule. |
rules_when_and_otherwise |
The line <line> out of Step "<step>" has both a rule and "otherwise"; use one of them. |
Remove the when, or remove otherwise. |
rules_no_agent |
Step "<step>" decides by rules, so it has no agent. |
Remove the step's agent_uuid, or set decide_by to agent. |
rules_too_many_lines |
Step "<step>" has <n> lines out; a choice by rules can have at most 20. |
Combine rules, or split the choice into two conditions in a row. |
when_on_agent_condition |
The line <line> out of Step "<step>" has a rule, but only a choice that decides by rules can have one. |
Set decide_by to rules on the step, or remove when and otherwise from the line. |
bad_decide_by |
Step "<step>" is not a choice, so it cannot decide by rules. or Step "<step>" decides by "<value>"; it must be "agent" or "rules". |
Use decide_by only on a condition, and only with agent or rules. |
The ordinary rules for conditions still apply to a rules condition: at least two lines out, and every line has its own name (condition_few_lines, unnamed_line, duplicate_line_name). A rules condition does not need text (empty_text is not reported for it) and does not need an agent (no_agent, agent_missing and agent_disabled are not reported for it).