← All posts

Building AI Agents with Node.js: What I Learned as a Tech Lead in Kanpur

Mohd Zakir Hassan, tech lead from Kanpur, in conversation on a sofa

I'm Mohd Zakir Hassan, a Tech Lead in Kanpur, and I've been building AI agents with Node.js alongside my day job. An agent is a surprisingly small idea: a program that takes a goal, asks a model what to do next, runs a tool, and repeats until the job is done. Here's what I've learned about doing it well. (For the higher-level lessons, see my earlier post on building AI agents from Kanpur.)

Why Node.js works well for agents

Agents spend most of their time waiting on network calls to models and APIs, which is what Node's async model is good at. You also get TypeScript types for tool inputs, a huge npm ecosystem, and the same language as your React frontend. For a full stack JavaScript developer, that last point matters more than any benchmark.

The core loop

Strip away the frameworks and an agent looks like this. Here callModel stands for whichever model SDK you use:

async function runAgent(goal, tools, { maxSteps = 8 } = {}) {
  const messages = [{ role: 'user', content: goal }];

  for (let step = 0; step < maxSteps; step++) {
    const reply = await callModel(messages, describe(tools));
    messages.push(reply);

    if (!reply.toolCalls?.length) return reply.content;

    for (const call of reply.toolCalls) {
      const result = await tools[call.name].run(call.args);
      messages.push({ role: 'tool', id: call.id, content: JSON.stringify(result) });
    }
  }
  throw new Error('Agent hit the step limit');
}

Once you can read this loop, most agent frameworks stop being mysterious.

Design tools like public APIs

When an agent misbehaves, the fix is usually a better tool, not a longer prompt. Give each tool a clear name, a one-sentence description, a validated input schema and a predictable output. I validate arguments with a schema library before running anything, and return readable errors so the model can correct itself on the next step.

Always set limits

The maxSteps above is not decoration. Agents can loop, so I cap steps, set timeouts on every tool, and keep a budget for model calls. A runaway agent should fail loudly and cheaply.

Keep a human in the loop for anything public

Anything that posts, pays or sends a message should wait for approval. I let agents draft and I approve. It is slower, and it is why I trust the results. Make approval a first-class tool, something like requestApproval(), not an afterthought.

Log every step

Record each model call, tool call, input and output with a run ID. When something goes wrong, the log tells you whether the model misunderstood, a tool failed or the input was bad. Without it, you are guessing.

Make tools safe to retry

Models retry, and so do networks. Where a tool changes something, make it idempotent: use an idempotency key or check state first, so a repeated call does not do the work twice.

Test with recorded runs

I test the deterministic parts normally (tools, validation, limits) and replay saved transcripts to check that the loop handles a fixed sequence of model replies. For the model itself, I keep a small set of example goals and review the outputs by hand whenever I change prompts or tools.

Keep prompts short and versioned

I keep the system prompt short and specific: the role, the rules that must never be broken, and the output format. Details about each tool belong in the tool descriptions. Treat prompts like code: keep them in version control, review changes, and record which prompt version produced each run. When you switch models, re-run your example goals before you trust the new one.

Plan for long-running tasks

Some jobs take longer than a web request should. Run them as background jobs on a queue, save the message history after every step so a crashed run can resume, and stream progress to the interface so users aren't staring at a spinner. A plain job queue is enough to start; you don't need a heavy platform.

Treat outside content as untrusted

Anything an agent reads from the outside world (web pages, emails, documents) can contain instructions aimed at the model. Never let such content unlock a tool the user didn't intend, and give each tool the narrowest permissions it needs. The same security thinking you apply to a normal API applies here.

Start with one boring task

Pick one real, repetitive task, build the loop, add limits and logs, and only then add a second tool. If you want to see a small, working example of agent-style tooling, my open-source package council-board is on npm, and the rest of my work is on the projects page. You can find me on X and LinkedIn, and there is more about me as a software developer in Kanpur.