Introduction to AI Agent

Shakeel Ali - 16 Apr, 2026

An AI agent is like a digital worker that:

1. Observes (User Input)

2. Decides (Reasoning using AI Model mostly the Large Language Model)

3. Acts (Takes actions using Tools)


Observes

AI Agent can receive user input from console/terminal or HTTP Request. The input is mostly in Natural Language.

 

Decides

AI Agent uses LLM to process the user input. We can provide system prompt to the AI Agent to constraint or to set the focus of the processing of the user input. AI Agent uses system prompt and user input to decide what Tool (function) to call. If the Tool needs parameters, AI Agent extracts parameters from the user input and passes them to the Tool. If required parameters are not available, the user is asked to supply the parameters before they are passed to the Tool.

 

Acts

AI Agent uses Tools (functions) to act upon the user input. Tools perform work, like database search, web search, file search, perform logic in memory, etc.

 

Hands-On:


Let us build a simple but real-world AI Agent in NodeJS using an open-source library named “Pi Agent Core” which is available @mariozechner/pi-agent-core as an npm package.

 

Step 1

– Create a new folder named “ai-agent-app1”

– Open the folder in Visual Studio Code

– Create “.env” file and type LLM_API_KEY=”<<open-ai-api-key>>”


Step 2

– Go to the Terminal Window

– Inside the “ai-agent-app1” folder, run “npm install @mariozechner/pi-agent-core config” to install the Pi core agent library and config library to load the env file content.


Step 3

– Create a file name “agent.js”. In this file we will start with creating tools. Type the following code:


File: agent.js


import { Agent } from “@mariozechner/pi-agent-core”;

import { getModel } from “@mariozechner/pi-ai”;

import { config } from ‘dotenv’;


config({ path: “.env” }); // “.env” file keeps the LLM API Key


/* 4 Tools to act on the user input: additionTool, multiplicationTool, subtractionTool, divisionTool */

const additionTool = {

          name: “addition-tool”, 

          description: “Performs Addition”,

           parameters: {

      type: “object”,

       properties: {

x: { type: “number” },

y: { type: “number” },

         },

         required: [“x”, “y”]

            },

            execute: (_id, params) => {

       const result = params.x + params.y;

        console.log(“Addition Tool: “, params, ” Result: “, result);

          return {

        content: [{ type: “text”, text: result }],

         details: {}

};

}

};


const subtractionTool = {

name: “subtraction-tool”,

description: “Performs Subtraction”,

parameters: {

type: “object”,

              properties: {

           x: { type: “number” },

           y: { type: “number” },

              },

                             required: [“x”, “y”]

            },

            execute: (_id, params) => {

          const result = params.x – params.y;

          console.log(“Subtraction Tool: “, params, ” Result: “, result);

          return {

        content: [{ type: “text”, text: result }],

          details: {}

};

}

};


const multiplicationTool = {

name: “multiplication-tool”,

description: “Performs Multiplication”,

parameters: {

type: “object”,

properties: {

x: { type: “number” },

y: { type: “number” },

             },

              required: [“x”, “y”]

               },

              execute: (_id, params) => {

          const result = params.x * params.y;

           console.log(“Multiplication Tool: “, params, ” Result: “, result);

            return {

       content: [{ type: “text”, text: result }],

       details: {}

};

}

};


const divisionTool = {

name: “division-tool”,

description: “Performs Division”,

parameters: {

type: “object”,

properties: {

x: { type: “number” },

y: { type: “number” },

             },

             required: [“x”, “y”]

             },

             execute: (_id, params) => {

          const result = params.x / params.y;

          console.log(“Division Tool: “, params, ” Result: “, result);

            return {

        content: [{ type: “text”, text: result }],

        details: {}

};

}

};


Each tool has a name, description, parameters and execute function. The description is important for the Agent LLM to find out what the tool does. Parameters are required by the tool as input to perform work. Parameters can be required or optional. The tool has an execute function, that is called with input paramters by the Agent. The execute function can perform work and return the result in a specific format so that Agent can further process the result.


Let us now 

1. Create an agent with LLM, Tools, System Prompt, LLM API Key

2. Subscribe to receive the Agent output

3. Run the Agent to perform the work:


async function main() {


      // Create an Agent with LLM and Tools 

      const mathAgent = new Agent({

     initialState: {

    systemPrompt: “You are an agent to perform Math Operations”,

    tools: [ additionTool, subtractionTool, multiplicationTool, divisionTool ], 

    model: getModel(“openai”, “gpt-4o-mini”),

      },

      getApiKey: () => process.env.LLM_API_KEY // obtain API Key for the OpenAI LLM

       });


// Subscribe a function to receive the result of the Agent execution

let result = “”;

mathAgent.subscribe((event) => {

if (event.type === “message_update” && event.assistantMessageEvent.type === “text_delta”) {

         // Stream just the new text chunk

         result += event.assistantMessageEvent.delta;

}

});


// Run the agent with the user query

await mathAgent.prompt(“10 + 200 * 3 / 2 – 89”);

console.log(“Result of Arithmetic Instruction:”, result);


result = “” ; 

await mathAgent.prompt(“Take 10, add 5, subtract 2, multiply by 6, and divide by 3”);

console.log(“Result of Natural Language Instruction:”, result);


result = “” ;

await mathAgent.prompt(“I had 10 apples, I ate 3 and sold 2. How many left?”);

console.log(“Result of Logic Puzzle:”, result);

}


main(); // call the main function


Now the run the application “node agent.js”. You will see the output correctly generated.


The example demontrates that the agent is able to handle arithmetic instruction, process natural language query and also handle logic puzzle all with the help of LLM and Tools. Even if you remove the tools, the agent will be able to solve all three questions using just an LLM and LLMs are capable of handle arithmetic, and logic puzzles. But still we have create the tools to demonstrate how tools can be built. As we go further, we will build more complex tools, without them agent will not be able to complete its work.


Next: We will create a Terminal UI for the user to type a question. Also, we will add session to the Agent so that it can remember the previously asked questions and its answers and use them to process the new question asked by the user.