DEV Community

Canan Korkut
Canan Korkut

Posted on • Originally published at Medium

Build Your First AI Agent with n8n

In this article, I will walk you step by step through how to build your own AI agent using the open-source automation platform n8n. This agent will track overdue invoices and automatically send reminder emails to customers. The final system will look like this:

If you want to use n8n for free, you can install and run it locally. For this, you need to install Docker on your computer.

If you want to use n8n for free, you can install and run it locally. For this, you need to install Docker on your computer.

  • First, go to the Docker Desktop website and download the version suitable for your operating system, then install it. Docker allows n8n to run on your computer in an isolated environment.

  • To make sure your workflows and connections in n8n are not lost when you shut down your computer, we need to create a “storage space.” Open your terminal (CMD or PowerShell) and run the following command:

docker volume create n8n_data
Enter fullscreen mode Exit fullscreen mode
  • Now, let’s start n8n. Paste the following command into your terminal:
docker run -it --rm --name n8n -p 5678:5678 -v n8n_data:/home/node/.n8n n8nio/n8n
Enter fullscreen mode Exit fullscreen mode
  • Once the installation is complete, open your browser and go to:

http://localhost:5678

In this project, our scenario is as follows: we have a table that stores customers’ payment statuses. Normally, checking this table every day and manually sending reminder emails to overdue customers is time-consuming.

The AI Agent we build will handle this process automatically in three steps:

  • Data Retrieval: It checks the table and identifies which customers have outstanding debts and how many days they are overdue.

  • Analysis: Based on the amount and delay duration, it decides the tone of the email (a gentle reminder or a more urgent warning).

  • Action: It sends personalized emails automatically to customers.

We will build this system in n8n using the following components:

  1. Trigger (When Chat Message Received): Listens for commands in the chat and starts the workflow.

  2. AI Agent: The main node that manages the process.

  3. Gemini Chat Model: The “brain” that enables decision-making.

  4. Memory: Short-term memory so the agent doesn’t forget what it’s doing.

  5. Tools: Used to read the table and send emails.

After completing the setup, let’s create a project in n8n.

  • Click the + icon on the right side and select “On Chat Message”. This adds the required trigger.

  • Then click the + button again and type “AI Agent” in the search bar. Add it and double-click on it to configure the settings.

# AI Agent Prompt

**Role:**
You are an intelligent collections assistant responsible for managing overdue invoice reminders using our internal data table.

**Task:**
Identify customers who have an overdue invoice AND whose 'Status' is 'Pending'. If a customer's 'Status' is 'Reminded', skip them.

**Overdue Rules:**
- Be polite if the invoice is less than 7 days overdue.
- Adopt a progressively firmer and more professional tone as the overdue period increases (e.g., more direct language after 14+ days). 
- Today's date is {{ $now.format('yyyy-MM-dd') }}. Use this to calculate how many days each invoice is overdue based on the 'DueDate' field.

**Tools:**
- `getInvoices`: Use this tool to fetch all rows from the internal data table. You must filter these results yourself to find rows where:
    1. 'DueDate' is before today.
    2. 'Balance' is greater than 0.
    3. 'Status' is exactly 'Pending'.
- `sendPaymentReminder`: Use this tool to send the finalized email to the customer. Use the 'Email' field from the table as the recipient.

**Outputs:**
Return a JSON summary containing:
- `emailsSent`: Total number of emails sent.
- `details`: An array where each element includes:
    - `customerName`
    - `amountOverdue`
    - `daysOverdue`
    - `emailSubject`
    - `emailBody`
Enter fullscreen mode Exit fullscreen mode

After this, you can close the window. The AI Agent has three outputs: Chat Model, Memory, and Tool.

  • Click on the Chat Model input and select Gemini. Then, double-click on it to configure the settings. Create a new credential, obtain your API key from Google AI Studio, and paste it into the required field. Finally, choose the gemini-2.0-flash-lite model, as it provides a higher usage limit.

  • Click on the Memory input and select Simple Memory. Then, double-click on it to configure the settings. Set the Session ID to “Define Below,” enter session as the key, and adjust the Context Window Length to 5.

  • To send emails, click the + icon under the Tools section, search for Send Email Tool, and select it. Then, create a new credential by entering your Gmail address as the user and smtp.gmail.com as the host. For the password, go to security.google.com, search for “Sign in with App Password,” generate a password, and paste it into the required field. After saving, double-click the email node again, set the From Email as your own email address, and click the star icon next to To Email, Subject, and Text to auto-fill them. Finally, set the email format to text and rename the tool to sendPaymentReminder.

  • We will manually create a data table and use it in our workflow. To do this, click on Overview on the right side, select Data Tables, and create your table. Fill it out as shown below.

  • Then, go back to your workflow, click the + icon under the Tools section, and search for Data Table. Set the Operation to Get, select your table, and finally rename this node to getInvoices.

  • All setup steps are complete. Now you can trigger the workflow by entering a prompt in the chat.

Example:

Run the payment reminder workflow for all customers.
Enter fullscreen mode Exit fullscreen mode

In this project, we built an AI Agent on n8n that can make its own decisions. After setting up a free working environment with Docker, we configured Gemini as the agent’s brain and Memory as its short-term memory.

As a result, we created a system that, with a single command, can access the data table, analyze outstanding debts, determine the appropriate email tone, and automatically send personalized reminders.

Top comments (0)