Accumulating data across loop iterations in n8n workflows?

Need help with data accumulation in n8n loops

I’m trying to create a workflow where an AI gradually builds understanding by looping through a database. Here’s what I want to do:

  1. AI reads user input and creates a query
  2. Runs the query and gets results
  3. Makes a new query based on those results
  4. Builds a report over multiple rounds

I’m not looking to loop over a fixed list. Instead, I need a way to keep adding to the data as the loop goes on.

I tried using a method I found online about making my own loop counter, but it didn’t work out. I got an error saying the node wasn’t there.

Does anyone know the right way to do this kind of data building in n8n loops? I’d really appreciate some guidance on how to set this up properly in my workflow. Thanks!

have u tried using the ‘set’ node to store accumulated data? u could update it each loop iteration. another option is to use the ‘function’ node with a global variable to keep track. both ways let u build up info across multiple rounds. hope this helps with ur AI project!

n8n offers a flexible way to accumulate data across iterations by using a Function node to manage dynamic variables. In my experience, it works well to initialize an accumulator before entering the loop and then update it within the loop to collect new information. This practice avoids issues related to scope and missing nodes. I would set up a Function node at the start of the workflow to create an empty array, then use another Function node in each loop iteration to append the latest data. This method provides a scalable path to build the desired report.

I’ve dealt with similar challenges in n8n workflows, and I found that using a combination of Function nodes and the Loop Over Items node works really well for accumulating data dynamically. Here’s what I’d suggest:

Set up a Function node at the start to initialize an empty array for your accumulated data. Then, inside your loop, use another Function node to process each iteration’s results and push them into that array.

The key is to use the $executionId variable to maintain state across iterations. Something like:

$(‘myAccumulatedData’ + $executionId) = $(‘myAccumulatedData’ + $executionId) || ;
$(‘myAccumulatedData’ + $executionId).push(itemData);

This approach lets you build up your report data gradually without losing information between loop cycles. After the loop, you can retrieve and process the full dataset for your final AI analysis and report generation.

Just make sure to clear the accumulated data at the start of each workflow run to avoid carrying over old information.