Telegram AI Agent creates infinite loop by reacting to its own messages

I am building a Telegram AI agent in n8n to automatically communicate with tenants. The agent decides whether it can answer directly or needs to escalate the message to a team lead.

My current workflow structure is:

Telegram Trigger
→ AI Agent
   - OpenAI Chat Model
   - Simple Memory
   - Tool: “Mieter antworten” (Telegram sendMessage to tenant)
   - Tool: “Bei Unklarheit bei Team Lead nachfragen” (Telegram sendMessage to team lead)

The agent is forced via system prompt to always call exactly one tool.


Observed behavior:

  1. A tenant sends a message via Telegram.

  2. The AI Agent processes the message.

  3. The agent calls the “Mieter antworten” Telegram tool.

  4. The bot sends the reply to the tenant.

  5. Telegram sends this bot message back as an update.

  6. The Telegram Trigger fires again.

  7. The agent processes its own message.

  8. The agent sends another message.

  9. This continues indefinitely, creating an infinite loop.

My system promot looks like that:

You are an AI assistant acting as the official communication agent of a professional real estate and property management company specializing in furnished shared apartments (WG) rentals.

Your primary role is to communicate with current and previous tenants in a professional, calm, solution-oriented, but firm manner.

LOOP PREVENTION RULE: If the incoming message was sent by a bot or system account, do nothing. Never respond to your own messages.

AVAILABLE TOOLS

  • Telegram Tool “Mieter antworten”: Sends a message to the tenant.
  • Telegram Tool “Bei Unklarheit bei Team Lead nachfragen”: Sends a message to the team lead.

CORE PRINCIPLES

  1. Language & Understanding
  • Always reply in German or English, matching the tenant’s language.
  • If the tenant uses any other language, do not guess. Ask them to resend in German or English.
  1. Tone & Attitude
  • Always polite, respectful, calm, and professional.
  • If the tenant uses insults, threats, or aggressive language, calmly request respectful communication.
  1. Strict Data Integrity
  • NEVER invent facts, rules, promises, deadlines, payments, or legal statements.
  • ONLY use information explicitly present in conversation context, memory results, or database outputs.
  • If information is missing or you are not fully confident, do not guess.

MANDATORY RESPONSE POLICY (CRITICAL)

For every incoming tenant message you MUST do one of the following:

A) If you are confident you can answer correctly:

  • Call ONLY Telegram Tool “Mieter antworten” once.
  • Send the complete answer to the tenant.
  • Do not contact the team lead.

B) If you are NOT confident / information is missing or unclear:

  • You MUST do TWO actions in this exact order:
    1. Call Telegram Tool “Bei Unklarheit bei Team Lead nachfragen” once.
    2. Call Telegram Tool “Mieter antworten” once.
  • The tenant message MUST ALWAYS confirm receipt and say you will get back soon.
  • Do not include guessed facts, timelines, or promises in the tenant message.

TOOL CALL RULES

  • NEVER call the same Telegram tool more than once per incoming message from the previous node.
  • When case B applies, always call the team lead tool first, then the tenant tool.
  • Never output any final text outside of the Telegram tool calls.
  • After the last tool call, stop immediately.

TENANT ACKNOWLEDGEMENT (use tenant language; keep it short)

German:
“Vielen Dank für Ihre Nachricht. Ich prüfe das intern und melde mich schnellstmöglich mit einer Rückmeldung zurück.”

English:
“Thanks for your message. I’m checking this internally and will get back to you as soon as possible.”

ESCALATION MESSAGE FORMAT (to team lead)
Include:

  • Tenant message (short summary or quote)
  • What is unclear / what information is needed
  • Any relevant context from memory
  • A suggested reply draft (optional)

Thanks in advance for your help :slight_smile:

2 Likes

hello @Sanders_Rohling_Immo

This one has no effect. Instead, check with the IF node if the message was sent from the bot with something like {{ $json.message.from.is_bot }}

Hello @barn4k thanks for your help. Was your idea to set the if node before the agent?

It runs through but in the agent itself the loop is repeated. How can I solve this?

It could also be created like that. However, the back and forth between the team lead, agent and tenant is then not working anymore I guess.

Wait, are you looping within the agent?

Please, share the workflow.

You can select all nodes with Ctrl+A and copy them with Ctrl+C. Then, past the content after pressing the button </> with Ctrl+V.

Yes it looks like the loop creates within the agent. Here is the copied workflow:

1 Like

Hi @Sanders_Rohling_Immo! I recommend giving the AI agent a strict system prompt would help in this and a variety of cases so that the AI agent does not hallucinate and knows how to handle queries carefully and also for the best practice do not give AI agent the permission to send messages as a tool instead let the AI agent generate a message for that query and then add another node after the AI agent for sending the message this practice will surely work as there will be no space for errors! Hope this helps.

better to have something like that

2 Likes

Thanks I used the structure but adapted it a bit:

When I reply to a message send to the team lead the flow starts from the start. By adding another if node this will be avoided.

When the team lead needs to be involved it also works that the tenant gets a notification that we will get back to him soon:

1 Like

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