Learn · Claude Code fundamentals · CCAR-F task 1.1
The agentic loop
Gather, act, observe, repeat: the loop behind every Claude Code session, and why you branch on stop_reason.
Key ideas
- Claude only asks; the harness (Claude Code or your own code) runs the tools.
- The loop is: gather context, act, observe the result, repeat until done.
- Branch on stop_reason: tool_use means run the tools and call again; end_turn means stop.
- Return every tool_result for parallel calls in one user message, matched by tool_use_id.
- Report tool failures as tool_result with is_error: true so Claude can adapt.
- A turn or budget cap (max_turns, max_budget_usd) is a safety backstop, not the stop condition.
Worked example: A correct loop in the Messages API
This is what Claude Code and the Agent SDK do for you. Note the single branch on stop_reason, the one message of tool results, and that any stop reason other than tool_use ends the loop instead of repeating it.
Exam traps
- Stopping when the reply contains text. Claude often writes text and asks for a tool in the same turn.
- Using an iteration cap as the main way to end the loop.
- Sending parallel tool results as several separate user messages.
- Raising an exception on a tool error instead of returning is_error: true.
Official docs
Practise CCAR-F questions · Next lesson: Built-in tools