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

# If/Else Stage

> Branch conversations based on conditions

The **If/Else** stage routes conversations based on variable values and conditions. Create branching logic to handle different scenarios.

## Overview

If/Else nodes evaluate conditions against collected data and route to different paths:

```
             ┌─────────────┐
             │   If/Else   │
             └──────┬──────┘
        ┌──────────┼──────────┐
        │          │          │
        ▼          ▼          ▼
   [Condition A] [Condition B] [Default]
```

<Info>
  If/Else nodes are colored **yellow** on the canvas.
</Info>

## Configuration

### Conditions

Each condition evaluates a variable against a value:

| Property     | Description              |
| ------------ | ------------------------ |
| **Variable** | The variable to check    |
| **Operator** | Comparison operator      |
| **Value**    | Value to compare against |
| **Target**   | Node to route to if true |

### Operators

| Operator       | Description           | Example                                   |
| -------------- | --------------------- | ----------------------------------------- |
| `==`           | Equals                | `status == "active"`                      |
| `!=`           | Not equals            | `plan != "free"`                          |
| `<`            | Less than             | `age < 18`                                |
| `>`            | Greater than          | `total > 100`                             |
| `<=`           | Less than or equal    | `quantity <= 10`                          |
| `>=`           | Greater than or equal | `score >= 80`                             |
| `exists`       | Variable is set       | `order_number exists`                     |
| `is_empty`     | Variable not set      | `email is_empty`                          |
| `contains`     | Contains substring    | `issue contains "refund"`                 |
| `not_contains` | Doesn't contain       | `notes not_contains "urgent"`             |
| `in`           | Value in list         | `department in ["sales", "support"]`      |
| `not_in`       | Value not in list     | `status not_in ["cancelled", "expired"]`  |
| `regex`        | Matches pattern       | `order_number regex "^[A-Z]{2}[0-9]{6}$"` |

### Default Target

The fallback route when no conditions match. **Always set a default** to handle unexpected cases.

## Evaluation Order

Conditions are evaluated **in order** from top to bottom. The first matching condition wins.

```yaml theme={null}
Conditions:
  1. issue_type == "billing"     → Billing Team
  2. issue_type == "technical"   → Tech Support
  3. priority == "high"          → Urgent Queue
  4. Default                     → General Support
```

If `issue_type` is "billing", the caller goes to Billing Team even if `priority` is also "high".

## Example Configurations

<AccordionGroup>
  <Accordion title="Department Routing">
    ```yaml theme={null}
    Conditions:
      - variable: department
        operator: ==
        value: "sales"
        target: Sales Conversation

      - variable: department
        operator: ==
        value: "support"
        target: Support Conversation

      - variable: department
        operator: ==
        value: "billing"
        target: Billing Conversation

    Default: General Conversation
    ```
  </Accordion>

  <Accordion title="Priority-Based Routing">
    ```yaml theme={null}
    Conditions:
      - variable: customer_tier
        operator: ==
        value: "enterprise"
        target: VIP Support

      - variable: wait_time
        operator: >
        value: "10"
        target: Priority Queue

      - variable: issue_severity
        operator: ==
        value: "critical"
        target: Urgent Handler

    Default: Standard Queue
    ```
  </Accordion>

  <Accordion title="Order Status Check">
    ```yaml theme={null}
    Conditions:
      - variable: order_status
        operator: ==
        value: "shipped"
        target: Tracking Info

      - variable: order_status
        operator: ==
        value: "processing"
        target: Processing Update

      - variable: order_status
        operator: ==
        value: "cancelled"
        target: Cancellation Handling

      - variable: order_status
        operator: is_empty
        target: Order Lookup

    Default: Order Support
    ```
  </Accordion>

  <Accordion title="Multi-Condition Routing">
    ```yaml theme={null}
    Conditions:
      - variable: language
        operator: ==
        value: "spanish"
        target: Spanish Agent

      - variable: return_customer
        operator: ==
        value: "true"
        target: Loyalty Program

      - variable: cart_value
        operator: >
        value: "500"
        target: Premium Sales

    Default: Standard Flow
    ```
  </Accordion>
</AccordionGroup>

## Common Patterns

### Validate Required Data

Check if required information was collected:

```yaml theme={null}
Conditions:
  - variable: customer_email
    operator: exists
    target: Proceed with Email

  - variable: customer_phone
    operator: exists
    target: Proceed with Phone

Default: Collect Contact Info
```

### Business Hours Routing

Route based on time-related variables:

```yaml theme={null}
Conditions:
  - variable: is_business_hours
    operator: ==
    value: "true"
    target: Live Support

Default: After Hours Message
```

### Sentiment-Based Routing

Route frustrated customers differently:

```yaml theme={null}
Conditions:
  - variable: sentiment
    operator: ==
    value: "angry"
    target: De-escalation Handler

  - variable: sentiment
    operator: ==
    value: "frustrated"
    target: Priority Support

Default: Standard Support
```

## Flow Examples

### Simple Branch

```
┌─────────────────┐
│ Data Extraction │
│ - issue_type    │
└────────┬────────┘
         │
    ┌────▼────┐
    │ If/Else │
    └────┬────┘
    ┌────┴────┬────────┐
    │         │        │
    ▼         ▼        ▼
[Returns] [Exchange] [Default]
```

### Nested Conditions

```
┌─────────────────┐
│ Data Extraction │
└────────┬────────┘
         │
    ┌────▼────┐
    │ If/Else │ (Department)
    └────┬────┘
         │
    ┌────▼────┐
    │ If/Else │ (Priority)
    └────┬────┘
         │
    ┌────▼────┐
    │ Handler │
    └─────────┘
```

## Best Practices

<CardGroup cols={2}>
  <Card title="Always Set Default" icon="shield">
    Every If/Else needs a default path. Unhandled cases break flows.
  </Card>

  <Card title="Order Matters" icon="list-ol">
    Put most specific conditions first. More general conditions go later.
  </Card>

  <Card title="Test All Paths" icon="vial">
    Verify each condition branch works as expected with test data.
  </Card>

  <Card title="Keep It Simple" icon="minimize">
    Avoid deeply nested If/Else chains. Consider restructuring complex logic.
  </Card>
</CardGroup>

## Related

<CardGroup cols={2}>
  <Card title="Data Extraction" icon="clipboard-list" href="/agent-builder/stages/data-extraction">
    Collect the variables to evaluate
  </Card>

  <Card title="Transitions" icon="arrow-right" href="/agent-builder/transitions">
    Alternative routing with conversation transitions
  </Card>

  <Card title="Conversation" icon="comments" href="/agent-builder/stages/conversation">
    Handle each branch with conversation nodes
  </Card>
</CardGroup>
