Skip to content
Claude Certified Architect – Foundations (CCAR-F) Exam Prep

Lesson 1 of 30 · 7 min

x
1/30

Agentic Loops

Video lesson: CCAR-F 01/30: Agentic Loops

The loop

  1. Send messages and tool definitions to Claude.
  2. Read stop_reason.
  3. If tool_use: run every requested tool, append the assistant turn, then append one user message containing only the tool_result blocks.
  4. Call Claude again. Repeat.
  5. If end_turn: the model finished. Use the answer.

The control signal is the structured stop_reason field, not anything in the text.

What each stop_reason means for the loop

stop_reasonMeaningLoop action
tool_useClaude is calling toolsRun them, return a tool_result for every tool_use block
end_turnFinished naturallyStop and use the response
max_tokensHit your max_tokensRaise the limit and retry, or continue; an incomplete tool_use block means retry with a higher limit
stop_sequenceOne of your stop sequences firedRead stop_sequence to see which
pause_turnA server-tool loop hit its iteration limit (default 10)Send the assistant content back unchanged to continue
refusalClaude declined; HTTP 200, not an errorRead stop_details; retry on a fallback model
model_context_window_exceededResponse filled the context windowTreat as truncated

Message rules that cause real bugs

  • Each tool_use needs a matching tool_result in the next user message, and that message should contain nothing else.
  • Keep the same tools array on the follow-up request.
  • Adding text after tool results can teach Claude to end its turn: you get end_turn with an empty response (2 to 3 tokens). Do not resend the empty turn. If it persists, add a continuation prompt in a new user message.
  • HTTP errors (4xx/5xx) are separate from stop_reason; handle them around the request.

Always bound the loop (design guidance)

Add a maximum step count, a cost budget and timeouts. The Agent SDK exposes max_turns and a spend cap (max_budget_usd / maxBudgetUsd); hitting them ends the query with an error result subtype (for example error_max_turns, error_max_budget_usd), and you can resume the session with a higher limit.

Exam traps

  • Parsing the assistant's text ("I'm done", a keyword) to decide whether to stop. Wrong: use stop_reason.
  • Treating max_tokens as success. The output is cut off.
  • Treating refusal as an exception. It arrives as a normal 200 response.
  • Unbounded loops. A loop with no step limit is a defect, not a design.
  • Skipping a tool_result for one of several parallel tool_use blocks.

References

  1. Handling stop reasons — checked Oct 16, 2026
  2. Subagents in the SDK — checked Oct 16, 2026

Check your understanding

  • A response has stop_reason: "tool_use" with three tool_use blocks. What do you send next?Show answer

    Answer

    One user message with three tool_result blocks and nothing else.
  • The model returns end_turn with empty content right after you sent tool results plus a text hint. Why, and what now?Show answer

    Answer

    Text after tool results can teach it to end the turn. Don't resend the empty turn; add a continuation prompt in a new user message.
  • Which signal ends the loop: the word "finished" in the text, or stop_reason?Show answer

    Answer

    stop_reason.

Progress is saved in this browser.

Next Lesson