AI Agent Iternation Problem

Problem on “Rewrite” agent to feedback new message to user.

Describe the problem/error/question

I build a workflow to connect to Telegram, and AI agent help to generat 60 words story
case:

  1. User input: e.g A super hero
  2. AI agent will generate a 60 words story about ‘super hero’
  3. Then the story will feedback to Telegram to ask user OK, End Process or NOT OK,
  4. Then Text classifier will split OK, End Process or NOT OK to do different process respectively
  5. If NOT OK, then Rewrite agent will help to rewrite the story again and feedback to telegram to ask user again
    My problem is the Rewrite Agent is able to generate a new story, but when it feeds back to Telegram to ask chat user, it cannot show the new generate story.
    How can I fix this problem. I also try to specify {{ $(‘Rewrite’).item.json.output }}, but it seems cannot recognise this node.
    Can any body help ?

What is the error message (if any)?

Please share your workflow

(Select the nodes on your canvas and use the keyboard shortcuts CMD+C/CTRL+C and CMD+V/CTRL+V to copy and pa{
  "nodes": [
    {
      "parameters": {
        "updates": [
          "message"
        ],
        "additionalFields": {}
      },
      "type": "n8n-nodes-base.telegramTrigger",
      "typeVersion": 1.2,
      "position": [
        0,
        144
      ],
      "id": "317634b8-2116-44d2-af52-43879975b5af",
      "name": "Telegram Trigger",
      "webhookId": "ea5cd90d-93ad-4d44-83b0-e01b0cf52cc9",
      "credentials": {
        "telegramApi": {
          "id": "1twgKWsg6g1Dtqzn",
          "name": "Telegram n8n forida Training"
        }
      }
    },
    {
      "parameters": {
        "promptType": "define",
        "text": "={{ $json.message.text }}",
        "options": {
          "systemMessage": "You are a blog writer. Write a 60 words short story based on the user input. Output only the blog content and nothing else.\n"
        }
      },
      "type": "@n8n/n8n-nodes-langchain.agent",
      "typeVersion": 3.1,
      "position": [
        224,
        144
      ],
      "id": "e7daf30a-c4ae-42bb-bc99-28102bd6cb85",
      "name": "AI Agent"
    },
    {
      "parameters": {
        "options": {}
      },
      "type": "@n8n/n8n-nodes-langchain.lmChatDeepSeek",
      "typeVersion": 1,
      "position": [
        224,
        480
      ],
      "id": "6d33cd6d-824f-4df4-ac19-aabb3e1e331d",
      "name": "DeepSeek Chat Model",
      "credentials": {
        "deepSeekApi": {
          "id": "ZFjXFjQ2DqoWlQJ7",
          "name": "DeepSeek account"
        }
      }
    },
    {
      "parameters": {
        "operation": "sendAndWait",
        "chatId": "={{ $('Telegram Trigger').item.json.message.chat.id }}",
        "message": "=This is a proposed message by AI, please confirm you OK or NOT OK (Click the \"Response\" button):\n{{ $('AI Agent').item.json.output }}\n\n",
        "responseType": "freeText",
        "options": {
          "appendAttribution": false
        }
      },
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1.2,
      "position": [
        576,
        144
      ],
      "id": "e0f77902-0974-4dc2-a3ad-ea70a44d004b",
      "name": "Send message and wait for response",
      "webhookId": "9a705097-0021-4c67-b678-b7f1aa7d85e5",
      "credentials": {
        "telegramApi": {
          "id": "1twgKWsg6g1Dtqzn",
          "name": "Telegram n8n forida Training"
        }
      }
    },
    {
      "parameters": {
        "inputText": "={{ $json.data.text }}",
        "categories": {
          "categories": [
            {
              "category": "true",
              "description": "you agree the blog output"
            },
            {
              "category": "end",
              "description": "user want to end the process"
            },
            {
              "category": "false",
              "description": "user disagree the blog output and want modification"
            }
          ]
        },
        "options": {
          "systemPromptTemplate": "The user is deciding whether to send a blog or not. They will provide you with a decision in the form of text.\n\nPlease classify the text provided by the user into one of the following categories: {categories}, and use the provided formatting instructions below. Don't explain, and only output the json."
        }
      },
      "type": "@n8n/n8n-nodes-langchain.textClassifier",
      "typeVersion": 1.1,
      "position": [
        800,
        16
      ],
      "id": "f950fba7-ce26-4dfb-9a69-aa5ec9be699c",
      "name": "Text Classifier"
    },
    {
      "parameters": {
        "chatId": "={{ $('Telegram Trigger').item.json.message.chat.id }}",
        "text": "no problem, AI will send the blog out 👍",
        "additionalFields": {
          "appendAttribution": false
        }
      },
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1.2,
      "position": [
        1216,
        -192
      ],
      "id": "0d146b7f-efce-4e11-8e16-95707c611655",
      "name": "AI Blog idea accept by user",
      "webhookId": "f0ee3ace-5724-4bc9-8ffc-b14c09453441",
      "credentials": {
        "telegramApi": {
          "id": "1twgKWsg6g1Dtqzn",
          "name": "Telegram n8n forida Training"
        }
      }
    },
    {
      "parameters": {
        "chatId": "={{ $('Telegram Trigger').item.json.message.chat.id }}",
        "text": "Chat discard 😉",
        "additionalFields": {
          "appendAttribution": false
        }
      },
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1.2,
      "position": [
        1216,
        0
      ],
      "id": "f0cd84b4-1836-47c7-9411-922b640740ff",
      "name": "Chat discard",
      "webhookId": "82671f04-8913-4dac-b926-110e587b3261",
      "credentials": {
        "telegramApi": {
          "id": "1twgKWsg6g1Dtqzn",
          "name": "Telegram n8n forida Training"
        }
      }
    },
    {
      "parameters": {
        "promptType": "define",
        "text": "=AI generated blog:\n {{ $('AI Agent').item.json.output }}\n\nUser request:\n{{ $('Send message and wait for response').item.json.data.text }}",
        "options": {
          "systemMessage": "You are a reviewer agent. You take the initial AI generated blog and the User request, and update the blog content accordingly\n"
        }
      },
      "type": "@n8n/n8n-nodes-langchain.agent",
      "typeVersion": 3.1,
      "position": [
        1152,
        240
      ],
      "id": "59b9bdd2-cb97-4fa1-b2d1-c2d4bab033a5",
      "name": "Rewrite"
    }
  ],
  "connections": {
    "Telegram Trigger": {
      "main": [
        [
          {
            "node": "AI Agent",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "AI Agent": {
      "main": [
        [
          {
            "node": "Send message and wait for response",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "DeepSeek Chat Model": {
      "ai_languageModel": [
        [
          {
            "node": "AI Agent",
            "type": "ai_languageModel",
            "index": 0
          },
          {
            "node": "Text Classifier",
            "type": "ai_languageModel",
            "index": 0
          },
          {
            "node": "Rewrite",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Send message and wait for response": {
      "main": [
        [
          {
            "node": "Text Classifier",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Text Classifier": {
      "main": [
        [
          {
            "node": "AI Blog idea accept by user",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Chat discard",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Rewrite",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Rewrite": {
      "main": [
        [
          {
            "node": "Send message and wait for response",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "pinData": {},
  "meta": {
    "templateCredsSetupCompleted": true,
    "instanceId": "6a0047fe2b9c895f71f01671e5ed78f15429728631ff5ee2f135076afdde178a"
  }
}ste the workflow.)

Share the output returned by the last node

Information on your n8n setup

  • n8n version: 2.2.6
3 Likes

ah the issue is that your Rewrite agent output isn’t being passed correctly to the telegram node. the problem is in your connections — the Rewrite node connects back to ‘Send message and wait for response’, but that node still expects the input from Text Classifier. you need a separate telegram node in the Rewrite flow specifically to send the rewritten story back. or, set up the output format of Rewrite to match what the original Telegram node expects, then use that output field in the message template.

Hmm… may i know the new added telegram node should be added to which position ?

do you mind to give me a workflow digram? I am quite new in n8n. :wink:

1 Like

Hi @Alan_Cheung1 use Set Fields node right before AI agent and when your FLOW is on a REWRITE state after the text classifier route that rewrite branch to the set fields node and there specify a FEEDBACK filed which you will give in the AI agent as a System prompt section, initially that field would be NULL in the first run and would not cause problem but once user rejects and wants to give a feedback the AI agent would consider that as the rewrite branch routes back to the set fields which tells the AI agent instead of NULL the exact feedback. Below is a similar example:
So below system shows you how the you can implement feedback system in your flow, in the incorrect branch add your telegram node and there set the edit fields input accordingly and everything would just work perfectly:

Let me know if this works for you.

Alan_Cheung1, your workflow logic is correct, the issue is just how you are referencing the data

Right now, your Telegram node is still using the output from AI Agent, not from the Rewrite node. So even though Rewrite generates a new story, Telegram keeps showing the old one.

The main issue is this part:
{{ $('Rewrite').item.json.output }}

This often fails because inside loops or wait nodes, .item can break or not point to the latest data.


Simple fix

In your Telegram node (the one after Rewrite), change it to:

{{ $json.output }}

This works because when Rewrite runs, it becomes the current node, so $json already contains the new story.


Why your current setup fails

  • $('AI Agent') → always points to the first story

  • $('Rewrite') → may not resolve properly after “wait for response”

  • $json → always gives you the latest node data (this is the safest)


Important tip

Since you are looping back to the same Telegram node, avoid referencing specific nodes like:

  • $('AI Agent')

  • $('Rewrite')

Instead, always use:
{{ $json.output }}

This ensures:

  • first run → shows original story

  • rewrite → shows updated story


Clean approach

Let each step pass its own data forward

So your flow becomes stable:
AI Agent → Telegram → Classifier → Rewrite → Telegram (same node, but now using $json.output)


If it still doesn’t show, check the Rewrite node output to confirm the field is actually called output. Sometimes it can be text depending on the model.

the send loop feed from rewrite agent. But seems cannot get the output.

The ai agent cannot get ‘rewrite’ agent output too.

it doesn’t work. I use {{ $json.output }} in the second loop in telegram node

1 Like

Alan_Cheung1, I see the issue clearly from your screenshot

The problem is not the expression, {{$json.output}} is correct. The issue is that in the second loop, the Telegram node is still receiving data from the previous context, not from the Rewrite node.

When you use “Send and Wait for Response”, n8n creates a new execution context. After the user replies, the data coming forward is from the Telegram node, not from Rewrite anymore. That’s why $json.output no longer points to the rewritten story.

What’s actually happening

Flow is like this:

AI Agent → Telegram (wait) → Classifier → Rewrite → Telegram

But when it loops back, the Telegram node is now working with its own data (data.text), not the Rewrite output. So the rewritten story is not being carried forward.

Fix (reliable way)

You need to store the rewritten story before going back to Telegram

Add a Set node after Rewrite:

Field: story

Value: {{$json.output}}

Then in your Telegram node, use:

{{$json.story}}

Why this works

The Set node makes sure the rewritten story becomes part of the current flow data. So even after the wait node, it is still available.

Important tip

Avoid depending on node references like:

$(‘Rewrite’)…

In loops and wait nodes, they often break or point to old data. Always pass values forward using $json.

If it still shows the old story, check the input of the Telegram node and confirm that story is present there. That will tell you exactly where the data is getting lost.

You can send me a dm, if that’s okay by you

@Alan_Cheung1 Blessing’s suggestion with the Set node is the right approach — storing the rewritten output in a field before looping back is the standard pattern for this. if the Set node approach still doesn’t work, one more thing to check: make sure the Telegram node you’re using in the second loop is actually receiving the Set node output as input. sometimes the connection gets missed visually but the data doesn’t flow through. if you share a screenshot of your updated workflow after adding the Set node, we can see where the data is getting lost.

The issue is with the expression in your “Send message and wait for response” node. It’s hardcoded to reference the first AI Agent output ($('AI Agent').item.json.output), so on the second pass it still pulls the original story instead of the rewritten one.

You need to make the message expression dynamic so it picks up the output from whichever node just ran. The cleanest way: use $json.output in the Telegram message node instead of referencing a specific node name. Since the Rewrite node connects directly into the Telegram node, $json.output will always grab the current input.

Change your message text to:

This is a proposed message by AI, please confirm you OK or NOT OK:
{{ $json.output }}

This way it works on the first run (from AI Agent) and also on subsequent rewrites (from Rewrite node) since both output to the same output field.

Also worth noting: your Rewrite node is on n8n version 2.2.6 which is quite old. Some expression resolution behaviors improved in later versions, so if possible consider upgrading.

1 Like

Exactly, great summary. Using $json keeps it stable through the loops — that’s the pattern that works. Thanks for breaking it down!

Thank you very much.:100:

the problem has been solved, as I use the follow system prompt. AND the critical thing is I upgraded the n8n platform to version 2.14.2

If I don’t upgrade version 2.14.2 , the problem is still existing. :wink:

This is a proposed message by AI, please confirm you OK or NOT OK: {{ $json.output }}

Awesome, glad you got it sorted! Upgrading to 2.14.2 definitely helped — earlier versions had some rough edges with data flow in loops. good to have the solution documented for the next person hitting this :+1: