Nodes/Akatz-Loop-Nodes/For Loop Close | akatz-loops
ComfyUI Node

For Loop Close | akatz-loops

The other half of ComfyUI's for loop — where the magic happens

By akatz-ai·Created about a year ago·Updated 8 months ago· 23
For Loop Close | akatz-loops
  • flow_control
  • initial_value1
  • initial_value2
  • initial_value3
  • initial_value4
  • value1
  • value2
  • value3
  • value4

If ForLoopOpen is the top of your loop, ForLoopClose | akatz-loops is where the actual looping happens. The open node just announces the loop and hands out the counter; the close node is the one that looks at everything wired between the two, clones the whole region as a fresh subgraph, and runs it again until the counter hits zero. It's the busiest, cleverest node in the pack, and it's a genuinely good demonstration of what execution model inversion bought ComfyUI (PR 2666 - "execution model inversion", the engine feature that lets nodes rebuild their own part of the graph at runtime).

How it works

At runtime the close node builds an internal _ForLoopCounter node (feed it the current counter, get back the decremented value plus a "should continue" boolean), then expands your whole loop body into a fresh WhileLoopClose graph via GraphBuilder. Every contained node between open and close gets copied with a stable display ID, reconnected internally, and executed - that's why each iteration is a real isolated run and why VRAM doesn't pile up the way feedback-wire loops do. When the counter hits zero, it stops cloning and passes the final values out.

Inputs and outputs that matter

  • flow_control (required) - the token that came out of your ForLoopOpen. Route it through the body and into here; this is how the close node finds its matching open node and knows where the loop ends.
  • initial_value1 … initial_value4 (optional) - carry state forward between iterations. Wire your value1…value4 outputs from the open node through whatever the body does, then back into these sockets. Each iteration starts from these values and the outputs below reflect the final state.
  • value1 … value4 (outputs) - the loop's final values, after every iteration has run. Wire these into whatever consumes the result (a VAE decode, a save node, another loop).

The sockets are all * (any type), so you can thread latents, models, strings, or your pack's custom ACCUMULATION objects through the loop - which is how you pair it with Accumulate | akatz-loops to collect every iteration's output into one list.

Installing it

Same pack, same drill as its sibling:

cd ComfyUI/custom_nodes
git clone https://github.com/akatz-ai/ComfyUI-Execution-Inversion
# then restart ComfyUI

Or via ComfyUI Manager, searching "Akatz-Loop-Nodes". Only dependency is opencv-python, no model downloads.

Where people get burned

The most common failure: leaving the initial_value sockets unwired and expecting the close node to remember the open node's values automatically. It won't - you have to explicitly thread values through the body and back in. Second classic: mixing up which loop the flow_control belongs to if you nest loops; the close node pairs to the open node whose flow_control you fed it, so keep the pairs visually obvious on the canvas.

A practical note on cost: loops re-execute the body from scratch each pass, so the total time is iterations × body time. That's expected, but it means a 10-iteration loop around a full sampler is ten full generations - not a batch, a serial run. Design for it.

CategoryAkatz Loop Nodes/Flow

Inputs (5)

NameTypeDefaultDescription
flow_controlFLOW_CONTROL
initial_value1opt*
initial_value2opt*
initial_value3opt*
initial_value4opt*

Outputs (4)

NameTypeDescription
value1*
value2*
value3*
value4*