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

# Complex Logic

> Advanced flow control and logic patterns

Build sophisticated automation with conditional logic, loops, and advanced control flow.

## Conditional Logic

### Basic Conditions

Route flows based on data values:

```mermaid theme={null}
graph LR
    A[Input] --> B{Condition}
    B -->|True| C[Path A]
    B -->|False| D[Path B]
```

**Use Cases:**

* Route by priority (high/medium/low)
* Filter by criteria
* Decision trees
* Error handling

**Example:**

```
Support Ticket
  → Check Priority
  → If High: Immediate Assignment
  → If Low: Queue for Later
```

### Multi-Way Branching

Route to multiple paths:

```mermaid theme={null}
graph LR
    A[Input] --> B{Type?}
    B -->|Technical| C[Tech Team]
    B -->|Billing| D[Finance Team]
    B -->|General| E[Support Team]
```

**Use Cases:**

* Content routing
* Department assignment
* Workflow selection
* Resource allocation

### AI-Assisted Conditions

Use AI to make conditional decisions:

```mermaid theme={null}
graph LR
    A[Text Input] --> B[Route Decision]
    B --> C{Category?}
    C -->|Urgent| D[Immediate Action]
    C -->|Normal| E[Standard Process]
    C -->|Low| F[Batch Process]
```

**Use Cases:**

* Sentiment-based routing
* Intent classification
* Dynamic prioritization
* Smart filtering

***

## Loops & Iteration

### List Processing

Connect a list output to a step that takes a single value, and the step runs once for each item:

```mermaid theme={null}
graph LR
    A[List of Items] --> B[For Each Item]
    B --> C[Process Item]
    C --> D[Collect Results]
```

**Use Cases:**

* Batch processing
* Data transformation
* Multi-item operations
* Aggregation

**Example:**

```
List of Customers
  → For Each Customer
    → Generate Personalized Email
    → Send Email
  → Return Success Count
```

### Conditional Loops

A flow cannot loop back to an earlier step: a path that forms a cycle is rejected, and the While Loop node is not offered in the editor. To repeat work until it is good enough, do it inside an Agent node, or set retries in a node's **Error Handling**:

```mermaid theme={null}
graph LR
    A[Start] --> B[Process]
    B --> C{Done?}
    C -->|No| B
    C -->|Yes| D[Complete]
```

**Use Cases:**

* Retry logic
* Iterative refinement
* Quality checks
* Progressive processing

**Example:**

```
Generate Content
  → Check Quality
  → If Quality < 80%: Regenerate with feedback
  → If Quality >= 80%: Accept
```

***

## Advanced Patterns

### Parallel Processing

Execute multiple paths simultaneously:

```mermaid theme={null}
graph TB
    A[Input] --> B[Path 1]
    A --> C[Path 2]
    A --> D[Path 3]
    B --> E[Join Results]
    C --> E
    D --> E
```

**Use Cases:**

* Multi-source data gathering
* Parallel API calls
* Concurrent processing
* Performance optimization

**Example:**

```
Customer ID
  → Get Customer Info (CRM)
  → Get Order History (Database)
  → Get Support Tickets (Ticketing System)
  → Combine All Data
  → Generate Report
```

### Nested Subflows

Call flows within flows for modularity:

```mermaid theme={null}
graph LR
    A[Main Flow] --> B[Subflow 1]
    B --> C[Subflow 2]
    C --> D[Subflow 3]
    D --> E[Final Result]
```

**Use Cases:**

* Reusable components
* Complex workflows
* Separation of concerns
* Team collaboration

**Example:**

```
Order Processing
  → Validate Order (subflow)
  → Process Payment (subflow)
  → Ship Order (subflow)
  → Send Confirmation (subflow)
```

### Error Handling & Retry

Handle failures gracefully:

```mermaid theme={null}
graph LR
    A[Operation] --> B{Success?}
    B -->|Yes| C[Continue]
    B -->|No| D[Retry]
    D --> E{Max Retries?}
    E -->|No| A
    E -->|Yes| F[Escalate]
```

**Use Cases:**

* API call failures
* External service timeouts
* Transient errors
* Resilient automation

***

## State Management

### Flow Variables

Flow variables hold configuration a flow reads, such as URLs, IDs and defaults. A run cannot change them, so a running count comes from a node's output, for example List Operations or Math Operations on the iterated results:

**Use Cases:**

* Counters and accumulators
* Temporary storage
* Cross-node data sharing
* Loop state

**Example:**

```
Initialize Counter = 0
  → For Each Item
    → Process Item
    → If Success: Counter + 1
  → Return Counter (total successes)
```

### Context Passing

Pass context between flow steps:

**Use Cases:**

* Maintain conversation context
* Track processing history
* Accumulate results
* Decision tracking

***

## Dynamic Flows

### Runtime Path Selection

Choose flow path dynamically:

```mermaid theme={null}
graph LR
    A[Input] --> B[Analyze]
    B --> C[Select Strategy]
    C --> D{Strategy?}
    D -->|A| E[Flow A]
    D -->|B| F[Flow B]
    D -->|C| G[Flow C]
```

**Use Cases:**

* Adaptive workflows
* Context-dependent processing
* Optimization strategies
* A/B testing

### Dynamic Tool Selection

Let agents choose which tools to use:

```mermaid theme={null}
graph LR
    A[User Request] --> B[Agent Analyze]
    B --> C{Best Tool?}
    C -->|KB| D[Search Knowledge]
    C -->|Web| E[Web Research]
    C -->|Flow| F[Execute Flow]
    D --> G[Synthesize]
    E --> G
    F --> G
```

**Use Cases:**

* Intelligent automation
* Resource optimization
* Context-aware processing
* Cost optimization

***

## Best Practices

**Design:**

* Keep flows modular with subflows
* Use clear naming conventions
* Document complex logic
* Plan for error cases

**Performance:**

* Minimize sequential dependencies
* Use parallel processing where possible
* Cache expensive operations
* Optimize loop iterations

**Maintainability:**

* Use subflows for reusability
* Add comments and descriptions
* Version control flow definitions
* Test edge cases

**Debugging:**

* Add logging at key points
* Use test mode frequently
* Inspect intermediate outputs
* Track execution paths

> [card] [Working with AI](https://docs.noxus.ai/platform/flows/ai-processing.md)
>
> AI nodes and agents

> [card] [Agents in Flows](https://docs.noxus.ai/platform/scenarios/agents-in-flows.md)
>
> Combine agents with flows


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.