DEV Community

Cover image for Get Market Price with Tools in n8n

Get Market Price with Tools in n8n

Now I move from a simple chat to a true AI agent—one that can use external tools to accomplish tasks. I'll build a stock price lookup agent using n8n, OpenAI.

Here again we will need APIs to integrate the new tool, but lets first create a new workflow in n8n:

  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"

    Add on chat message node

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

    Enable output of the on chat message node

  5. Add an "AI Agent".

    Add AI Agent node

    For now press escape button, we will get back to this AI Agent node later.

  6. This time though we're gonna try to use OpenAI. So if you do NOT have an account go there and open one. Then you just need to create a new API key:

    Create new API key in Open AI

    Generate a new API key

    Copy the API key and keep it, we will be using it in the next steps.

  7. Now in OpenAI add 5$ credit using your credit card and disable the auto-reload feature, we do NOT wanna at this point have this enabled. Honestly I believe you would want some more control over it. Like monitoring how tokens are burned and have a limit on how much your workflow should be able to burn.

  8. So now we need to click the "+" under Chat Model and search for and select OpenAI Chat Model. This will be the model used by AI Agent to do our mundane tasks:

    Add OpenAI chat model node

  9. Now its time to use the API key:

    Paste your API key

  10. Select a model, you can go wild here or pick a cheap and fast one like gpt-4.1-mini:

    Select gpt-4.1-mini model

    Press escape and let's actually confirm something 😁:

    Try to send it a message like this: "Hi I am Jawad" or whatever your name might be, wait for it to reply, and then ask again "what is my name?" in the chat pannel:

    LLMs do NOT have memory

  11. So now let's add memory.

    1. Click the "+" under Memory in the AI Agent node. Select Simple Memory (no credentials required—great for a simple demo, we definitely wanna use sophisticated memories later). Press Escape to return.

      Add simple memory

    2. Click the Reset Chat Session arrow (top of the node) to clear the previous test. Then type again the same message and voilà it knows who you are:

      Simple memory test

      Info

      So what in chat.deepseek.com happens is that each time it is sending the new prompt to the LLM it ocntains all previous prompts and their responses to give you the illusion of memory.

      So every new prompt for every single request constructs a prompt which includes:

      1. The system instructions (persona/rules).
      2. The full history of previous user messages and AI responses.
      3. The current new message.

      And in fact this is why also sometimes after LLM starts to "forget" some stuff, you just have to summarize the whole conversation and open a fresh session. So the context window has more room and memory.

      I put "forget" inside a quotation mark since as you know by now it is not really a memory, it is more like just keep feeding the LLM back the whole conversation so it can predicts the best answer to your prompt.

  12. So we have done all of this to be able to get the stock price. But before adding a new tool to our AI Agent we need an API to talk to and ask it about stock price. Here we will use https://marketstack.com

    Note: you might encounter some issues in their website while registering yourself. If it did NOT work in one browser go and try it in another.

    Now you can see your API key, just copy it.

  13. Now let's add a new tool to our n8n. In the AI Agent node, click the "+" under Tool. Search for and select Marketstack Tool.

    Add Marketstack tool

    Then just you need to add your API key there. And jut click the magic button for ticket → select Let the model define this parameter. So ticker is the stock symbol and it can be "GOOGL" for Google, "APPL" for Apple, you get the idea, right?

    Add API key + ticker

  14. Now if you prompt it "Please tell me the end-of-day equity price for APPL"

    Tip

    It is quite possible the tool fail to do its job, then you usually get to see what might be wrong, so if your prompt did NOT work. Do NOT worry and just try to resolve it. For example here we have an issue with not specifying the date:

    Failure because the return all option is not on

    But keep in mind sometimes it is just some misconfiguration in your n8n nodes and not the prompt or external API. In this case it was all happening because of these two:

    Enable to fetch many

    Turn on latest

    And after you fix this little bug you will see it:

    Apple equity price

Exported Workflow

{
  "name": "My workflow",
  "nodes": [
    {
      "parameters": {
        "options": {}
      },
      "type": "@n8n/n8n-nodes-langchain.chatTrigger",
      "typeVersion": 1.4,
      "position": [
        0,
        0
      ],
      "id": "f61aff61-748a-420b-95f7-2355bfcf8a68",
      "name": "When chat message received",
      "webhookId": "13bc2c00-83e7-49ad-9016-b2bfab2fbc0b",
      "alwaysOutputData": true
    },
    {
      "parameters": {
        "options": {}
      },
      "type": "@n8n/n8n-nodes-langchain.agent",
      "typeVersion": 3.1,
      "position": [
        208,
        0
      ],
      "id": "934416e0-5966-4ad9-bfc9-22de51ed394c",
      "name": "AI Agent"
    },
    {
      "parameters": {
        "model": {
          "__rl": true,
          "value": "gpt-4.1-mini",
          "mode": "list",
          "cachedResultName": "gpt-4.1-mini"
        },
        "builtInTools": {},
        "options": {}
      },
      "type": "@n8n/n8n-nodes-langchain.lmChatOpenAi",
      "typeVersion": 1.3,
      "position": [
        144,
        208
      ],
      "id": "b8fdd0de-36a9-4232-9cef-f224c3f046d8",
      "name": "OpenAI Chat Model",
      "credentials": {
        "openAiApi": {
          "id": "0h4kMjpTValhfJZv",
          "name": "OpenAI account"
        }
      }
    },
    {
      "parameters": {},
      "type": "@n8n/n8n-nodes-langchain.memoryBufferWindow",
      "typeVersion": 1.4,
      "position": [
        272,
        240
      ],
      "id": "24cef214-7210-409d-8f99-74e659777b00",
      "name": "Simple Memory"
    },
    {
      "parameters": {
        "symbols": "={{ /*n8n-auto-generated-fromAI-override*/ $fromAI('Ticker', ``, 'string') }}",
        "returnAll": true,
        "filters": {
          "latest": true
        }
      },
      "type": "n8n-nodes-base.marketstackTool",
      "typeVersion": 1,
      "position": [
        464,
        192
      ],
      "id": "cf60ba15-b3a8-4743-81f0-cca5076687ef",
      "name": "Get many EoD data in Marketstack",
      "credentials": {
        "marketstackApi": {
          "id": "uykspUJ9pFnekPFQ",
          "name": "Marketstack account"
        }
      }
    }
  ],
  "pinData": {},
  "connections": {
    "When chat message received": {
      "main": [
        [
          {
            "node": "AI Agent",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "OpenAI Chat Model": {
      "ai_languageModel": [
        [
          {
            "node": "AI Agent",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Simple Memory": {
      "ai_memory": [
        [
          {
            "node": "AI Agent",
            "type": "ai_memory",
            "index": 0
          }
        ]
      ]
    },
    "Get many EoD data in Marketstack": {
      "ai_tool": [
        [
          {
            "node": "AI Agent",
            "type": "ai_tool",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1",
    "binaryMode": "separate",
    "availableInMCP": false
  },
  "versionId": "897b46b0-a6e1-4881-a520-0cd3149072ca",
  "meta": {
    "templateCredsSetupCompleted": true,
    "instanceId": "d7f40ac67c918c6b77c1db2f31baa37e504a493b4a22e372f2a513b7bf0ca5a9"
  },
  "nodeGroups": [],
  "id": "6bsOs8ssUv4uxhna",
  "tags": []
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)