{
  "id": "7EbxoldvZjmq2z4m",
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "name": "Improve AI email replies in Gmail from human feedback in Tracira",
  "tags": [],
  "nodes": [
    {
      "id": "60c9b13f-b42e-4a11-9c1a-57915b8e8c3d",
      "name": "Sticky Note 0",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        0,
        0
      ],
      "parameters": {
        "width": 564,
        "height": 820,
        "content": "## Self-improving email replies\n\nCorrect your AI once and it never makes that mistake again. **Nothing\nreaches a customer until you approve it** from any device.\n\nTwo branches, one automation, joined through Tracira (not by a wire).\nActivate the workflow once and both run.\n\n### How it works\n- **Draft branch:** every new Gmail message is flattened into clean\n  fields (sender, subject, body). Get instructions serves the playbook\n  stored in Tracira, the AI drafts with it, and Send to Tracira logs the\n  draft. Nothing is sent yet.\n- **Decision branch:** the Tracira Trigger fires the moment you decide.\n  Approve and Gmail sends the reply. Rewrite it yourself and Gmail sends\n  your version. Send it back with a note instead and the AI redrafts and\n  returns it for a second look. Either correction teaches the stored\n  instructions, so the next draft already follows it.\n\n### Setup\n1. Create a free account at [tracira.com](https://tracira.com/?utm_source=n8n&utm_medium=template&utm_campaign=self-improving-email-replies) if you don't have one yet.\n2. In n8n, search the nodes panel for **Tracira** and add it.\n3. Connect your **Gmail**, **OpenAI**, and **Tracira** credentials.\n4. Open **Get instructions** and **Send to Tracira** and set your workspace and task name.\n5. Activate the workflow, then approve or edit each draft in Tracira.\n\n### Customization tips\n- Edit the starter text in **Get instructions**. After the first run the\n  instructions live in Tracira and you change them by reviewing.\n- Narrow the **Filter** node to certain senders or labels."
      },
      "typeVersion": 1
    },
    {
      "id": "fdd2a8b8-a7bf-478f-a55d-454f9df3cb12",
      "name": "Sticky Note A1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        672,
        0
      ],
      "parameters": {
        "color": 7,
        "width": 652,
        "height": 484,
        "content": "### 1. Receive new email\n\n**Email** flattens the message into clean fields (sender, subject, body)."
      },
      "typeVersion": 1
    },
    {
      "id": "923c6af8-8f88-4dd0-a009-71466cd89ec9",
      "name": "Sticky Note A",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1328,
        0
      ],
      "parameters": {
        "color": 7,
        "width": 796,
        "height": 484,
        "content": "### 2. Draft with the stored instructions\n\n**Get instructions** serves the playbook hosted in Tracira (the first run seeds it).\nThe AI drafts with it, then **Send to Tracira** logs the draft and the version it followed. Nothing is sent yet."
      },
      "typeVersion": 1
    },
    {
      "id": "2b15acf3-be14-40de-9f30-d34ad146a94f",
      "name": "Sticky Note B",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        672,
        544
      ],
      "parameters": {
        "color": 7,
        "width": 1452,
        "height": 612,
        "content": "### 3. Act on the human decision\n\n**Approved** sends the AI's reply. **Rewritten by the reviewer** sends theirs, already fixed.\n**Sent back to the AI** redrafts it and resubmits it for another look.\nEither correction teaches the stored instructions, so there is no prompt editing here."
      },
      "typeVersion": 1
    },
    {
      "id": "6d09647b-8f40-4384-b722-50ee9041e257",
      "name": "Gmail",
      "type": "n8n-nodes-base.gmailTrigger",
      "position": [
        720,
        272
      ],
      "parameters": {
        "simple": false,
        "filters": {},
        "options": {},
        "pollTimes": {
          "item": [
            {
              "mode": "everyMinute"
            }
          ]
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "f32d558b-79a4-48dc-a14e-1e8678c4d0ca",
      "name": "Filter",
      "type": "n8n-nodes-base.filter",
      "position": [
        944,
        272
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "version": 3,
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "strict"
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "cda4fa40-70cb-4790-b9db-93d4bb2de7a0",
              "operator": {
                "type": "array",
                "operation": "notContains",
                "rightType": "any"
              },
              "leftValue": "={{ $json.labelIds }}",
              "rightValue": "SENT"
            }
          ]
        }
      },
      "typeVersion": 2.3
    },
    {
      "id": "41c9faf3-88b1-46a7-bb22-d9d01ce845b5",
      "name": "Email",
      "type": "n8n-nodes-base.set",
      "position": [
        1168,
        272
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "e1",
              "name": "sender",
              "type": "string",
              "value": "={{ $json.from?.value?.[0]?.address ?? $json.from?.text ?? $json.from }}"
            },
            {
              "id": "e2",
              "name": "subject",
              "type": "string",
              "value": "={{ $json.subject }}"
            },
            {
              "id": "e3",
              "name": "body",
              "type": "string",
              "value": "={{ $json.text }}"
            },
            {
              "id": "718b0121-3a71-4706-9af8-c027d468e884",
              "name": "bodyHtml",
              "type": "string",
              "value": "={{ $json.html }}"
            },
            {
              "id": "e4",
              "name": "threadId",
              "type": "string",
              "value": "={{ $json.threadId }}"
            },
            {
              "id": "e5",
              "name": "messageId",
              "type": "string",
              "value": "={{ $json.id }}"
            }
          ]
        }
      },
      "typeVersion": 3.4
    },
    {
      "id": "efb71f40-412e-413c-8269-a502a6211603",
      "name": "Get instructions",
      "type": "@deepidealab/n8n-nodes-tracira.tracira",
      "position": [
        1392,
        272
      ],
      "parameters": {
        "resource": "instructions",
        "instructionsTask": {
          "__rl": true,
          "mode": "name",
          "value": "Email reply and improve - n8n"
        },
        "instructionsProject": {
          "__rl": true,
          "mode": "name",
          "value": "Customer Support"
        },
        "starterInstructions": "You write friendly, concise replies to customer support emails. Answer the customer's question directly. Keep replies under 150 words. You can sign like \"Your AI, monitored with Tracira\". Reply with only the email body text, no subject line."
      },
      "typeVersion": 1
    },
    {
      "id": "d9821cc8-c8c9-4493-b4ae-a7beca483931",
      "name": "Draft with the instructions",
      "type": "@n8n/n8n-nodes-langchain.openAi",
      "position": [
        1584,
        272
      ],
      "parameters": {
        "modelId": {
          "__rl": true,
          "mode": "list",
          "value": "gpt-5.6-terra",
          "cachedResultName": "GPT-5.6-TERRA"
        },
        "options": {},
        "messages": {
          "values": [
            {
              "role": "system",
              "content": "={{ $json.content }}"
            },
            {
              "content": "=Customer email from {{ $('Email').first().json.sender }} - subject: {{ $('Email').first().json.subject }}\n\n{{ $('Email').first().json.body }}"
            }
          ]
        },
        "simplify": false
      },
      "typeVersion": 1.8
    },
    {
      "id": "8e6f957c-3838-492f-9408-1aed10db4562",
      "name": "Send to Tracira",
      "type": "@deepidealab/n8n-nodes-tracira.tracira",
      "position": [
        1904,
        272
      ],
      "parameters": {
        "mode": "async",
        "input": "={{ $('Email').first().json.bodyHtml }}",
        "output": "={{ $json.choices[0].message.content }}",
        "options": {
          "actorId": "={{ $('Email').first().json.sender }}",
          "metadata": {
            "entry": [
              {
                "key": "subject",
                "value": "={{ $('Email').first().json.subject }}"
              }
            ]
          },
          "sessionId": "={{ $('Email').first().json.threadId }}",
          "subjectId": "={{ $('Email').first().json.messageId }}",
          "instructionsVersion": "={{ $('Get instructions').first().json.version }}"
        },
        "taskName": {
          "__rl": true,
          "mode": "name",
          "value": "Email reply and improve - n8n"
        },
        "modelName": "={{ $json.model }}",
        "projectName": {
          "__rl": true,
          "mode": "name",
          "value": "Customer Support"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "48b8d145-417a-44ae-ad49-8dcee70b0bac",
      "name": "Tracira Trigger",
      "type": "@deepidealab/n8n-nodes-tracira.traciraTrigger",
      "position": [
        720,
        816
      ],
      "parameters": {
        "events": [
          "approved",
          "changed"
        ]
      },
      "typeVersion": 1
    },
    {
      "id": "6eaca3d2-6d8e-475a-b7f4-22d230a50ced",
      "name": "For this task?",
      "type": "n8n-nodes-base.filter",
      "position": [
        944,
        816
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "version": 3,
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "strict"
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "93e792c1-f3af-4fd4-b5bd-0d92e473ccff",
              "operator": {
                "type": "string",
                "operation": "equals"
              },
              "leftValue": "={{ $json.projectName }}",
              "rightValue": "Customer Support"
            },
            {
              "id": "fdeaf35d-5172-4f30-98f1-cd59ebf72378",
              "operator": {
                "type": "string",
                "operation": "equals"
              },
              "leftValue": "={{ $json.taskName }}",
              "rightValue": "Email reply and improve - n8n"
            }
          ]
        }
      },
      "typeVersion": 2.3
    },
    {
      "id": "f46705a5-7e25-435e-b3ff-6f656aa96c92",
      "name": "What did the reviewer do?",
      "type": "n8n-nodes-base.switch",
      "position": [
        1168,
        816
      ],
      "parameters": {
        "rules": {
          "values": [
            {
              "outputKey": "Approved: send it",
              "conditions": {
                "options": {
                  "version": 2,
                  "leftValue": "",
                  "caseSensitive": true,
                  "typeValidation": "strict"
                },
                "combinator": "and",
                "conditions": [
                  {
                    "id": "s0-0",
                    "operator": {
                      "type": "string",
                      "operation": "equals"
                    },
                    "leftValue": "={{ $json.decision }}",
                    "rightValue": "approved"
                  }
                ]
              },
              "renameOutput": true
            },
            {
              "outputKey": "Rewritten by the reviewer: send theirs",
              "conditions": {
                "options": {
                  "version": 2,
                  "leftValue": "",
                  "caseSensitive": true,
                  "typeValidation": "strict"
                },
                "combinator": "and",
                "conditions": [
                  {
                    "id": "s1-0",
                    "operator": {
                      "type": "string",
                      "operation": "notEmpty",
                      "singleValue": true
                    },
                    "leftValue": "={{ $json.correctedOutput || '' }}",
                    "rightValue": ""
                  }
                ]
              },
              "renameOutput": true
            },
            {
              "outputKey": "Sent back to the AI: revise it",
              "conditions": {
                "options": {
                  "version": 2,
                  "leftValue": "",
                  "caseSensitive": true,
                  "typeValidation": "strict"
                },
                "combinator": "and",
                "conditions": [
                  {
                    "id": "s2-0",
                    "operator": {
                      "type": "string",
                      "operation": "notEmpty",
                      "singleValue": true
                    },
                    "leftValue": "={{ $json.comment || '' }}",
                    "rightValue": ""
                  }
                ]
              },
              "renameOutput": true
            }
          ]
        },
        "options": {}
      },
      "typeVersion": 3.2
    },
    {
      "id": "e96c3910-b4e9-4634-a773-9b199fcd8a6d",
      "name": "Send the reply",
      "type": "n8n-nodes-base.gmail",
      "position": [
        1392,
        720
      ],
      "parameters": {
        "message": "={{ $json.output.replaceAll('\\n', '<br/>') }}",
        "options": {
          "appendAttribution": false
        },
        "messageId": "={{ $json.subjectId }}",
        "operation": "reply"
      },
      "typeVersion": 2.1
    },
    {
      "id": "5f13ee29-5d1d-4eb8-a536-6f4316b6d4c3",
      "name": "Get latest instructions",
      "type": "@deepidealab/n8n-nodes-tracira.tracira",
      "position": [
        1392,
        912
      ],
      "parameters": {
        "resource": "instructions",
        "instructionsTask": {
          "__rl": true,
          "mode": "name",
          "value": "={{ $json.taskName }}"
        },
        "instructionsProject": {
          "__rl": true,
          "mode": "name",
          "value": "={{ $json.projectName }}"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "fc105f79-6979-4424-b51f-e0f8cb8d7103",
      "name": "Revise the draft",
      "type": "@n8n/n8n-nodes-langchain.openAi",
      "position": [
        1584,
        912
      ],
      "parameters": {
        "modelId": {
          "__rl": true,
          "mode": "list",
          "value": "gpt-5.6-terra",
          "cachedResultName": "GPT-5.6-TERRA"
        },
        "options": {},
        "messages": {
          "values": [
            {
              "role": "system",
              "content": "={{ $('Get latest instructions').first().json.content }}\n\n--\n\nYou revise draft replies for a customer-support team. Rewrite the draft below so it follows the manager's instruction. Keep everything the manager did not ask to change. Reply with the revised email text only."
            },
            {
              "content": "=Input:\n{{ $('What did the reviewer do?').item.json.input }}\n\nDraft reply:\n{{ $('What did the reviewer do?').item.json.output }}\n\nManager's instruction:\n{{ $('What did the reviewer do?').item.json.comment }}"
            }
          ]
        },
        "simplify": false
      },
      "typeVersion": 1.8
    },
    {
      "id": "6113e90a-9b52-4efb-ae93-9ccd2562f5c4",
      "name": "Re-check the revised draft",
      "type": "@deepidealab/n8n-nodes-tracira.tracira",
      "position": [
        1904,
        912
      ],
      "parameters": {
        "mode": "async",
        "input": "={{ $('Tracira Trigger').item.json.input }}",
        "output": "={{ $json.choices[0].message.content }}",
        "options": {
          "actorId": "={{ $('Tracira Trigger').item.json.actorId }}",
          "metadata": {
            "entry": [
              {
                "key": "subject",
                "value": "={{ $('Tracira Trigger').item.json.metadata.subject }}"
              }
            ]
          },
          "sessionId": "={{ $('Tracira Trigger').item.json.sessionId }}",
          "subjectId": "={{ $('Tracira Trigger').item.json.subjectId }}",
          "revisionOf": "={{ $('Tracira Trigger').item.json.logId }}",
          "instructionsVersion": "={{ $('Get latest instructions').first().json.version }}"
        },
        "taskName": {
          "__rl": true,
          "mode": "name",
          "value": "={{ $('Tracira Trigger').item.json.taskName }}"
        },
        "modelName": "={{ $json.model }}",
        "projectName": {
          "__rl": true,
          "mode": "name",
          "value": "={{ $('Tracira Trigger').item.json.projectName }}"
        }
      },
      "typeVersion": 1
    }
  ],
  "active": false,
  "settings": {
    "binaryMode": "separate",
    "availableInMCP": false,
    "executionOrder": "v1"
  },
  "versionId": "a7573238-91c3-45f0-b605-49d63e37b555",
  "nodeGroups": [],
  "connections": {
    "Email": {
      "main": [
        [
          {
            "node": "Get instructions",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Gmail": {
      "main": [
        [
          {
            "node": "Filter",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Filter": {
      "main": [
        [
          {
            "node": "Email",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "For this task?": {
      "main": [
        [
          {
            "node": "What did the reviewer do?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Tracira Trigger": {
      "main": [
        [
          {
            "node": "For this task?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get instructions": {
      "main": [
        [
          {
            "node": "Draft with the instructions",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Revise the draft": {
      "main": [
        [
          {
            "node": "Re-check the revised draft",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get latest instructions": {
      "main": [
        [
          {
            "node": "Revise the draft",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "What did the reviewer do?": {
      "main": [
        [
          {
            "node": "Send the reply",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Send the reply",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Get latest instructions",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Draft with the instructions": {
      "main": [
        [
          {
            "node": "Send to Tracira",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}