{
  "id": "9pmjBc7dqrEagOoX",
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "name": "n8n Antigravity AI Automation: Create Autonomous Task-Handling AI Agent",
  "tags": [],
  "nodes": [
    {
      "id": "4c4ac089-ee21-4363-9bdc-fbf8bedfc52a",
      "name": "Readme",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -848,
        912
      ],
      "parameters": {
        "width": 900,
        "height": 664,
        "content": "## n8n Antigravity AI Automation: Create Autonomous Task-Handling AI Agent\n\nThis workflow creates a self-improving AI agent inside n8n that can understand natural language tasks, plan steps, use tools (HTTP, code, search, \u2026), reflect on results, and continue until the goal is reached \u2014 then deliver the final answer.\n\n### How it works\n\n1. Webhook or manual trigger receives a task description  \n2. LLM creates initial plan + first tool call (or finishes immediately)  \n3. Loop:  \n   \u2022 Execute chosen tool  \n   \u2022 Send observation back to LLM  \n   \u2022 LLM reflects \u2192 decides next action or finish  \n4. When finished \u2192 format final answer, save result, send Slack notification\n\n### Setup steps\n\n1. Connect **OpenAI** (or Anthropic/Groq/Gemini) credential  \n2. (Optional) Connect **Slack** credential for notifications  \n3. Replace the placeholder \u201cOther Tools\u201d Code node with real tool nodes (Switch + HTTP Request, Google Sheets, Code node, etc.)  \n4. Test with simple tasks first:  \n   \u2022 \u201cWhat is the current weather in Ahmedabad?\u201d  \n   \u2022 \u201cCalculate 17\u00d742 and explain the steps\u201d  \n5. Adjust max iterations (via SplitInBatches or custom counter) to prevent infinite loops  \n6. Activate the workflow and send POST request to webhook with JSON:  \n   ```json\n   {\"task\": \"your task here\"}"
      },
      "typeVersion": 1
    },
    {
      "id": "a4ba1f88-6239-4a56-bf2e-dc127652f8e3",
      "name": "Input",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        304,
        1056
      ],
      "parameters": {
        "color": 4,
        "width": 428,
        "height": 444,
        "content": "## 1. Input"
      },
      "typeVersion": 1
    },
    {
      "id": "31dba07e-8849-45ab-9d9c-aff436a7d73a",
      "name": "Planning",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        784,
        1040
      ],
      "parameters": {
        "color": 5,
        "width": 628,
        "height": 480,
        "content": "## 2. Planning"
      },
      "typeVersion": 1
    },
    {
      "id": "781af996-5a7e-446e-af73-90f2d32bbc47",
      "name": "Loop",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1456,
        768
      ],
      "parameters": {
        "color": 4,
        "width": 1064,
        "height": 780,
        "content": "## 3. Agent Loop"
      },
      "typeVersion": 1
    },
    {
      "id": "ab00a7e5-4f8b-4426-9b01-c1f9068db931",
      "name": "Output",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        2592,
        912
      ],
      "parameters": {
        "color": 5,
        "width": 508,
        "height": 620,
        "content": "## 4. Output"
      },
      "typeVersion": 1
    },
    {
      "id": "8bd1ac0e-4715-422e-a01e-42e918bc563b",
      "name": "Webhook: Start Agent",
      "type": "n8n-nodes-base.webhook",
      "position": [
        384,
        1232
      ],
      "parameters": {
        "path": "agent-run",
        "options": {},
        "httpMethod": "POST"
      },
      "typeVersion": 2
    },
    {
      "id": "822a91cd-8035-497f-a14c-9782d9540390",
      "name": "Initialize Task & Memory",
      "type": "n8n-nodes-base.set",
      "position": [
        608,
        1232
      ],
      "parameters": {
        "values": {
          "number": [
            {
              "name": "iteration"
            }
          ],
          "string": [
            {
              "name": "task",
              "value": "={{ $json.body.task || $json.query.task || 'Find current top AI agent frameworks in 2026' }}"
            },
            {
              "name": "context",
              "value": "Starting fresh - no prior steps"
            }
          ]
        },
        "options": {}
      },
      "typeVersion": 2
    },
    {
      "id": "22f2ec26-5296-4908-8ba6-64e62da27ee8",
      "name": "LLM: Initial Plan",
      "type": "n8n-nodes-base.openAi",
      "position": [
        832,
        1232
      ],
      "parameters": {
        "model": "gpt-4o-mini",
        "prompt": [
          {
            "role": "system",
            "message": "You are an autonomous agent. Use tools when needed. Respond ONLY with valid JSON:\n{\n  \"thought\": \"reasoning here\",\n  \"action\": \"http_request | web_search | code_execution | finish\",\n  \"action_input\": { ... },\n  \"final_answer\": \"...\"   // only when action = finish\n}\nDo NOT write anything outside the JSON."
          },
          {
            "role": "user",
            "message": "Task: {{ $json.task }}\n\nCurrent context:\n{{ $json.context }}\n\nWhat do you do next?"
          }
        ],
        "options": {},
        "requestOptions": {}
      },
      "credentials": {
        "openAiApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1.1
    },
    {
      "id": "836a5880-be68-462c-b9be-17bec27ab198",
      "name": "Already Finished?",
      "type": "n8n-nodes-base.if",
      "position": [
        1056,
        1232
      ],
      "parameters": {
        "conditions": {
          "boolean": [
            {
              "value1": "={{ JSON.parse($json.choices[0].message.content).action === 'finish' }}"
            }
          ]
        }
      },
      "typeVersion": 1
    },
    {
      "id": "3f6072e0-a74f-4321-bb05-c99b2a1d1c09",
      "name": "Loop Control",
      "type": "n8n-nodes-base.splitInBatches",
      "position": [
        1488,
        1376
      ],
      "parameters": {
        "options": {},
        "batchSize": 1
      },
      "typeVersion": 1
    },
    {
      "id": "67f9c4bf-f171-40d6-8bce-db2921f711f2",
      "name": "Parse LLM Output",
      "type": "n8n-nodes-base.code",
      "position": [
        1712,
        1200
      ],
      "parameters": {
        "jsCode": "const responseText = items[0].json.choices?.[0]?.message?.content || '';\nlet parsed;\ntry {\n  parsed = JSON.parse(responseText);\n} catch (e) {\n  parsed = { thought: 'Parse failed', action: 'finish', final_answer: 'LLM returned invalid format' };\n}\n\nreturn [{\n  json: {\n    thought: parsed.thought,\n    action: parsed.action,\n    action_input: parsed.action_input || {},\n    final_answer: parsed.final_answer,\n    iteration: ($input.first().json.iteration || 0) + 1\n  }\n}];"
      },
      "typeVersion": 2
    },
    {
      "id": "39a761b3-90c0-4ded-8806-491f3e780162",
      "name": "Loop \u2192 Finish?",
      "type": "n8n-nodes-base.if",
      "position": [
        1936,
        1200
      ],
      "parameters": {
        "conditions": {
          "string": [
            {
              "value1": "={{ $json.action }}",
              "value2": "finish",
              "operation": "equals"
            }
          ]
        }
      },
      "typeVersion": 1
    },
    {
      "id": "025a750b-6d34-4f7c-924e-d9390fbda05d",
      "name": "Tool \u2192 HTTP Request",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        2160,
        1152
      ],
      "parameters": {
        "url": "={{ $json.action_input.url }}",
        "options": {}
      },
      "typeVersion": 4.2
    },
    {
      "id": "3aa5fb16-8e16-4ebf-947a-48866bcd7f3f",
      "name": "Other Tools (expand later)",
      "type": "n8n-nodes-base.code",
      "position": [
        2160,
        1344
      ],
      "parameters": {
        "jsCode": "// Placeholder for other tools (expand with Switch later)\nconst action = $json.action;\nlet observation = 'No tool matched';\n\nif (action === 'web_search') {\n  observation = `Would search: ${$json.action_input.query}`;\n} else if (action === 'code_execution') {\n  observation = `Would run code: ${($json.action_input.code || '').substring(0,100)}...`;\n}\n\nreturn { json: { observation } };"
      },
      "typeVersion": 2
    },
    {
      "id": "c8b5784c-847c-4642-b195-6affde217d21",
      "name": "Merge Observations",
      "type": "n8n-nodes-base.merge",
      "position": [
        2384,
        1248
      ],
      "parameters": {
        "mode": "combine",
        "options": {},
        "mergeByFields": {
          "values": [
            {}
          ]
        }
      },
      "typeVersion": 2.1
    },
    {
      "id": "bf6bd74b-4cd1-4f16-a633-360e0a466035",
      "name": "LLM: Reflect \u2192 Next",
      "type": "n8n-nodes-base.openAi",
      "position": [
        2608,
        1248
      ],
      "parameters": {
        "model": "gpt-4o-mini",
        "prompt": [
          {
            "role": "system",
            "message": "Continue the task. Previous step + observation below.\nRespond ONLY with the same JSON format. Decide next action or finish."
          },
          {
            "role": "user",
            "message": "Task: {{ $('Initialize Task & Memory').first().json.task }}\nIteration: {{ $json.iteration }}\nThought: {{ $json.thought }}\nAction: {{ $json.action }}\nObservation: {{ $json.observation }}\n\nNext?"
          }
        ],
        "options": {},
        "requestOptions": {}
      },
      "credentials": {
        "openAiApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1.1
    },
    {
      "id": "2d1d2f25-7616-4b95-a3dc-5ffd3253468e",
      "name": "Update Memory",
      "type": "n8n-nodes-base.set",
      "position": [
        2832,
        1328
      ],
      "parameters": {
        "values": {
          "number": [
            {
              "name": "iteration",
              "value": "={{ $json.iteration }}"
            }
          ],
          "string": [
            {
              "name": "context",
              "value": "={{ $json.context + '\\n\\nIteration ' + $json.iteration + ' | Thought: ' + $json.thought + ' | Action: ' + $json.action + ' | Obs: ' + ($json.observation || 'none') }}"
            }
          ]
        },
        "options": {}
      },
      "typeVersion": 2
    },
    {
      "id": "3664d895-4a0c-4c18-846d-a4e8af2fb60a",
      "name": "Prepare Final Result",
      "type": "n8n-nodes-base.set",
      "position": [
        1792,
        944
      ],
      "parameters": {
        "values": {
          "string": [
            {
              "name": "final_answer",
              "value": "={{ $json.final_answer || $json.observation || 'Task completed (no explicit final answer provided)' }}"
            }
          ]
        },
        "options": {}
      },
      "typeVersion": 2
    },
    {
      "id": "7e2064fd-f412-4790-8ced-ce84e650ef24",
      "name": "Notify Slack",
      "type": "n8n-nodes-base.slack",
      "position": [
        2336,
        960
      ],
      "parameters": {
        "text": "={{ `Agent finished\\nTask: ${$json.task}\\nResult:\\n${$json.final_answer.substring(0,1500)}...` }}",
        "channel": "agent-results",
        "attachments": [],
        "otherOptions": {}
      },
      "credentials": {
        "slackApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "36791bda-27b1-4ed9-9852-662cbfbeb438",
      "name": "Wait For Result",
      "type": "n8n-nodes-base.wait",
      "position": [
        2064,
        944
      ],
      "parameters": {},
      "typeVersion": 1.1
    }
  ],
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "9f1b3110-f298-4597-8be9-c1df56eededf",
  "connections": {
    "Loop Control": {
      "main": [
        [
          {
            "node": "Parse LLM Output",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Update Memory": {
      "main": [
        [
          {
            "node": "Loop Control",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Wait For Result": {
      "main": [
        [
          {
            "node": "Notify Slack",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Loop \u2192 Finish?": {
      "main": [
        [
          {
            "node": "Prepare Final Result",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Tool \u2192 HTTP Request",
            "type": "main",
            "index": 0
          },
          {
            "node": "Other Tools (expand later)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Parse LLM Output": {
      "main": [
        [
          {
            "node": "Loop \u2192 Finish?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Already Finished?": {
      "main": [
        [
          {
            "node": "Prepare Final Result",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Loop Control",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "LLM: Initial Plan": {
      "main": [
        [
          {
            "node": "Already Finished?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Merge Observations": {
      "main": [
        [
          {
            "node": "LLM: Reflect \u2192 Next",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Prepare Final Result": {
      "main": [
        [
          {
            "node": "Wait For Result",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Webhook: Start Agent": {
      "main": [
        [
          {
            "node": "Initialize Task & Memory",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "LLM: Reflect \u2192 Next": {
      "main": [
        [
          {
            "node": "Update Memory",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Tool \u2192 HTTP Request": {
      "main": [
        [
          {
            "node": "Merge Observations",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Initialize Task & Memory": {
      "main": [
        [
          {
            "node": "LLM: Initial Plan",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Other Tools (expand later)": {
      "main": [
        [
          {
            "node": "Merge Observations",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}