Building a Simple Agent Runtime With Node.js
You built an agent that calls a model, picks a tool, and prints an answer. It works fine in a script. Then you try to run it for real, and it falls apart. No memory between steps. No retry when a call fails. No record of what actually happened. That's the moment every builder finds out they didn't build an agent. They built a function pretending to be one. What they actually needed was an agent…
An agent runtime is a system that enables an agent to maintain its context between calls. It keeps track of state, determines the next step, and directs work to models and tools. Without a runtime, an agent would forget everything after each function call and wouldn't be able to respond, retry, or remember previous interactions.
The agent runtime loop consists of four main steps: perceive, decide, act, and observe. In the perception step, the current state and input are read. In the decision step, the model is asked what action to take next. During the action step, a tool is called or an answer is returned. Finally, in the observe step, the result is saved, and the state is updated.
To build an agent runtime in Node.js, start by creating a simple project with Node.js and the node-fetch package. Create an AgentRuntime class that owns the loop, state, and a maximum number of steps. The run method initiates the loop, calling the model for decisions and tools for actions. A callTool method is included to handle tool execution, with error handling.
To add memory management, include methods to add memory and trim history to a specified limit. Tools are defined as functions and registered by name, allowing the runtime to seamlessly call them during the action step.
Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.