{
  "name": "Tavily search agent (upgrade)",
  "nodes": [
    {
      "parameters": {
        "updates": [
          "message"
        ],
        "additionalFields": {}
      },
      "type": "n8n-nodes-base.telegramTrigger",
      "typeVersion": 1.5,
      "position": [
        0,
        0
      ],
      "id": "d165283d-fb0c-4651-a6f3-03fd3357a9b7",
      "name": "Telegram Trigger",
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "promptType": "define",
        "text": "={{ $json.message.text }}",
        "options": {
          "systemMessage": "You are a Smart Research Agent equipped with the Tavily search tool. Your main responsibility is to search the web using Tavily whenever accurate, factual, or up-to-date information is needed.\n\n### SYSTEM INSTRUCTIONS & BEHAVIOR RULES:\n\n1. TOOL USAGE (Tavily):\n   - You MUST use the Tavily tool whenever the user asks a question that requires searching the web, looking up facts, fetching current news, or researching any topic.\n   - Do NOT try to answer from your internal knowledge if the prompt requires factual accuracy or recent information; always call Tavily first.\n\n2. CASUAL CONVERSATION:\n   - For simple greetings, standard pleasantries, or basic social chat that does NOT require research, respond directly without calling Tavily.\n\n3. OUT OF SCOPE / NO INFORMATION:\n   - If the user asks for personal advice, medical/legal opinions, illegal tasks, or if the Tavily search yields no useful results, respond politely indicating that you don't have this information or that it falls outside your role.\n\n4. LANGUAGE & TONE:\n   - You MUST respond strictly in Egyptian Colloquial Arabic (\u0627\u0644\u0639\u0627\u0645\u064a\u0629 \u0627\u0644\u0645\u0635\u0631\u064a\u0629).\n   - Maintain a friendly, polite, helpful, and professional tone at all times.\n\n5. OUTPUT FORMATTING & NO SOURCES:\n   - Do NOT mention sources, links, or where the information came from at the end of sentences or responses.\n   - Present the information clearly, concisely, and neatly.\n   - Use double line breaks (a blank line) between every sentence/point to ensure the output is well-spaced and easy to read."
        }
      },
      "type": "@n8n/n8n-nodes-langchain.agent",
      "typeVersion": 3.1,
      "position": [
        672,
        -128
      ],
      "id": "05306f65-e8e1-404c-88c8-c6b310e88cc5",
      "name": "AI Agent",
      "retryOnFail": true,
      "onError": "continueErrorOutput"
    },
    {
      "parameters": {
        "model": {
          "__rl": true,
          "mode": "list",
          "value": "gpt-5-mini"
        },
        "builtInTools": {},
        "options": {}
      },
      "type": "@n8n/n8n-nodes-langchain.lmChatOpenAi",
      "typeVersion": 1.3,
      "position": [
        528,
        112
      ],
      "id": "320fdca1-c6c7-4e6f-945a-75ab54780f18",
      "name": "OpenAI Chat Model",
      "credentials": {
        "openAiApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "sessionIdType": "customKey",
        "sessionKey": "={{ $json.message.chat.id }}",
        "contextWindowLength": 10
      },
      "type": "@n8n/n8n-nodes-langchain.memoryBufferWindow",
      "typeVersion": 1.4,
      "position": [
        752,
        112
      ],
      "id": "fe172bb5-1874-4d5b-8271-f25f87e3b116",
      "name": "Simple Memory"
    },
    {
      "parameters": {
        "chatId": "={{ $('Telegram Trigger').item.json.message.chat.id }}",
        "text": "={{ $json.output }}",
        "additionalFields": {
          "appendAttribution": false,
          "parse_mode": "Markdown"
        }
      },
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1.2,
      "position": [
        1120,
        -256
      ],
      "id": "c835fa6d-9b4f-4488-aef9-35318f43c33b",
      "name": "Send a text message",
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "query": "={{ /*n8n-auto-generated-fromAI-override*/ $fromAI('Query', ``, 'string') }}",
        "options": {}
      },
      "type": "@tavily/n8n-nodes-tavily.tavilyTool",
      "typeVersion": 1,
      "position": [
        992,
        128
      ],
      "id": "94aa601c-bd45-4c7d-8efb-c04c320dac91",
      "name": "Search in Tavily",
      "notesInFlow": false,
      "credentials": {
        "tavilyApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict",
            "version": 3
          },
          "conditions": [
            {
              "id": "54880703-e847-4525-aa6a-ee423c314cfa",
              "leftValue": "={{ $json.message.text }}",
              "rightValue": "=",
              "operator": {
                "type": "string",
                "operation": "notEmpty",
                "singleValue": true
              }
            },
            {
              "id": "a1c15e70-b788-44d7-99a7-9d4491495c96",
              "leftValue": "={{ $json.message.text.replace(/[\\p{Extended_Pictographic}\\u200d\\uFE0F\\s]/gu, '') }}",
              "rightValue": "",
              "operator": {
                "type": "string",
                "operation": "notEmpty",
                "singleValue": true
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.3,
      "position": [
        224,
        0
      ],
      "id": "637dc5eb-d943-412f-8358-4279c4efe533",
      "name": "If"
    },
    {
      "parameters": {
        "chatId": "={{ $('Telegram Trigger').item.json.message.chat.id }}",
        "text": "Send a text message please.",
        "additionalFields": {
          "appendAttribution": false
        }
      },
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1.2,
      "position": [
        576,
        336
      ],
      "id": "f11a47e9-69ab-4b81-aa14-b1f0f3aa8f36",
      "name": "Send a text message1",
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "chatId": "={{ $('Telegram Trigger').item.json.message.chat.id }}",
        "text": "Sorry ! can you try again later.",
        "additionalFields": {
          "appendAttribution": false,
          "parse_mode": "Markdown"
        }
      },
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1.2,
      "position": [
        1120,
        -48
      ],
      "id": "79a14f0f-7d6b-4fd4-b6bc-cbde6239cf66",
      "name": "Send a text message2",
      "credentials": {
        "telegramApi": {
          "name": "<your credential>"
        }
      }
    }
  ],
  "connections": {
    "Telegram Trigger": {
      "main": [
        [
          {
            "node": "If",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "OpenAI Chat Model": {
      "ai_languageModel": [
        [
          {
            "node": "AI Agent",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Simple Memory": {
      "ai_memory": [
        [
          {
            "node": "AI Agent",
            "type": "ai_memory",
            "index": 0
          }
        ]
      ]
    },
    "AI Agent": {
      "main": [
        [
          {
            "node": "Send a text message",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Send a text message2",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Search in Tavily": {
      "ai_tool": [
        [
          {
            "node": "AI Agent",
            "type": "ai_tool",
            "index": 0
          }
        ]
      ]
    },
    "If": {
      "main": [
        [
          {
            "node": "AI Agent",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Send a text message1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Send a text message": {
      "main": [
        []
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1",
    "binaryMode": "separate",
    "availableInMCP": false
  },
  "versionId": "80c3ba06-a8db-43db-bd66-fc006496030b",
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "nodeGroups": [],
  "id": "BvegKi8mySdSKNCW",
  "tags": []
}