Loops
Loops allow a workflow to repeat an operation or group of operations while a runtime decision indicates that another iteration should occur. Loops are useful when the same workflow logic must execute repeatedly for a collection, batch, polling process, iterative calculation, or other intentional repeated operation. The exact public API for defining workflow loops should be taken from the installed BindAI implementation.What Is a Loop?
A loop creates repeated execution within a workflow. Conceptually:Why Use Loops?
Loops are useful when the same workflow logic must execute repeatedly. Common examples include:- Processing collections
- Processing batches
- Polling external systems
- Waiting for background operations
- Repeated validation
- Processing paginated results
- Performing repeated calculations
- Incremental synchronization
Loop Execution Flow
A typical loop follows this pattern:Loop State
Loops normally depend on information that changes as execution progresses. For example:Loop Body
The loop body contains the operations performed during one iteration. For example:- Agents
- Tools
- Conditions
- Knowledge retrieval
- Memory operations
- External integrations
- Validation
- Data processing
Loop Exit
Every loop should have a clear exit path. Conceptually:Loop Decisions
The continuation decision determines whether another iteration should occur. Conceptually:- More items remain
- A task is not complete
- A validation condition has not been satisfied
- Additional pages are available
- A synchronization process still has work
Example: Processing a Collection
A common loop pattern processes items one at a time.Example: Polling an External Service
A loop can repeatedly check the state of an asynchronous operation.Example: Batch Processing
Loops can process batches incrementally.Example: Pagination
A workflow can repeatedly request pages of data.Example: Repeated Validation
A workflow may repeat validation until a desired state is reached.Loops vs Retry
Loops and retries both involve repeated execution, but they solve different problems.
Use a loop when repetition is part of the intended workflow behavior.
Use retry when an operation fails and should be attempted again according to a reliability policy.
A retry should not normally be implemented by wrapping every failure in a general-purpose workflow loop.
Loops with Conditions
Conditions can be used inside a loop.Loops with Agents
An agent can participate in each loop iteration. For example:Loops with Tools
Tools are useful when each iteration performs deterministic work.- Processing records
- Calling an API for each item
- Writing application data
- Performing calculations
- Synchronizing external resources
Loops with Knowledge
Knowledge retrieval can be performed during repeated processing. For example:Loops with Memory
Memory can also participate in iterative workflows. For example:Nested Loops
Loops can be combined with other loops. For example:Avoiding Infinite Loops
Every loop needs a reliable path toward termination. For example:- A clear termination condition
- State that changes during execution
- Measurable progress toward termination
- Appropriate safeguards for long-running operations
Maximum Iterations
For loops that depend on external systems or unpredictable data, a maximum iteration limit can provide an additional safety mechanism. Conceptually:Timeouts and Cancellation
Long-running loops should also consider execution limits. Conceptually:- External polling
- Large collections
- Untrusted input
- Slow integrations
- Long-running agent operations
Error Handling
A failure inside a loop body should follow the workflow’s configured error-handling behavior. Possible strategies include:Loop Progress
A useful loop should make measurable progress. For example:- An item position
- A page number
- A batch identifier
- A task status
- A completion flag
- Remaining work
Testing Loops
Loop behavior should be tested across several scenarios.Zero iterations
The continuation decision is initially false.One iteration
The decision is initially true but becomes false after one iteration.Multiple iterations
The decision remains true for several iterations before becoming false.Non-terminating state
The continuation decision never becomes false. The workflow should have an appropriate safeguard such as:- Maximum iterations
- Timeout
- Cancellation
- External execution limit
Loop Observability
Long-running loops should be observable. Useful information includes:- Current iteration
- Progress
- Start time
- Duration
- Current operation
- Failure count
- Retry count
- Termination reason
Loop Design Pattern
A reliable loop generally follows this pattern:Common Use Cases
Loop behavior is useful for:- Collection processing
- Batch processing
- Pagination
- Polling external services
- Repeated validation
- Incremental synchronization
- Waiting for state changes
- Iterative analysis
- Multi-step record processing
- Repeated application operations
Best Practices
- Define a clear termination condition.
- Make sure the loop can eventually exit.
- Update the state used by the continuation decision.
- Keep loop bodies focused.
- Avoid unnecessary nested loops.
- Use retry policies for transient execution failures.
- Consider maximum iteration limits.
- Add delays or backoff when polling external services.
- Use timeouts for potentially unbounded execution.
- Support cancellation for long-running workflows.
- Monitor progress in production workflows.
- Test zero, one, multiple, and non-terminating scenarios.
- Keep business logic separate from orchestration logic.
Current BindAI Scope
BindAI supports loop-based workflow orchestration as part of its workflow capabilities. Loop behavior can be used to coordinate:- Repeated agent execution
- Repeated tool execution
- Collection processing
- Batch processing
- Conditional repetition
- External polling
- Iterative validation
- Multi-step repeated operations
- Retry and reliability patterns
API Accuracy
The workflow concept described here is iterative execution. Implementation details such as:- Loop classes
- Builder methods
- Predicate signatures
- Loop-body registration
- Exit-path registration
- Iteration limits
- Timeout configuration
- Cancellation behavior
- Loop serialization
Summary
Loops allow BindAI workflows to repeat intentional workflow logic. A typical loop follows:Loops repeat intentional workflow behavior; retries recover from execution failures.Specific loop APIs should only be documented after they are verified against the current BindAI implementation and tests.
