Loading...
while (true) {
const res = await llm.generate({ messages, tools });
if (!res.toolCalls.length) return res.text;
messages.push(res.message, ...(await runTools(res.toolCalls)));
}for (let step = 0; step < MAX_STEPS; step++) {
const res = await llm.generate({ messages, tools });
if (!res.toolCalls.length) return res.text;
messages.push(res.message, ...(await runTools(res.toolCalls)));
}
throw new Error(`Agent stopped after ${MAX_STEPS} steps`);A step limit turns a possible endless tool loop into a clear, handled failure.
A chatbot replies to messages. An agent runs in a loop, calling tools and acting on their results until the task is done.
When the steps are known in advance. A fixed workflow is cheaper, faster and easier to test.
Give it minimal permissions, run risky actions in a sandbox, and require human approval for changes that are hard to undo.