Loop [[Groups]] are colorless groups with a number (positive integer) in their label. When all of the nodes inside the group are complete, they will all reset and be executed again. This will repeat until the number of loops defined in the label is reached, or if there is an arrow pointing to the group from within, which is rejected (using [[Choice arrows]]).
Arrows exiting the group will be pass along the values from the final loop.
The number of the current loop can be injected using the "{{#}}" variable. If the group is nested, the higher level group iteration number are accessible as "{{##}}", "{{###}}", ect.
![[Pasted image 20240724164128.png]]
Loop groups are the one place in a cannoli where the flow of arrows can turn back in on itself. Arrows can come from a node inside a loop group and point to the loop group itself. On the next loop, the new variable for that variable will be used.
Loop groups are useful for refining pieces of text over several iterations, setting up chat interactions using [[Action nodes]], or retrying the same process until it is successful or meets some criteria.
## Reflexive arrows
When an arrow starts inside of a loop group and points to the group itself, the value from that arrow will be used on the next loop. On the first loop, any references to that variable will be blank, unless you have another arrow loading that variable from the outside. In that case, the external arrow will be used on the first loop, and the reflexive arrow will be used for the rest of the loops.
![[Pasted image 20240724165556.png]]
In this example, we ask the LLM to critique and then re-write a haiku. On the first loop, the haiku will come from the content node outside the loop, but after that the haiku will come from the AI node within the group.
## Conditional loops
If there is a [[Choice arrows|choice arrow]] inside the group, you can use it to create conditional loops. When a reflexive arrow from within the group is rejected, the loop group won't repeat again.
You can use this functionality to stop looping when some criteria is met, using an AI node to decide.
![[Pasted image 20240724173311.png]]
In this example, we use a [[Floating nodes|floating node]] to load the initial haiku and then store the output for each loop.
- If the haiku is correct, and the second [[AI nodes|AI node]] chooses "yes", both of the "no" arrows will be rejected, so the loop won't repeat again.
- If the haiku is incorrect, and the AI node chooses "no", the reflexive arrow pointing at the group as well as the arrow pointing at the next AI node will activate. When all the nodes are complete, the loop will restart if it hasn't reached the limit, because the reflexive arrow wasn't rejected.