Learn n8n AI Agent node how to use?
This tutorial shows how to use the n8n AI Agent node for basic automation. The n8n AI Agent node how to use steps below include a direct path: install n8n, configure OpenAI credentials, create a workflow, and run the AI Agent node to process prompts and actions. The example uses Docker, Node.js tools, and Postgres for persistence.
What You Need
- Docker and docker-compose installed on your machine.
- Node.js and npm if you prefer the local n8n CLI.
- A Postgres database for n8n data.
- An OpenAI API key or compatible AI provider key.
n8n AI Agent node how to use — Step-by-step setup
Follow the numbered steps below. Each step is short and actionable. Commands are provided for Docker and the n8n CLI.
Step 1: Start Postgres and n8n with Docker
Create a docker-compose.yml file like this. Save it as docker-compose.yml then run the commands shown.
version: '3'
services:
postgres:
image: postgres:13
environment:
POSTGRES_USER: n8n
POSTGRES_PASSWORD: n8n
POSTGRES_DB: n8n
volumes:
- ./pgdata:/var/lib/postgresql/data
n8n:
image: n8nio/n8n:latest
ports:
- "5678:5678"
environment:
DB_TYPE: postgres
DB_POSTGRESDB_HOST: postgres
DB_POSTGRESDB_PORT: 5432
DB_POSTGRESDB_DATABASE: n8n
DB_POSTGRESDB_USER: n8n
DB_POSTGRESDB_PASSWORD: n8n
N8N_BASIC_AUTH_ACTIVE: "true"
N8N_BASIC_AUTH_USER: "admin"
N8N_BASIC_AUTH_PASSWORD: "changeme"
OPENAI_API_KEY: "${OPENAI_API_KEY}"
depends_on:
- postgres
# start services
docker-compose up -d
Step 2: Optionally run n8n locally with Node.js
Use the n8n CLI for development or quick testing.
# install n8n globally (optional)
npm install -g n8n
# start n8n locally
n8n start
Step 3: Create OpenAI credentials inside n8n
- Open the n8n web UI at http://localhost:5678.
- Go to Credentials and add an OpenAI credential.
- Paste your OPENAI_API_KEY and save.
Step 4: Build a workflow with the AI Agent node
- 1. Create a new workflow in n8n.
- 2. Add a Trigger node (e.g., Webhook or Cron) to start the flow.
- 3. Add the AI Agent node and select the OpenAI credential.
- 4. Configure the prompt and model (for example choose gpt-4 or text-davinci-003).
- 5. Connect nodes so the AI Agent output moves to an HTTP Request, Set node, or Postgres insert.
# import a saved workflow (example)
n8n import:workflow --input=workflow.json
# export after changes
n8n export:workflow --id=1 --output=workflow.json
Update
Keep n8n and images up to date. Use docker-compose to pull new images and restart. This preserves your Postgres data directory.
# update containers
docker-compose pull
docker-compose up -d
Security
- Store OPENAI_API_KEY in an environment file or secret manager, not in source control.
- Enable basic auth (shown) and use HTTPS in production with a reverse proxy.
- Limit Postgres access with firewall rules and strong passwords.
- Grant the AI API key only the required scopes and rotate it regularly.
Done
You now have a working n8n setup with the AI Agent node. The workflow can accept triggers, call the AI Agent node, and process results into Postgres or webhooks.
Next steps: refine prompts, add error handling, and log AI outputs for traceability.