Skip to main content

Agent Loop

An agent loop is a loop in which the agent decides at each step which tools to use and how, and which text to generate to respond to the user. The results of the tools are being fed back to the agent in the next iteration.

You can use runTools to implement an agent loop.

Example: Middle School Math Agent

This example agent (Source Code) solves middle school math problems. It uses the Math.js tool to solve the problems.

import { MathJsTool } from "@modelfusion/mathjs-tool";
import dotenv from "dotenv";
import { ChatMessage, ChatPrompt, openai, runTools } from "modelfusion";

// ...

// initial chat:
const chat: ChatPrompt = {
"You are solving math problems. " +
"Reason step by step. " +
"Use the calculator when necessary. " +
"The calculator can only do simple additions, subtractions, multiplications, and divisions. " +
"When you give the final answer, provide an explanation for how you got it.",
messages: [ChatMessage.user({ text: problem })],

// agent loop:
while (true) {
// call the language model and execute the tools:
const { text, toolResults } = await runTools({
model: openai
model: "gpt-4-1106-preview",
temperature: 0,
maxGenerationTokens: 500,
tools: [new MathJsTool({ name: "calculator" })],
prompt: chat,

// add the result to the messages for the next iteration:
ChatMessage.assistant({ text, toolResults }),
ChatMessage.tool({ toolResults })

if (toolResults == null) {
// no more actions, exit the program:
return text ?? "No answer found.";

if (text != null) {
// optionally forward the text to the user

for (const { tool, result, ok, args, toolCall } of toolResults ?? []) {
if (!ok) {
const error = result; // handle errors here

// handle tool results, e.g. by forwarding them to the user:
switch (tool) {
case "calculator": {
// type safe results to the arguments and the result:
const calculation = {
expression: args.expression,


Middle School Math Agent

Source Code

terminal app, agent, tools, GPT-4

Small agent that solves middle school math problems. It uses a calculator tool to solve the problems.

Wikipedia Agent

Source Code

terminal app, ReAct agent, GPT-4, OpenAI functions, tools

Get answers to questions from Wikipedia, e.g. "Who was born first, Einstein or Picasso?"