You are writing the agentic loop for a support agent on the Claude API. After each response you must decide whether to execute tools or hand the final answer back to the customer. Which signal should drive that branch?
- AThe
stop_reasonfield on the response: keep executing tools while it istool_use, and finish when it isend_turn. - BWhether the assistant's text contains a closing phrase such as I hope that helps.
- CWhether the response contains fewer tokens than a threshold you tune per deployment.
- DWhether the loop has run fewer than five iterations.
Correct answer: A. stop_reason is the API's own statement of why generation stopped. tool_use means Claude has emitted one or more tool_use blocks and is waiting on their results; you execute them, append tool_result blocks, and call again. end_turn means Claude finished on its own and the loop terminates. It is a structured field, so the branch is deterministic.
Why not B: Completion phrasing is model- and prompt-dependent. Parsing prose for control flow is the classic anti-pattern this field exists to remove.
Why not C: Response length has no defined relationship to whether tools are pending. A long final answer and a short tool request would both be misread.
Why not D: An iteration cap is a safety valve against runaway loops, not a termination signal. Used as the primary check it truncates work that was still in progress.