GuidesAI and agents

The AI agent loop: step budget, stopping and handing back

By the Banzai Labs teamUpdated on 3 min read

ClaudeToolResultBudgetstep 7 / 15then stopEvery turn costs: set a step count, a budget and an exit
The agent calls tools in a loop; a step budget stops it before it runs forever.

The loop

const MAX_STEPS = 15;
for (let step = 1; step <= MAX_STEPS; step++) {
  const res = await client.messages.create({ model: process.env.CLAUDE_MODEL!, max_tokens: 2048, tools, messages });
  messages.push({ role: 'assistant', content: res.content });
  if (res.stop_reason !== 'tool_use') return res; // final answer

  const results = [];
  for (const block of res.content) {
    if (block.type !== 'tool_use') continue;
    results.push({ type: 'tool_result', tool_use_id: block.id, content: await run(block.name, block.input) });
  }
  messages.push({ role: 'user', content: results });
}
return summarizeAndStop(messages); // last call without tools

The four safeguards

  • StepsA maximum number of turns.
  • TokensA running total not to exceed.
  • TimeAn overall timeout for the task.
  • RepetitionSame call, same arguments: stop.

What to log on every turn

Data What for
Tool and arguments Understand what the agent did
Input and output tokens Track the cost of the task
Tool duration Find the slow step
Any error Send it back to the model, or stop

A clean exit

  1. 1Limit reachedThe loop stops.
  2. 2Last turn without toolsThe model summarizes what is done and what remains.
  3. 3Hand backTo the user, with that summary.

The basics: tool use and AI agents on real data. The cost: cap your AI API bill.

Frequently asked questions

How many steps should I allow?
It depends on the task. Measure the number of steps on real cases, then set the limit a little above. A task that often hits it is poorly split or poorly tooled.
What should happen when the limit is reached?
Ask the model for one last turn without tools to summarize what is done and what is missing, then hand control back to the user or a human.
How do I spot a loop going in circles?
By comparing calls: the same tool with the same arguments several times in a row is a signal. Stop and report it.

Read next

All guides