Skip to main content

Loop Node

The Loop Node is a specialized node that enables repetitive processes within the Sequential Agent workflow, allowing a sequence of nodes to be executed multiple times until a specific condition is met. This enables iterative refinement, information gathering, or step-by-step task completion.

Understanding the Loop Node

The Loop Node acts as a control mechanism, creating a cyclic path within the graph that can be traversed multiple times. It evaluates a condition each time and determines whether to:

  • Continue the loop: Proceed to the nodes within the loop for another iteration
  • Exit the loop: Move on to the node connected to the "Exit" output when a termination condition is met

This functionality is particularly valuable for workflows that require:

  • Incremental problem-solving: Breaking down complex tasks into smaller, repeated steps
  • Information gathering: Collecting multiple pieces of information through a series of related questions
  • Refinement processes: Iteratively improving a response or output until it meets certain criteria
  • Hierarchical conversations: Exploring topics in depth before returning to higher-level discussion

Inputs

RequiredDescription
Loop ConditionYesA JavaScript expression that will be evaluated at the beginning of each loop iteration to determine whether to continue looping or exit. This expression should evaluate to either true (continue looping) or false (exit the loop).
Max IterationsNoAn optional maximum number of iterations to prevent infinite loops. If not specified, a default limit is applied as a safety measure.

Outputs

The Loop Node has two possible output connections:

  • Loop: This path is followed when the loop condition evaluates to true, directing the flow to the nodes within the loop.
  • Exit: This path is followed when the loop condition evaluates to false or the maximum number of iterations is reached, directing the flow to continue beyond the loop.

Features

Conditional Looping with JavaScript Expressions

Like the Condition Node, the Loop Node uses JavaScript expression evaluation to determine whether to continue looping. This provides tremendous flexibility, allowing you to:

  • Access any part of the State using dot notation (e.g., state.itemsToProcess.length > 0)
  • Track iteration counts using custom State variables (e.g., state.iterationCount < 5)
  • Check for completion criteria in the custom State (e.g., !state.allRequiredInfoCollected)
  • Examine conversation history to make loop decisions (e.g., state.messages.find(m => m.includes("stop")))

Iteration Safeguards

To prevent infinite loops, the Loop Node includes safeguards:

  • Explicit maximum iterations: Allows you to set a specific limit based on your workflow's needs
  • Default maximum: Applies a reasonable limit if none is specified
  • Early exit: Allows breaking out of the loop based on the condition, even before reaching the maximum iterations

Best Practices

Clear loop purpose

Define a clear purpose for each loop in your workflow. If possible, document with a sticky note what you're trying to achieve with the loop.