AI School · Level 4 · Agents · 2 of 4

Your first agent, step by step

Léelo en español →

Reading time: about 20 minutes

Here we build a real agent, from an empty folder to watching it reason on your screen. You do not need to know how to program. You need to be able to copy, paste and read what comes out, which is a different skill and the only one required.

What you will have at the end: an agent that answers questions about your pharmacy stock — "what am I running low on?", "why did this run out?" — reading your own data. It only reads: it cannot change or send anything. That is deliberate, and it is where you have to start.

The thirteen steps

  1. What you need (15 minutes, once)
  2. The folder and the key
  3. Talking to the model once, with no agent
  4. The data: a CSV you already have
  5. The two tools
  6. Describing them to the model
  7. The loop: thirty lines
  8. Running it and reading what it does
  9. Breaking it on purpose, four times
  10. The five terminal errors
  11. What you can and cannot ask it
  12. Making it run by itself every morning
  13. What you have and what you are missing

1. What you need

Three things, all free. You do this once in your life.

WhatWhat forCost
Node.js (version 20 or newer) Running the program on your computer Free. nodejs.org, big button, next-next
A text editor Writing the files. Visual Studio Code is fine Free
A Google AI Studio key Talking to the model Free with a daily cap. You saw this in the AI Studio lesson

To check Node is installed, open the terminal — on Windows, "Command Prompt"; on Mac, "Terminal" — and type:

$ node --version v22.11.0

If a number appears, you are set. If it says the command is not recognised, it is not installed: back to nodejs.org.

Why Node and not Python, which sounds easier: because everything else in this school — the website, the functions, the chatbot — is JavaScript, and learning one language for everything is worth more than picking the "easiest" one for each task. It is also already installed on any machine where you have built the website.

2. The folder and the key

Create a folder — call it agent — and inside it three empty files: agent.js, .env and stock.csv. From the terminal, go into it:

$ cd agent $ npm init -y

That creates a package.json. Open it and add one line, "type": "module", so you can use the modern way of writing JavaScript:

package.json{ "name": "agent", "version": "1.0.0", "type": "module", "main": "agent.js" }

Now the key. It goes in the .env file, alone, on one line:

.envGEMINI_API_KEY=AIza...your_key_here
The key NEVER goes inside agent.js. This is not fussiness: the day you push that folder to GitHub — and you will — the key is published, and there are bots crawling GitHub looking for exactly that. A leaked key gets spent in hours and the bill is yours. If you are using Git, also add a .gitignore file with the line .env in it.

3. Talking to the model once, with no agent

Before building any loop you have to check the key works. This program asks one question and prints the answer. Nothing else.

agent.js — first version// Reads the .env file with nothing installed: Node 20+ ships with this. import { readFileSync } from 'node:fs'; const env = readFileSync('.env', 'utf8'); const KEY = env.split('=')[1].trim(); const MODEL = 'gemini-2.5-flash-lite'; const URL = `https://generativelanguage.googleapis.com/v1beta/models/${MODEL}:generateContent`; const res = await fetch(URL, { method: 'POST', headers: { 'Content-Type': 'application/json', 'x-goog-api-key': KEY }, body: JSON.stringify({ contents: [{ role: 'user', parts: [{ text: 'Say "it works" and nothing else.' }] }], }), }); const data = await res.json(); console.log(JSON.stringify(data, null, 2));

Save it and run it:

$ node agent.js

If all is well you get a long block with the answer inside. What matters is that "text": "it works" appears somewhere. If instead you get an error saying API key not valid, the key was copied wrong.

Why we print the whole block and not just the text: because what comes back in there is exactly what you will have to read when the agent does something odd. Getting used to looking at the raw response from minute one saves entire afternoons later.

← Previous: what an agent is Next: the tools →