The agent loop
An agent is a model in a loop: it reads context, decides to call a tool or answer, receives tool results, and repeats until done.
Stable: this rarely changes. Reviewed 17 Oct 2026.
Key ideas
- Core loop: call model, if it returns tool calls execute them, append results, call again; stop on a final answer or a limit.
- Always set guards: max turns, max tokens, wall-clock timeout, cost budget.
- Run independent tool calls in parallel when the model emits several.
- Return tool errors to the model as results so it can recover; do not crash the loop.
- Log every step (input, tool call, result, tokens) for debugging and evals.
In code
messages = [{'role': 'user', 'content': task}]
for turn in range(MAX_TURNS):
resp = client.messages.create(model=MODEL, tools=TOOLS,
messages=messages, max_tokens=4096)
messages.append({'role': 'assistant', 'content': resp.content})
if resp.stop_reason != 'tool_use':
break
results = [run_tool(b) for b in resp.content if b.type == 'tool_use']
messages.append({'role': 'user', 'content': results})Common mistakes
- Using an agent where a fixed workflow would do. Agents cost more and are harder to test.