Execute an agent task in a loop until exit is called. Provides planning,
progress tracking, and controlled exit functionality.
The agent runs until the model calls the built-in exit tool, the
maxIterations limit is reached, or an abortSignal is triggered.
Message injection
The messages array is used directly (not copied), so you can push new
messages onto it at any point while the agent is running. They will be
included in the context at the start of the next iteration:
constmessages = [{ type:'user', text:'Perform the task.' }]
// inject a user message or system notification mid-run: messages.push({ type:'user', text:'Also handle edge case Y.' }) messages.push({ type:'context', text:'System: disk usage at 90%.' })
The agent also appends its own bot responses to the same array as each
iteration completes, so messages reflects the full conversation history.
Execute an agent task in a loop until exit is called. Provides planning, progress tracking, and controlled exit functionality.
The agent runs until the model calls the built-in
exittool, themaxIterationslimit is reached, or anabortSignalis triggered.Message injection
The
messagesarray is used directly (not copied), so you can push new messages onto it at any point while the agent is running. They will be included in the context at the start of the next iteration:The agent also appends its own
botresponses to the same array as each iteration completes, somessagesreflects the full conversation history.