> ## Documentation Index
> Fetch the complete documentation index at: https://docs.maadify.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Rules and Routing

> Define conditions and actions that control workflow routing

## How rules work

Rules evaluate workflow context and decide what runs next. Each rule has:

* **Conditions**: what must be true.
* **Actions**: what should happen when conditions match.
* **Order**: the priority for evaluation on a node.

All conditions and actions make up the flow view.

<Frame>
  <img src="https://mintcdn.com/maadify/X4aEKWAIWIr8YAhZ/images/rules/agent_canvas.png?fit=max&auto=format&n=X4aEKWAIWIr8YAhZ&q=85&s=b311f7de8d572f78b5ad953edb279812" alt="Flow view showing conditions and actions" width="593" height="1009" data-path="images/rules/agent_canvas.png" />
</Frame>

## How the state transition selects the next step

The runtime evaluates rules in order and stops on the first rule that triggers. If no rule triggers, the run fails with a “no rule triggered” error.

<Steps>
  <Step title="Prepare context and variables">
    The engine builds a context map from tool outputs, runtime inputs, and message history, then exposes that data as rule variables.
  </Step>

  <Step title="Normalize rules">
    Rule conditions are normalized (types are applied, tool scoping is resolved, and sub-agent IDs are mapped to current names).
  </Step>

  <Step title="Evaluate rules in order">
    The first rule that matches runs its actions and determines the next step.
  </Step>

  <Step title="Re-evaluate after tool execution">
    If a rule executes a tool, the engine stores the response in context and re-evaluates rules with the new data.
  </Step>
</Steps>

<Tip>
  Always add a fallback rule at the end so the workflow has a deterministic next step.
</Tip>

## Add a rule

<Steps>
  <Step title="Choose a condition type">
    Pick the condition that matches the data you want to evaluate.
  </Step>

  <Step title="Set operators and values">
    Use type-specific operators and provide the comparison value.
  </Step>

  <Step title="Select actions">
    Route to the next agent, execute a tool, request approval, or set an error.
  </Step>
</Steps>

<Frame>
  <img src="https://mintcdn.com/maadify/X4aEKWAIWIr8YAhZ/images/rules/general_edit_rule.png?fit=max&auto=format&n=X4aEKWAIWIr8YAhZ&q=85&s=5319a836db5ff0a0715ee134ca806aa7" alt="Rule builder with conditions and actions" width="829" height="749" data-path="images/rules/general_edit_rule.png" />
</Frame>

## Add/edit rule dialog behavior

When you open the rule dialog, it supports both **Add** and **Edit** modes:

* **Add New Rule** when no rule is selected.
* **Edit Rule** when you clicked a rule from the flow or list.
* **View Rule** if you do not have edit permissions.

Use the dialog to build conditions, actions, and nested groups in one place.

<Tabs>
  <Tab title="Edit mode">
    <Frame>
      <img src="https://mintcdn.com/maadify/X4aEKWAIWIr8YAhZ/images/rules/general_edit_rule.png?fit=max&auto=format&n=X4aEKWAIWIr8YAhZ&q=85&s=5319a836db5ff0a0715ee134ca806aa7" alt="Rule builder with conditions and actions" width="829" height="749" data-path="images/rules/general_edit_rule.png" />
    </Frame>
  </Tab>

  <Tab title="View mode">
    <Frame>
      <img src="https://mintcdn.com/maadify/X4aEKWAIWIr8YAhZ/images/rules/view_mode.png?fit=max&auto=format&n=X4aEKWAIWIr8YAhZ&q=85&s=0761d8a34bcb96c9d38b361c10ea8fcb" alt="Rule dialog in read-only View Rule mode" width="1498" height="713" data-path="images/rules/view_mode.png" />
    </Frame>
  </Tab>
</Tabs>

## Condition groups and nesting

Rules support **all**/**any** logic with nested groups for complex branching.

<Tip>
  Use nested groups to model branching without duplicating rules.
</Tip>

<Frame>
  <img src="https://mintcdn.com/maadify/X4aEKWAIWIr8YAhZ/images/rules/nested_conditions.png?fit=max&auto=format&n=X4aEKWAIWIr8YAhZ&q=85&s=f36ba0a16476aebfe9f8eaecda85274a" alt="Rule conditions showing nested all and any groups" width="958" height="582" data-path="images/rules/nested_conditions.png" />
</Frame>

## Condition types

Available condition types include:

* **Last agent name**: route based on the most recent sub-agent.
* **Last message content**: match text returned by the last step.
* **Last tool executed**: route based on which tool ran last.
* **Execute tool content**: evaluate fields inside tool output.
* **Runtime inputs**: evaluate values provided by triggers or prior steps.

<Frame>
  <img src="https://mintcdn.com/maadify/X4aEKWAIWIr8YAhZ/images/rules/conditions_overview.png?fit=max&auto=format&n=X4aEKWAIWIr8YAhZ&q=85&s=9538239d362f06cb53e865df023f6da6" alt="Condition type dropdown listing the available condition types" width="1166" height="597" data-path="images/rules/conditions_overview.png" />
</Frame>

## Operators by content type

For **execute tool content**, operators change based on the selected content type:

* **Text**: `equal_to`, `not_equal_to`, `starts_with`, `ends_with`, `contains`, `does_not_contain`, `non_empty`, `matches_regex`
* **Numeric**: `equal_to`, `not_equal_to`, `greater_than`, `less_than`, `greater_than_or_equal_to`, `less_than_or_equal_to`
* **Boolean**: `is_true`, `is_false`
* **Select**: `contains`, `does_not_contain`
* **Select multiple**: `contains_all`, `is_contained_by`, `shares_at_least_one_element_with`, `shares_exactly_one_element_with`, `shares_no_elements_with`
* **Array length**: `equal_to`, `not_equal_to`, `greater_than`, `less_than`, `greater_than_or_equal_to`, `less_than_or_equal_to`

<Note>
  Array operators compare the length of the selected array to the value you provide.
</Note>

<Frame>
  <img src="https://mintcdn.com/maadify/X4aEKWAIWIr8YAhZ/images/rules/operator_options.png?fit=max&auto=format&n=X4aEKWAIWIr8YAhZ&q=85&s=b428e7f72ac98cbbf0c03d8f9a43b915" alt="Operator dropdown showing operators that change with the selected content type" width="938" height="463" data-path="images/rules/operator_options.png" />
</Frame>

## Actions you can run

Rules can trigger one or more actions:

* **Select next agent**: route to a sub-agent.
* **Execute tool**: call a tool with templated input.
* **Send approval**: pause and request confirmation.
* **Set error**: flag the run and route to error handling.

<Tabs>
  <Tab title="Select next agent">
    <Frame>
      <img src="https://mintcdn.com/maadify/X4aEKWAIWIr8YAhZ/images/rules/action_select_agent.png?fit=max&auto=format&n=X4aEKWAIWIr8YAhZ&q=85&s=0159fa16fdd73b800cdfb3ab8ed966f9" alt="Rule action picker listing select next agent, execute tool, send approval, and set error" width="926" height="329" data-path="images/rules/action_select_agent.png" />
    </Frame>
  </Tab>

  <Tab title="Execute tool">
    <Frame>
      <img src="https://mintcdn.com/maadify/X4aEKWAIWIr8YAhZ/images/rules/action_execute_tool.png?fit=max&auto=format&n=X4aEKWAIWIr8YAhZ&q=85&s=50f7d0f0a37f909ce2485cf761712984" alt="Rule action picker listing select next agent, execute tool, send approval, and set error" width="762" height="743" data-path="images/rules/action_execute_tool.png" />
    </Frame>
  </Tab>

  <Tab title="Send approval">
    <Frame>
      <img src="https://mintcdn.com/maadify/X4aEKWAIWIr8YAhZ/images/rules/action_set_approval.png?fit=max&auto=format&n=X4aEKWAIWIr8YAhZ&q=85&s=618fb3a97a67a6067485c110be518bd6" alt="Rule action picker listing select next agent, execute tool, send approval, and set error" width="922" height="695" data-path="images/rules/action_set_approval.png" />
    </Frame>
  </Tab>

  <Tab title="Set error">
    <Frame>
      <img src="https://mintcdn.com/maadify/X4aEKWAIWIr8YAhZ/images/rules/action_set_error.png?fit=max&auto=format&n=X4aEKWAIWIr8YAhZ&q=85&s=26e320e336d7320bc3ca3b7dde8d4ae2" alt="Rule action picker listing select next agent, execute tool, send approval, and set error" width="928" height="337" data-path="images/rules/action_set_error.png" />
    </Frame>
  </Tab>
</Tabs>

## Approvals and errors

Approvals pause execution and can send email notifications to selected users. Errors generate notifications and can route to recovery steps.

<Check>
  Approvals appear in **Notifications** and in **Agent activity**.
</Check>

<Tabs>
  <Tab title="Approval action">
    <Frame>
      <img src="https://mintcdn.com/maadify/X4aEKWAIWIr8YAhZ/images/rules/approval_notification.png?fit=max&auto=format&n=X4aEKWAIWIr8YAhZ&q=85&s=604704d282f9b0796b613ffa7d578b33" alt="View of email" width="599" height="620" data-path="images/rules/approval_notification.png" />
    </Frame>
  </Tab>

  <Tab title="Error action">
    <Frame>
      <img src="https://mintcdn.com/maadify/X4aEKWAIWIr8YAhZ/images/rules/error_view_activity.png?fit=max&auto=format&n=X4aEKWAIWIr8YAhZ&q=85&s=37528fdd391ad45e816c4074fe94fa51" alt="Set error action configuration with error message and routing" width="554" height="642" data-path="images/rules/error_view_activity.png" />
    </Frame>

    <Frame>
      <img src="https://mintcdn.com/maadify/X4aEKWAIWIr8YAhZ/images/rules/error_notification.png?fit=max&auto=format&n=X4aEKWAIWIr8YAhZ&q=85&s=75d398e070700468ce521c33e043e607" alt="Set error action configuration with error message and routing" width="597" height="538" data-path="images/rules/error_notification.png" />
    </Frame>
  </Tab>
</Tabs>

## Rule ordering

Rules are evaluated in order for each node. Use **Rule order** in flow view to change evaluation priority.

<Frame>
  <img src="https://mintcdn.com/maadify/K7CT7HZ-nt5ok2U5/images/flows/rule_order.png?fit=max&auto=format&n=K7CT7HZ-nt5ok2U5&q=85&s=b6b99ac3cfa7cf753715001c9dc98606" alt="Rule order dialog showing evaluation priority" width="1114" height="731" data-path="images/flows/rule_order.png" />
</Frame>

## How rule data is interpreted at runtime

Rules are normalized before evaluation to make them resilient and consistent:

* **Sub-agent ID resolution**: rules that reference a sub-agent ID are resolved to the current agent name so renames do not break routing.
* **Tool-scoped context paths**: conditions referencing tool output are scoped by tool ID or tool instance ID.
* **Typed variables**: condition types (text, numeric, boolean, select, array) control how values are compared.
* **Fallback conditions**: an empty condition set is converted to an always-true condition so the rule can be used as a default.

## Context variables and tool scoping

Rule conditions can reference tool outputs using a dot-path. At runtime, the engine resolves:

**tool\_key** would be `{tool_name}_{tool_id}`

* **Tool ID paths**: `tool_key.field.path` (e.g., `64.items.0.id`)
* **Tool instance paths**: `tool_key__instance_id.field.path` when multiple tool instances exist

When a rule targets **last tool executed** and a tool instance is provided, the engine evaluates against `last_tool_executed_instance` so the condition matches the correct instance.

<Tabs>
  <Tab title="Select Tool">
    <Frame>
      <img src="https://mintcdn.com/maadify/X4aEKWAIWIr8YAhZ/images/rules/tool_content_condition.png?fit=max&auto=format&n=X4aEKWAIWIr8YAhZ&q=85&s=08f99a9ec5785a363cc883d2d6b6a761" alt="Pick your tool" width="766" height="714" data-path="images/rules/tool_content_condition.png" />
    </Frame>
  </Tab>

  <Tab title="Tool Condition - Tool Property Paths">
    <Frame>
      <img src="https://mintcdn.com/maadify/X4aEKWAIWIr8YAhZ/images/rules/tool_content_condition_property.png?fit=max&auto=format&n=X4aEKWAIWIr8YAhZ&q=85&s=e0738f702ee8c74f45f65cb84d592fb4" alt="Variable picker showing tool ID and tool instance dot paths" width="767" height="537" data-path="images/rules/tool_content_condition_property.png" />
    </Frame>
  </Tab>
</Tabs>

## Tool execution effects on rule evaluation

Rules that execute tools can change the rule evaluation loop:

* After a tool runs, its response is stored in context and rules are re-evaluated.
* Tools already executed in the current transition are filtered out to prevent loops.
* After a tool executes, only rules that check **last tool executed** are eligible to trigger, which prevents earlier entry rules from firing again on the same message.

## Continue conversation behavior

Two parent-agent settings change how “last agent” and “last message” are interpreted:

* **Default agent on continued conversation**: routes directly to the configured agent when a conversation resumes.
* **Continue with last agent**: ignores a returning user message and keeps the last agent as the routing reference.

Use these settings to control whether a resumed conversation restarts your rule logic or continues from the last agent.

<Frame>
  <img src="https://mintcdn.com/maadify/X4aEKWAIWIr8YAhZ/images/rules/default_agent_continued_conversation.png?fit=max&auto=format&n=X4aEKWAIWIr8YAhZ&q=85&s=b88e5e2783d7d903b3ccc56c382664ab" alt="Parent agent settings showing default agent on continued conversation and continue with last agent toggles" width="648" height="128" data-path="images/rules/default_agent_continued_conversation.png" />
</Frame>
