AI-Powered Auto-Healing for Failed n8n Workflows

High-Level Summary

This workflow automates the entire overdue invoice recovery process using an AI agent. It runs daily on a schedule, reads unpaid invoices from a Google Sheet, and for each invoice uses OpenAI's GPT-4o-mini to analyze the situation and generate a personalized, context-aware recovery email. The email is sent via Gmail, and if the AI determines the account requires escalation (legal or collections), an internal alert is emailed to your legal team. The workflow then updates the invoice sheet with the new escalation level and appends a detailed log row to a separate recovery sheet for full auditability. Slack alerts notify the team if no invoices are found or if the AI encounters an error.

Nodes and Their Functions

Below is every node in the workflow, listed in execution order, with its type, authentication method, and key configuration.

  • Daily Invoice Check (Schedule Trigger, No auth) — Fires the workflow every 24 hours. Configured with an interval rule of 24 hours.
  • Read Overdue Invoices (Google Sheets, OAuth2) — Reads all rows from the "invoices" tab of your Google Sheet. Requires a Google Sheets OAuth2 credential and the spreadsheet ID.
  • Any Overdue Invoices? (If, No auth) — Checks if the Invoice field is not empty. If invoices exist, the workflow proceeds to the AI agent; otherwise, it triggers a Slack alert.
  • Slack: No Invoices Found Alert (Slack, OAuth2) — Sends a message to a specified Slack channel when no overdue invoices are found. Uses Slack OAuth2 credential.
  • AI Recovery Agent (LangChain Agent, uses OpenAI API key via sub-node) — The core AI node. It receives each invoice row and a system prompt instructing it to act as an AR recovery specialist. It outputs a JSON object with escalation level, subject, email body, payment plans, and escalation action. The agent is connected to the GPT-4o-mini Language Model node.
  • GPT-4o-mini Language Model (OpenAI Chat Model, API Key) — Provides the language model for the AI agent. Uses the gpt-4o-mini model. Requires an OpenAI API key credential.
  • Parse & Structure AI Response (Code, No auth) — A JavaScript code node that cleans and parses the AI's raw output into a structured JSON object. It also merges the original invoice data (invoice number, client name, etc.) into the output.
  • Send Recovery Email to Client (Gmail, OAuth2) — Sends the personalized recovery email to the client. Uses the subject and email_body from the AI response. The recipient email is taken from the original invoice row. Requires a Gmail OAuth2 credential.
  • Needs Escalation? (If, No auth) — Checks if the escalation_action field from the AI response is not "none". If escalation is needed, the workflow sends an internal alert; otherwise, it skips directly to logging.
  • Send Escalation Alert to Legal Team (Gmail, OAuth2) — Sends an HTML email to a predefined legal team address with details about the high-risk invoice. The email includes client name, invoice number, amount due, days overdue, escalation level, and AI summary. Requires a Gmail OAuth2 credential.
  • Update Invoice Status in Sheet (Google Sheets, OAuth2) — Updates the original invoice row in the "invoices" tab with the new escalation level and AI summary. Uses the row number to match the correct row.
  • Append Row to Recovery Log (Google Sheets, OAuth2) — Appends a new row to the "Recovery sheet" tab with a full log of the action taken, including invoice details, AI subject, summary, escalation level, and Gmail label IDs.
  • Slack: AI Error Alert (Slack, OAuth2) — Triggered if the AI Recovery Agent node encounters an error. Sends a Slack message with the invoice number and a request to check execution logs.

Additionally, there are several Sticky Notes (Overview, Section: Trigger, Section: AI Agent, Section: Email, Section: Logging, Section: Security) that serve as documentation and visual guides within the editor.

Setup Instructions

To use this workflow, you need the following accounts and credentials:

  1. Google Sheets — A spreadsheet with two tabs:

    • invoices: Columns should include Invoice, Client, Email, Amount, Due Date, Days Overdue, Level, Reminders Sent, and optionally row_number.
    • Recovery sheet: Columns for logging (Invoice, Client, Amount, Days Overdue, Level, Email, escalation_level, summary, subject, labelIds). Create a Google Sheets OAuth2 credential in n8n and connect it to both Google Sheets nodes.
  2. Gmail — A Gmail account for sending recovery emails and escalation alerts. Create a Gmail OAuth2 credential in n8n. Update the escalation email recipient address (user@example.com) to your legal or collections team's email.

  3. OpenAI — An OpenAI API key with access to the gpt-4o-mini model. Create an OpenAI API credential in n8n.

  4. Slack — A Slack workspace and a channel for alerts. Create a Slack OAuth2 credential in n8n. Replace YOUR_SLACK_CHANNEL_ID with the actual channel ID.

After connecting credentials, update the Google Sheet ID in the "Read Overdue Invoices", "Update Invoice Status in Sheet", and "Append Row to Recovery Log" nodes. Also update the Slack channel IDs and the legal team email address. Then activate the workflow.

Use Cases and Variations

  • Custom Escalation Rules — Modify the AI prompt or the "Needs Escalation?" condition to use different criteria (e.g., amount threshold, client tier).
  • Multi-language Support — Adjust the AI prompt to generate emails in different languages.
  • Integration with Accounting Software — Replace the Google Sheets nodes with API calls to QuickBooks, Xero, or other invoicing platforms.
  • Different AI Models — Swap the GPT-4o-mini model for another OpenAI model or use a different provider via the LangChain integration.
  • SMS or Other Channels — Add Twilio or other nodes to send reminders via SMS in addition to email.
  • Dashboard & Reporting — Extend the logging sheet to feed into a BI tool for real-time recovery metrics.
19 nodesschedule triggerFinance
Sticky NoteAgentGmailGoogle SheetsIFLm Chat Open AICodeSlack

Workflow JSON

{
  "id": "XOF9UqmsKeApytec",
  "meta": {
    "instanceId": "8443f10082278c46aa5cf3acf8ff0f70061a2c58bce76efac814b16290845177",
    "templateCredsSetupCompleted": true
  },
  "name": "Automated Overdue Invoice Recovery & Escalation Workflow",
  "tags": [],
  "nodes": [
    {
      "id": "b857c280-3f76-48ef-98cd-939e8359e539",
      "name": "Overview",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -2160,
        928
      ],
      "parameters": {
        "width": 600,
        "height": 476,
        "content": "## 💸 Accounts Receivable Recovery Agent\n\n### How it works\nThis workflow runs daily and automatically handles overdue invoice follow-ups — from sending personalized reminder emails to escalating high-risk accounts. An AI agent reads each unpaid invoice, determines the urgency level (1–4), drafts a context-aware email, and decides whether to loop in your legal or collections team.\n\nOnce the email is sent, it updates the invoice record and appends a full log row to a recovery sheet — so your team always knows what was sent, when, and why.\n\n### Setup steps\n1. Connect your **Google Sheets OAuth2** credential and point both sheet nodes to your spreadsheet (Invoices tab + Recovery Sheet tab)\n2. Connect your **Gmail OAuth2** credential for both outbound email nodes\n3. Connect your **OpenAI API** credential to the GPT-4o-mini model node\n4. Connect your **Slack OAuth2** credential for error and no-invoice alerts\n5. Update `legal@yourcompany.com` in the escalation email node to your real address\n6. Run once manually to verify the sheet columns match: Invoice, Client, Email, Amount, Days Overdue, Level, Reminders Sent"
      },
      "typeVersion": 1
    },
    {
      "id": "1a439390-d077-4825-b424-e2b99ee73771",
      "name": "Section: Trigger",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1456,
        1488
      ],
      "parameters": {
        "color": 7,
        "width": 900,
        "height": 732,
        "content": "## ⏱️ Trigger & Invoice Fetch\n\nFires every 24 hours, reads all rows from the Invoices sheet, and checks whether any valid invoice entries exist. If nothing is found, a Slack alert notifies the team immediately so no silent failures go unnoticed."
      },
      "typeVersion": 1
    },
    {
      "id": "7bd1281a-7405-4d8f-ae1e-2a4b46535999",
      "name": "Section: AI Agent",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -496,
        1408
      ],
      "parameters": {
        "color": 7,
// ... truncated (copy to see full JSON)

How to Import This Workflow

  1. 1Copy the workflow JSON above using the Copy Workflow JSON button.
  2. 2Open your n8n instance and go to Workflows.
  3. 3Click Import from JSON and paste the copied workflow.

Don't have an n8n instance? Start your free trial at n8nautomation.cloud

Ready to automate with n8n?

Get affordable managed n8n hosting with 24/7 support.