---
title: "Agent Flow Logic"
icon: "diagram-project"
---

Agent Flow Logic provides a native workflow capability for building sophisticated, end-to-end agent execution flows tailored to your specific use case. With Agent Flow Logic steps, you can:

*   **Model complex decision trees** that route user queries to the right sub-agents or tools
*   **Branch dynamically** based on input content, metadata, or intermediate results
*   **Parallelize work** to speed up multi-step operations
*   **Use Human approvals** to involve humans in the agent execution loop

This powerful feature set allows you to encapsulate business logic, error handling, and context management directly within the agent configuration, without external orchestration.

## Agent Router Step

The Agent Router is the core of Agent Flow Logic. It evaluates incoming requests and determines which route or routes to execute using an LLM-driven decision step. The router sends descriptions of each available route (including purpose, input/output specifications, and any metadata) along with the user's query to the LLM, which then selects:

1.  **A single best-fit route**
2.  **Multiple concurrent routes** when the query matches more than one handler

![Thumbnail](./Images/Agent%20Router.png)



### How It Works

1.  **Define Routes and Select an LLM**
    *   Give each route a clear description.
    *   Optionally set a **default route** (see below).

2.  **LLM Decision**
    *   On each invocation, the router prompts the LLM with:
        *   The user’s input
        *   Descriptions of each route
        *   (If enabled) Recent chat history
    *   The LLM replies with one or more route identifiers.

3.  **Dispatch**
    *   The system invokes the selected route(s), passing along any extracted parameters and context.
    *   Route results are collected and returned to the user (or fed into downstream steps).

### Single Route Execution

By default, the Agent Router picks only **one** route per request—the one the LLM deems the best match. This is ideal when:

*   You have **mutually exclusive** handlers (e.g., “Check Weather” vs. “Book Flight”).
*   You want a **simplified, linear** workflow for each user query.

### Execute Multiple Routes in Parallel

For scenarios where a query may legitimately touch on more than one domain, you can enable **parallel routing**. When **Execute Multiple Routes** is turned on:

*   The router can select **all routes** whose descriptions match the input.
*   Each matching route runs ** simultaneously**, reducing end-to-end latency.
*   Results are merged and returned in a single combined response.

> 💡 Note: Use parallel routing when your application needs to gather information from multiple sub-agents in one go (e.g., performing sentiment analysis and topic extraction in the same pass).

### Include Chat History

Long-running conversations often require context to make accurate routing decisions. By enabling **Include Chat History**, you instruct the router to:

*   Pass the last *N* messages from the conversation along with the routing prompt.
*   Maintain stateful awareness of user intent across turns.

> 💡 Note: This is especially useful for multi-step workflows (surveys, booking flows, troubleshooting guides) where the correct handler may depend on prior user responses.

## Setting the Default Route

A **default route** acts as a fallback when:

*   The LLM is **uncertain** which route fits best.
*   **No** route descriptions sufficiently match the user’s query.

### Why and When to Configure

*   **Graceful Degradation**: Ensure users never hit a dead end if the router can’t decide.
*   **Catch-All Logic**: Implement generic handlers (e.g., “I’m sorry, I didn’t understand that. Can I help with…?”).




## Use the Loop Step

The Loop step in Agent Studio provides advanced control over your agent workflows, allowing you to create efficient subflows that manage context and prevent execution context overload.

### How it Works

The Loop step iterates through an array of items, executing a defined subflow for each.

### Configure the Loop Step

1.  **Add the Loop Step:**
    *   In Agent Studio, open the **Flow Control** section.
    *   Drag the **Loop** step onto the canvas.
2.  **Define Subflow Logic:**
    *   Inside the **Loop** step, add your desired subflow logic.
    *   Set the maximum number of iterations.
        > 💡 Note: The current limit is 100 iterations per agent execution.
3.  **Ensure Connections:**
    *   Make sure all steps within the **Loop** are properly connected to pass validation when publishing the agent.
4.  **Input and Output:**
    *   The **Loop** step requires an **Object Array** as input to iterate through each item.
        *   You can instruct an AI model to produce this **Object Array**. The Loop step can then process the stringified array and trigger the iteration cycles.
        > 💡 Tip: When using a **Prompt** component to generate the input array, instruct the AI to always respond with structured data in JSON format. For example:
        >
        > "Every response must be a valid JSON array of objects. Never include explanatory text, markdown formatting, or any content outside the JSON structure."
    *   Upon completion, it outputs the same **Object Array** with the results added, passing it down the execution flow.
