DEV Community

Cover image for Simple AI Agent with n8n and OpenRouter

Simple AI Agent with n8n and OpenRouter

I just felt like spending some quality time with n8n, open router and learn how to build impactful AI agents using n8n. So here we go 🤩!

BTW This is gonna be a series, so buckle up and sit tight 😁 and in this post I am just gonna dive in and give you a feeling of what is possible with n8n.

Step 1: Setting Up OpenRouter

First of all OpenRouter provides access to many different AI models, including free ones over a unified API. So I guess I do NOT need to convince you to use it 😅.

Create an OpenRouter Account

  1. Go to OpenRouter.
  2. Sign Up.
  3. Create an API key:
    • Name your key (e.g., "n8n-tut").
    • Leave the credit limit, and expiration blank (we'll use free models).
    • Copy the key to your clipboard and keep in somewhere since we will need it in a second.
    • The key should start with sk-....

Step 2: Setting Up n8n

n8n is the workflow automation tool we'll be using to build AI agents visually.

Create an n8n Account

  1. Go to n8n.io.
  2. Start your 14-day free trial by opening a new account.

Step 3: Build Your First Workflow

We'll build a simple dummy workflow which will look like chat.deepseek.com. So it is gonna be an AI Agent that responds to chat messages.

Create a New Workflow

  1. In the n8n dashboard, click Start from scratch or Create workflow.
  2. You'll see the Canvas Editor where we build our workflow.
  3. Then add a new node: "On chat message"

    On chat message node

  4. Then go to "Settings" tab and make sure the "Always Output Data" is enabled and then press Escape.

    Settings in n8n

  5. Add an "AI Agent".

AI Agent in n8n

  1. Now just press escape button and add a "OpenRouter Chat Model".

    OpenRouter Chat Model node

  2. Now its time to connect the AI agent to a model. For that to happen we need to setup credentials (you remember the API key you copied from OpenRouter, right?).

    Setup credentials

    Create the new credentials

    Select a free model, BTW you can see a list of free models in OpenRouter or with their details. Here I decided to go with openai/gpt-oss-20b:free:

    Now after you added the new credentials press escape.

  3. Click on the "Open chat" button in n8n and send "Hi" as a message to the model.

    Model response

Congratulations! 🎉🎉🎉 You've built a fully functional, serverless AI agent without writing a single line of code.

Final Exported Workflow

{
  "name": "My workflow",
  "nodes": [
    {
      "parameters": {
        "options": {}
      },
      "type": "@n8n/n8n-nodes-langchain.chatTrigger",
      "typeVersion": 1.4,
      "position": [
        0,
        0
      ],
      "id": "6881d1af-2109-48c1-8b5b-7e13265cbf08",
      "name": "When chat message received",
      "webhookId": "3e263e19-6109-496d-ba12-d2d219272e14",
      "alwaysOutputData": true
    },
    {
      "parameters": {
        "options": {}
      },
      "type": "@n8n/n8n-nodes-langchain.agent",
      "typeVersion": 3.1,
      "position": [
        208,
        0
      ],
      "id": "e035f1ba-5a92-45f6-8a67-12fd5389d7c6",
      "name": "AI Agent"
    },
    {
      "parameters": {
        "model": "openai/gpt-oss-20b:free",
        "options": {}
      },
      "type": "@n8n/n8n-nodes-langchain.lmChatOpenRouter",
      "typeVersion": 1,
      "position": [
        80,
        208
      ],
      "id": "7cc211e8-251c-4515-94d4-602ff25ac1d0",
      "name": "OpenRouter Chat Model",
      "credentials": {
        "openRouterApi": {
          "id": "JqfB4keRYcfUEUMV",
          "name": "OpenRouter account"
        }
      }
    }
  ],
  "pinData": {},
  "connections": {
    "When chat message received": {
      "main": [
        [
          {
            "node": "AI Agent",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "OpenRouter Chat Model": {
      "ai_languageModel": [
        [
          {
            "node": "AI Agent",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1",
    "binaryMode": "separate",
    "availableInMCP": false
  },
  "versionId": "15c48f82-0427-4be3-bd5b-323365ad8be9",
  "meta": {
    "templateCredsSetupCompleted": true,
    "instanceId": "d7f40ac67c918c6b77c1db2f31baa37e504a493b4a22e372f2a513b7bf0ca5a9"
  },
  "nodeGroups": [],
  "id": "6bsOs8ssUv4uxhna",
  "tags": []
}
Enter fullscreen mode Exit fullscreen mode

Good to Knows About n8n

  • You can self-host it free of charge.
  • You can modify it, develop some features on top of it and sell those features.
  • You cannot resell it and compete with n8n itself 😜.
  • It's lisense is called "fair code".

Top comments (0)