Design and implement agentic loops for autonomous task execution
What you should be able to do
- Drive an agentic loop from stop_reason, continuing on "tool_use" and terminating on "end_turn".
- Append every tool result to the conversation before the next request, so the model reasons over what actually came back.
- Tell a model-driven agent apart from a pre-configured decision tree, and say which one a requirement calls for.
- Recognise the three loop anti-patterns — text parsing, iteration caps as the stop condition, and text-presence as a completion signal.
Exam traps (5)
End the loop when the assistant's reply reads as finished — it says "done" or stops asking for tools.
Completion phrasing is generated text, so it varies run to run and in every language the model can answer in. The loop then ends early on a polite interim summary, or never ends at all.
Termination is a protocol fact, not a reading-comprehension task. Branch on stop_reason and nothing else.
Cap the loop at N iterations and treat hitting the cap as the task being finished.
A cap cannot distinguish a finished task from a stuck one, so long legitimate work gets truncated and the real failure is never surfaced.
Keep a cap as a runaway backstop that raises an error, and let "end_turn" be the successful exit.
Treat any turn that contains assistant text as the final answer.
A single turn can carry text and tool_use blocks together — the model often narrates before it calls a tool. Stopping there drops the tool call entirely.
Text and tool use are not exclusive. Only stop_reason tells you the turn was terminal.
Re-send the original prompt each iteration and let the model retry from scratch.
Without tool results in the history the model has no evidence the call ever happened, so it requests the same tool again — an infinite loop that bills every pass.
Each iteration extends one growing conversation. Results are appended, not discarded.
Hard-code the tool order once the happy path is known — it is faster and more predictable.
That is a workflow, not an agent: it cannot react to what a tool actually returned, so every unanticipated result falls through.
Let the model choose the next call from context. Where a specific order genuinely must hold, enforce that one ordering programmatically instead of scripting the whole path.
Where the guide and the current docs differ (1)
stop_reason values
Answer this: The loop continues on "tool_use" and terminates on "end_turn" — a two-value protocol.
The docs now say: The documented set is end_turn, max_tokens, stop_sequence, tool_use, pause_turn, refusal and model_context_window_exceeded. A configured stop_sequence also completes successfully, and pause_turn must be re-sent rather than raised as an error. Read it
Primary sources
- Claude API — Messageschecked 2026-08-15
- Claude API — tool usechecked 2026-08-15
- Agent SDK — overviewchecked 2026-08-15
Know cold
- Agentic loop
- Stop reason
- Tool use
- Deterministic workflow
14 practice questions in the bank are tagged to this task statement.
Practise this task in context: open it inside the interactive study guide, which carries the concept cards, the mock quiz and the practice simulation.