> For the complete documentation index, see [llms.txt](https://whitepaper.aitech.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://whitepaper.aitech.io/agentforge/blocks/condition.md).

# condition

The Condition block allows you to branch your workflow execution path based on boolean expressions. It evaluates conditions and routes the workflow accordingly, enabling you to create dynamic, responsive workflows with different execution paths.

{% hint style="info" %}
Condition blocks enable deterministic decision-making without requiring an LLM, making them ideal for straightforward branching logic.
{% endhint %}

### Overview

The Condition block enables you to:

* **Create branching logic**: Route workflows based on boolean expressions
* **Make data-driven decisions**: Evaluate conditions using previous block outputs
* **Handle multiple scenarios**: Define multiple conditions with different paths
* **Provide deterministic routing**: Make decisions without requiring an LLM

### How It Works

The Condition block operates through a sequential evaluation process:

{% stepper %}
{% step %}

### Evaluate Expression

Processes the JavaScript/TypeScript boolean expression using current workflow data
{% endstep %}

{% step %}

### Determine Result

Returns true or false based on the expression evaluation
{% endstep %}

{% step %}

### Route Workflow

Directs execution to the appropriate destination block based on the result
{% endstep %}

{% step %}

### Provide Context

Generates metadata about the decision for debugging and monitoring
{% endstep %}
{% endstepper %}

### Configuration Options

#### Conditions

Define one or more conditions that will be evaluated. Each condition includes:

* **Expression**: A JavaScript/TypeScript expression that evaluates to true or false
* **Path**: The destination block to route to if the condition is true
* **Description**: Optional explanation of what the condition checks

You can create multiple conditions that are evaluated in order, with the first matching condition determining the execution path.

#### Condition Expression Format

Conditions use JavaScript syntax and can reference input values from previous blocks.

{% tabs %}
{% tab title="Score Threshold" %}

```javascript
// Check if a score is above a threshold
<agent.score> > 75
```

{% endtab %}

{% tab title="Text Analysis" %}

```javascript
// Check if a text contains specific keywords
<agent.text>.includes('urgent') || <agent.text>.includes('emergency')
```

{% endtab %}

{% tab title="Multiple Conditions" %}

```javascript
// Check multiple conditions
<agent.age> >= 18 && <agent.country> === 'US'
```

{% endtab %}
{% endtabs %}

#### Accessing Results

After a condition evaluates, you can access its outputs:

* **`<condition.result>`**: Boolean result of the condition evaluation
* **`<condition.matched_condition>`**: ID of the condition that was matched
* **`<condition.content>`**: Description of the evaluation result
* **`<condition.path>`**: Details of the chosen routing destination

### Advanced Features

#### Complex Expressions

Use JavaScript operators and functions in conditions:

```javascript
// String operations
<user.email>.endsWith('@company.com')

// Array operations
<api.tags>.includes('urgent')

// Mathematical operations
<agent.confidence> * 100 > 85

// Date comparisons
new Date(<api.created_at>) > new Date('2024-01-01')
```

#### Multiple Condition Evaluation

Conditions are evaluated in order until one matches:

```javascript
// Condition 1: Check for high priority
<ticket.priority> === 'high'

// Condition 2: Check for urgent keywords
<ticket.subject>.toLowerCase().includes('urgent')

// Condition 3: Default fallback
true
```

#### Error Handling

Conditions automatically handle:

* Undefined or null values with safe evaluation
* Type mismatches with appropriate fallbacks
* Invalid expressions with error logging
* Missing variables with default values

### Inputs and Outputs

{% tabs %}
{% tab title="Configuration" %}

* **Conditions**: Array of boolean expressions to evaluate
* **Expressions**: JavaScript/TypeScript conditions using block outputs
* **Routing Paths**: Destination blocks for each condition result
  {% endtab %}

{% tab title="Variables" %}

* **condition.result**: Boolean result of condition evaluation
* **condition.matched\_condition**: ID of the matched condition
* **condition.content**: Description of evaluation result
* **condition.path**: Details of chosen routing destination
  {% endtab %}

{% tab title="Results" %}

* **Boolean Result**: Primary condition evaluation outcome
* **Routing Information**: Path selection and condition details
* **Access**: Available in blocks after the condition
  {% endtab %}
  {% endtabs %}

### Example Use Cases

#### Customer Support Routing

**Scenario: Route support tickets based on priority**

1. API block fetches support ticket data
2. Condition checks if `<api.priority>` equals `'high'`
3. High priority tickets → Agent with escalation tools
4. Normal priority tickets → Standard support agent

#### Content Moderation

**Scenario: Filter content based on analysis results**

1. Agent analyzes user-generated content
2. Condition checks if `<agent.toxicity_score>` > 0.7
3. Toxic content → Moderation workflow
4. Clean content → Publishing workflow

#### User Onboarding Flow

**Scenario: Personalize onboarding based on user type**

1. Function block processes user registration data
2. Condition checks if `<user.account_type>` === `'enterprise'`
3. Enterprise users → Advanced setup workflow
4. Individual users → Simple onboarding workflow

### Best Practices

* **Order conditions correctly**: Place more specific conditions before general ones to ensure specific logic takes precedence over fallbacks
* **Include a default condition**: Add a catch-all condition (`true`) as the last condition to handle unmatched cases and prevent workflow execution from getting stuck
* **Keep expressions simple**: Use clear, straightforward boolean expressions for better readability and easier debugging
* **Document your conditions**: Add descriptions to explain the purpose of each condition for better team collaboration and maintenance
* **Test edge cases**: Verify conditions handle boundary values correctly by testing with values at the edges of your condition ranges


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://whitepaper.aitech.io/agentforge/blocks/condition.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
