Challenge: Each loop iteration needs access to all previous iteration results

Describe the problem/error/question

I’m working on a workflow where I need to accumulate results from each loop iteration and pass them to the next iteration. Each iteration needs to know what was done in previous iterations to avoid duplicating the repetition in the generated content. I’m generating content paragraph by paragraph. N8N sends data via http request to the workflow in dify.

What I’m trying to achieve:

I have a loop that processes items and generates content. Each iteration must:

  1. Know what content was already generated in previous iterations
  2. Generate new content based on new data and what’s already done
  3. Add its result to the accumulated output

Example flow:

  • Iteration 1: done = "" (empty) → generates “Result A” → done = "Result A"
  • Iteration 2: done = "Result A" → generates “Result B” → done = "Result A\nResult B"
  • Iteration 3: done = "Result A\nResult B" → generates “Result C” → done = "Result A\nResult B\nResult C"

The challenge: I need to pass the accumulated “done” variable from one loop iteration to the next within the same loop execution. The number of iterations varies based on input data size (number of headings in the brief).

What I’ve tried:

  1. Using $workflow.staticData - getting “Cannot set properties of undefined” errors
  2. Trying to reference previous iteration results - not sure how to access them properly
  3. Accumulating “done” in google docs, but it doesn’t seem like the the best possible solution.

Question: What’s the best practice in n8n for maintaining and updating state across loop iterations? Is there a pattern for accumulating results where each iteration builds upon the previous ones? For example, in make.com for the same task I used “set variable” and “get variable” nodes. With that I was able to pass data from the finished iteration to the next iteration.

Any examples or guidance would be greatly appreciated!

Please share your workflow

This is a simplified version of the workflow.

Information on your n8n setup

  • n8n version:: 1.97.1
  • **Running n8n via Docker

I write a demo version of this and not sure if this is what you expected

Basically the loop will generate A eleven times

and we can get A,A,A,A,A,A,A,A,A,A in the end.

Hey @JCS ,

I recreated the similar workflow you shared.

Hope this will solve your problem

I tried adding as much explanation I can in the workflow itself, but still if you have any doubt like how it is working…feel free to reply to this message.

This workflow is updated in way that you can directly use this, just replace the Sample API Call node with the actual content_writer node; don’t forgot to activate the node.


How I did this?

The Problem:-

  • The problem you are facing is that you were not able to pass the previous accumulated output to the next iteration while making the api call right?
  • So for that You can just see how I setup the variables from the start of the loop and updating the same on each iteration [ Prepare Iteration Data node].
  • Similarly have a look at Accumulated content node, this is the node where I actually added all the previous outputs and combine them together.
  • and yes few other expressions are used to get the desired output at the end, so feel free to look at the workflow, if not understood anything, ask me, will help.
2 Likes

I have implemented this solution in the workflow and it works as intended. I’m honestly impressed by your knowledge. Thanks for working solution and clear explaination.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.