GuidesAI and agents
The AI agent loop: step budget, stopping and handing back
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
- 1Limit reachedThe loop stops.
- 2Last turn without toolsThe model summarizes what is done and what remains.
- 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.