{
  "name": "Cheaper, faster, accurate answers with memory summarization & dynamic routing!",
  "nodes": [
    {
      "id": "fccc9f50-71fa-4e25-9b15-8fd540ddc2fa",
      "name": "Model Selector",
      "type": "@n8n/n8n-nodes-langchain.modelSelector",
      "position": [
        800,
        1536
      ]
    },
    {
      "id": "cf5b2ea0-78c5-47bf-a3c1-4c59c9a32f76",
      "name": "Structured Output Parser",
      "type": "@n8n/n8n-nodes-langchain.outputParserStructured",
      "position": [
        336,
        1504
      ]
    },
    {
      "id": "3a60caa7-eb1a-4bbd-88fc-55d7a3ffae29",
      "name": "Gemini 2.5 Flash Lite",
      "type": "@n8n/n8n-nodes-langchain.lmChatGoogleGemini",
      "position": [
        720,
        1728
      ]
    },
    {
      "id": "412d35ed-cd49-4d20-b425-2f556ef175b1",
      "name": "Gemini 2.5 Flash",
      "type": "@n8n/n8n-nodes-langchain.lmChatGoogleGemini",
      "position": [
        880,
        1728
      ]
    },
    {
      "id": "2cc40e2c-a2be-435e-8540-9235efc41e08",
      "name": "Gemini 2.5 Pro",
      "type": "@n8n/n8n-nodes-langchain.lmChatGoogleGemini",
      "position": [
        1024,
        1728
      ]
    },
    {
      "id": "35f56404-6998-4952-a7f3-8716712bf96a",
      "name": "Get Chat Memory",
      "type": "n8n-nodes-base.postgres",
      "position": [
        -256,
        1328
      ]
    },
    {
      "id": "5e6058bc-a3b8-4827-954e-85e3a000a986",
      "name": "MarkdownV2",
      "type": "n8n-nodes-base.code",
      "position": [
        1136,
        1264
      ]
    },
    {
      "id": "6ce0f61c-03d1-4bef-827f-ac3c80f48939",
      "name": "Send a text message",
      "type": "n8n-nodes-base.telegram",
      "position": [
        1312,
        1264
      ]
    },
    {
      "id": "662e6beb-7498-42c2-ba0c-6530c100bf97",
      "name": "Fix mime",
      "type": "n8n-nodes-base.code",
      "position": [
        -1184,
        1328
      ]
    },
    {
      "id": "07281d83-6c94-4952-819e-288a4435da24",
      "name": "Typing…",
      "type": "n8n-nodes-base.telegram",
      "position": [
        -1744,
        1184
      ]
    },
    {
      "id": "e211d4af-9ebd-4524-84ef-934349b6b155",
      "name": "get_message (Audio/Video message)",
      "type": "n8n-nodes-base.set",
      "position": [
        -848,
        1328
      ]
    },
    {
      "id": "9c28c37b-1d37-4f23-90a3-872f5e3870a7",
      "name": "Analyze voice message",
      "type": "@n8n/n8n-nodes-langchain.googleGemini",
      "position": [
        -1024,
        1328
      ]
    },
    {
      "id": "ea646e8c-e6d0-47ea-8743-45eade4c1c4d",
      "name": "get_message (text)",
      "type": "n8n-nodes-base.set",
      "position": [
        -1184,
        1152
      ]
    },
    {
      "id": "1ca88076-5c56-4561-bb8a-fdf1db080d2a",
      "name": "Input Message Router1",
      "type": "n8n-nodes-base.switch",
      "position": [
        -1600,
        1312
      ]
    },
    {
      "id": "298fa06c-d3fc-4d20-a2d4-fe879a01527c",
      "name": "Download Voice Message",
      "type": "n8n-nodes-base.telegram",
      "position": [
        -1360,
        1328
      ]
    },
    {
      "id": "5a1dc986-6bfe-4400-bdbf-d4f5817e0f7e",
      "name": "Telegram Trigger",
      "type": "n8n-nodes-base.telegramTrigger",
      "position": [
        -1888,
        1328
      ]
    },
    {
      "id": "0188bc12-4fc1-4149-8c80-edf8e80009ec",
      "name": "Normalize input",
      "type": "n8n-nodes-base.set",
      "position": [
        -464,
        1328
      ]
    },
    {
      "id": "4db66ea5-0817-4326-9991-ab6a905ec0ee",
      "name": "Aggregate",
      "type": "n8n-nodes-base.aggregate",
      "position": [
        -48,
        1328
      ]
    },
    {
      "id": "a4ce42c2-d1cc-4233-9ee7-2aac9a5f0c45",
      "name": "Google Gemini 2.5 Flash Lite",
      "type": "@n8n/n8n-nodes-langchain.lmChatGoogleGemini",
      "position": [
        192,
        1504
      ]
    },
    {
      "id": "5c2cd387-3b15-4869-87da-6873437a0d33",
      "name": "get_error_message",
      "type": "n8n-nodes-base.set",
      "position": [
        -1184,
        1504
      ]
    },
    {
      "id": "d31898fc-4d10-43ea-bc5d-402ac29f3f4b",
      "name": "Agent",
      "type": "@n8n/n8n-nodes-langchain.agent",
      "position": [
        800,
        1328
      ]
    },
    {
      "id": "ee146b55-17b6-414c-be6b-f8416ece7075",
      "name": "Sticky Note",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1984,
        1104
      ],
      "parameters": {
        "width": 1312,
        "height": 624,
        "content": ""
      }
    },
    {
      "id": "e677f7d7-2cfc-476d-bdc0-08b4cf17cb4d",
      "name": "Sticky Note1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -576,
        1248
      ],
      "parameters": {
        "width": 1216,
        "height": 400,
        "content": ""
      }
    },
    {
      "id": "d36b2018-ca63-4c1f-a40e-46d1452753e2",
      "name": "When clicking ‘Execute workflow’",
      "type": "n8n-nodes-base.manualTrigger",
      "position": [
        -1888,
        880
      ]
    },
    {
      "id": "451eb06f-5b47-44c5-86fa-20587cf89870",
      "name": "Create Chat Memory Table",
      "type": "n8n-nodes-base.postgres",
      "position": [
        -1696,
        880
      ]
    },
    {
      "id": "fb4c1415-dd98-4944-8c12-e69f8d445be7",
      "name": "Sticky Note2",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1984,
        848
      ],
      "parameters": {
        "width": 512,
        "height": 208,
        "content": ""
      }
    },
    {
      "id": "50ac5adc-ce42-4f35-84ac-cf8e2bea5d38",
      "name": "Sticky Note3",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        672,
        1248
      ],
      "parameters": {
        "width": 880,
        "height": 624,
        "content": ""
      }
    },
    {
      "id": "97859eb8-951f-4abf-8b89-63299b456304",
      "name": "Update Chat Memory (User and Agent)",
      "type": "n8n-nodes-base.postgres",
      "position": [
        1136,
        1424
      ]
    },
    {
      "id": "632c9d0c-5ab3-46a6-bb4d-0d4bd3341fde",
      "name": "Summarize & Categorize",
      "type": "@n8n/n8n-nodes-langchain.chainLlm",
      "position": [
        192,
        1328
      ]
    },
    {
      "id": "8979f56c-2809-426d-acda-9d455df8836e",
      "name": "Sticky Note4",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1984,
        576
      ],
      "parameters": {
        "width": 512,
        "height": 256,
        "content": "## ⚙️ Database Initialization (Chat Memory Table)\n\n**Purpose:**  \nThis section is responsible for creating and preparing the `chat_memory` table in PostgreSQL. It ensures that chat interactions are st"
      }
    },
    {
      "id": "cf047f39-85cd-41a6-8d2a-76ecec83b6f2",
      "name": "Sticky Note5",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1392,
        432
      ],
      "parameters": {
        "width": 704,
        "height": 640,
        "content": "## 🔵 Input Handling (Telegram Trigger & Preprocessing)\n\n### Purpose:\nThis section receives and processes incoming messages from Telegram. It detects whether the input is text, voice, or unsupported me"
      }
    },
    {
      "id": "3557d1c3-c155-430a-a11b-5bc901111d46",
      "name": "Sticky Note6",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -352,
        496
      ],
      "parameters": {
        "width": 784,
        "height": 720,
        "content": "## 🔴 Chat Memory Retrieval & Context Optimization\n\n### Purpose:\nThis section retrieves past interactions from the database, aggregates them into a single string, and summarizes them before passing the"
      }
    },
    {
      "id": "42fdc20f-2c28-4826-8704-20ffb8792bd7",
      "name": "Sticky Note7",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        704,
        384
      ],
      "parameters": {
        "width": 784,
        "height": 832,
        "content": "## 🟣 Agent Processing & Response Delivery\n\n### Purpose:\nThis section routes the request to the appropriate Gemini model depending on task difficulty, generates the final response, and sends it back to"
      }
    },
    {
      "id": "1c29f5cf-c95e-40fc-9dc0-362dcda71574",
      "name": "Sticky Note8",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -2560,
        1104
      ],
      "parameters": {
        "width": 544,
        "height": 592,
        "content": "## ✅ Key Benefits of This Architecture\n\n### Cost Efficiency\n\n*   Uses cheaper LLMs for summarization and simple queries.\n*   Reserves expensive models only for complex tasks.\n\n### Performance Optimiza"
      }
    },
    {
      "id": "84019987-a071-44d0-8e4b-d80c838007dc",
      "name": "Sticky Note9",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -2496,
        848
      ],
      "parameters": {
        "width": 368,
        "height": 192,
        "content": "## Acknowledgment\n\nA special thank you to Davide for the inspiration behind this template.  \nHis work on the [**AI Orchestrator that dynamically selects models based on input type**](https://n8n.io/wo"
      }
    },
    {
      "id": "0c425ab2-41d3-43c9-86bb-9c7cbfe968a4",
      "name": "Sticky Note10",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1584,
        1648
      ],
      "parameters": {
        "width": 368,
        "height": 192,
        "content": "## 💡 Need Assistance?\n\nIf you’d like help customizing or extending this workflow, feel free to reach out:  \n\n📧 Email: [johnsilva11031@gmail.com](mailto:johnsilva11031@gmail.com)  \n🔗 LinkedIn: [John Al"
      }
    }
  ],
  "connections": {
    "Agent": {
      "main": [
        [
          {
            "node": "MarkdownV2",
            "type": "main",
            "index": 0
          },
          {
            "node": "Update Chat Memory (User and Agent)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Fix mime": {
      "main": [
        [
          {
            "node": "Analyze voice message",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Aggregate": {
      "main": [
        [
          {
            "node": "Summarize & Categorize",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "MarkdownV2": {
      "main": [
        [
          {
            "node": "Send a text message",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Gemini 2.5 Pro": {
      "ai_languageModel": [
        [
          {
            "node": "Model Selector",
            "type": "ai_languageModel",
            "index": 2
          }
        ]
      ]
    },
    "Model Selector": {
      "ai_languageModel": [
        [
          {
            "node": "Agent",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Get Chat Memory": {
      "main": [
        [
          {
            "node": "Aggregate",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Normalize input": {
      "main": [
        [
          {
            "node": "Get Chat Memory",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Gemini 2.5 Flash": {
      "ai_languageModel": [
        [
          {
            "node": "Model Selector",
            "type": "ai_languageModel",
            "index": 1
          }
        ]
      ]
    },
    "Telegram Trigger": {
      "main": [
        [
          {
            "node": "Input Message Router1",
            "type": "main",
            "index": 0
          },
          {
            "node": "Typing…",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "get_error_message": {
      "main": [
        [
          {
            "node": "Normalize input",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "get_message (text)": {
      "main": [
        [
          {
            "node": "Normalize input",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Analyze voice message": {
      "main": [
        [
          {
            "node": "get_message (Audio/Video message)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Gemini 2.5 Flash Lite": {
      "ai_languageModel": [
        [
          {
            "node": "Model Selector",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Input Message Router1": {
      "main": [
        [
          {
            "node": "get_message (text)",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Download Voice Message",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "get_error_message",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Download Voice Message": {
      "main": [
        [
          {
            "node": "Fix mime",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Summarize & Categorize": {
      "main": [
        [
          {
            "node": "Agent",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Structured Output Parser": {
      "ai_outputParser": [
        [
          {
            "node": "Summarize & Categorize",
            "type": "ai_outputParser",
            "index": 0
          }
        ]
      ]
    },
    "Google Gemini 2.5 Flash Lite": {
      "ai_languageModel": [
        [
          {
            "node": "Summarize & Categorize",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "get_message (Audio/Video message)": {
      "main": [
        [
          {
            "node": "Normalize input",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Update Chat Memory (User and Agent)": {
      "main": [
        []
      ]
    },
    "When clicking ‘Execute workflow’": {
      "main": [
        [
          {
            "node": "Create Chat Memory Table",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}