How to pass data between iterations in a loop?

I want to achieve what seems like a very basic thing:
An AI should loop over a database, gradually building up its “understanding” during the loop.

So I don’t need a loop over predefined items — I need a standard cycle with accumulation.

The idea:

AI looks at the user prompt, prepares a query,

Executes the query, gets a response,

Prepares a new query based on the response,

And gradually builds a research report over multiple iterations.

I tried using this approach:

But it didn’t work — I get an error saying the node doesn’t exist.

How should I properly implement this behavior in n8n?
Or this is not task for n8n?

Hi @Ars_N

Open a separate workflow and test this suggestion below.

Remember to check the credentials and adjust as needed.

If you find any part in Portuguese, translate it to your language, because I don’t speak English. So I need to take your message, translate it to Portuguese, understand the problem, see if I can help in any way, answer the question, translate it to English and then post it to the community. I wish you all the best!

If this suggestion solved your problem, please mark my post as a solution (blue box with check mark) so that this ongoing discussion does not distract others who want to find the answer to the original question and click the heart. Thank you :+1:

2 Likes

Thanks. I did it

Summary
{
  "name": "strange n8 loop",
  "nodes": [
    {
      "parameters": {
        "options": {}
      },
      "type": "@n8n/n8n-nodes-langchain.chatTrigger",
      "typeVersion": 1.1,
      "position": [
        0,
        0
      ],
      "id": "8a6cb4cc-d4ac-45bd-b276-321922ee943e",
      "name": "When chat message received",
      "webhookId": "96d9f8f5-722e-4593-a5bb-5c3efd3bc59c"
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict",
            "version": 2
          },
          "conditions": [
            {
              "id": "c49e4542-7cdc-4fdf-8c09-c082d85c3e90",
              "leftValue": "={{ $json.i }}",
              "rightValue": 9,
              "operator": {
                "type": "number",
                "operation": "gt"
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [
        740,
        0
      ],
      "id": "21df1251-b02f-4d99-8b0b-9d5ee53a4c70",
      "name": "If1"
    },
    {
      "parameters": {
        "jsCode": "const i = $('Code').first().json.i + 1\nreturn { i }"
      },
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        520,
        0
      ],
      "id": "f70efdd9-84b3-45fc-8bcb-6752d31d5e55",
      "name": "Code1"
    },
    {
      "parameters": {
        "jsCode": "let i\ntry {\n  i = $('Code1').first().json.i\n} catch {\n  i = 1\n}\nreturn { i }"
      },
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        220,
        0
      ],
      "id": "468b51f5-ebb9-4db1-a9aa-591fa08d3822",
      "name": "Code"
    }
  ],
  "pinData": {},
  "connections": {
    "When chat message received": {
      "main": [
        [
          {
            "node": "Code",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Code1": {
      "main": [
        [
          {
            "node": "If1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Code": {
      "main": [
        [
          {
            "node": "Code1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "If1": {
      "main": [
        [],
        [
          {
            "node": "Code",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "4535727c-3fd0-48f4-b938-cb8df9bda606",
  "meta": {
    "templateCredsSetupCompleted": true,
    "instanceId": "525f549c3b59914bbbed57afe22c1d9536919c3a6ce21e930ce8eb0d62da5189"
  },
  "id": "MKFdNvFow8w2ky3j",
  "tags": []
}```

Interesting why is so not obviously for implementation? Just loop… n8n, what you are?

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