Creating conditional loop

Hi! I’m new to coding, so apologies if my question seems basic—I couldn’t find a clear answer elsewhere.

I’m trying to build an n8n workflow that does the following:

  1. Retrieves a keyword from a Google Sheets node.
  2. Has an AI agent search the keyword on the web.
  3. Stores the search results in another Google Sheet.

Now, I want each keyword to be searched 5 times, with each result indexed like: keyword_1, keyword_2, …, keyword_5.

I understand that I need to create a loop and use an IF node to repeat the process 5 times. However, I’m not sure how to define the loop condition properly. From what I’ve read, I need to use an index variable to increment the loop, using something like {{index + 1}}.

I can create a basic loop, but I’m struggling to implement this with two Google Sheet nodes and an AI agent nodes in between. I tried using the execution index as a condition in the IF node, which worked temporarily—but once the execution index goes beyond 5, the loop stops functioning correctly.

Any advice or suggestions would be greatly appreciated!

Hi, it’s important to understand why do you need to search each keyword 5 times in google sheets, and why it’s a problem if after 5 times it stops working correctly - you only need it 5 times per keyword, right? It will also be very helpful if you share the workflow with as. Right now it’s still unclear.

Provide the workflow code or screenshot would be helpful.

From the text. Looks like your config is right.

Or what you expect when IF : excution index > 5 happen?

Thank you for your replies, peachotomoaorit and darell_tw,
The intended behavior is as follows:

  • when IF : runindex < 5, I expect the workflow to repeat searching the same keyword
  • when IF : runindex = 5, the workflow should move to the second row of the first Google Sheet, retrieve the next keyword, and perform 5 searches using that new keyword.

The current setup using the IF node works correctly in general. However, there’s a critical issue:
Using runIndex as the condition is not reliable for controlling the loop.
Once runIndex exceeds 5, the condition IF: runIndex < 5 becomes false, causing the loop to terminate prematurely. As a result, the keyword is searched only once instead of the intended 5 times.

I hope I could explain the issue clearly.
Thank you in advance for your help.

Here is a screenshot of my workflow:

I slighly modified the workflow and want to confirm it’s what you expected?

Each keyword doing AI search for 5 times.

After that. Switch to another keyword in google sheet.