# Agentic loops and stop_reason control

*Claude Certified Architect - Foundations Study Guide · Agentic Architecture & Orchestration · Last updated October 22, 2026 · https://www.certifyforce.com/study-guide/claude-certified-architect-foundations/agentic-architecture-and-orchestration/agentic-loops-and-stop-reason-control*

How a client-tool agentic loop works on the Messages API and in the Agent SDK: continue on stop_reason tool_use, branch on every other value, append full assistant turns and all tool_result blocks, and keep turn and budget caps as backstops that report the run as incomplete.

- Certification: Claude Certified Architect - Foundations
- Exam section: Agentic Architecture & Orchestration
- Study guide: https://www.certifyforce.com/study-guide/claude-certified-architect-foundations

**Official exam objective:**

> Design and implement agentic loops for autonomous task execution

## What the exam expects

- Write loop control that continues on `stop_reason` `"tool_use"` and handles every other value it exits on.
- Append each assistant response and its `tool_result` blocks to one running `messages` list.
- Answer parallel tool calls correctly: one `tool_result` per `tool_use` block, all in the next user message, nothing after them.
- Reject the named anti-patterns: parsing natural-language phrases, treating any text block as completion, or using an iteration cap as the primary stop.
- Choose model-driven tool selection over hard-coded sequences for open-ended work, and read Agent SDK result subtypes.

Key terms: **agentic loop** (send, inspect `stop_reason`, run tools, return results, repeat); **`stop_reason`** (the response field that says why Claude stopped generating); **`tool_result`** (the user-turn block that answers a `tool_use` block by `tool_use_id`); **`ResultMessage`** (the Agent SDK message that ends the loop, with a `subtype`); **backstop** (a turn or budget cap that only catches runaway runs).

## Key facts

| Fact | Detail | Source |
|---|---|---|
| Loop continue condition | `stop_reason == "tool_use"`; exit on any other value | [How tool use works](https://platform.claude.com/docs/en/agents-and-tools/tool-use/how-tool-use-works) |
| Current `stop_reason` values | `end_turn`, `max_tokens`, `stop_sequence`, `tool_use`, `pause_turn`, `refusal`, `model_context_window_exceeded` | [Stop reasons and fallback](https://platform.claude.com/docs/en/build-with-claude/handling-stop-reasons) |
| `pause_turn` | Server-tool loop hit its limit (default 10 iterations); send the response back as-is | [Stop reasons and fallback](https://platform.claude.com/docs/en/build-with-claude/handling-stop-reasons) |
| `refusal` | Returned as a normal HTTP 200; `stop_details` names the category | [Stop reasons and fallback](https://platform.claude.com/docs/en/build-with-claude/handling-stop-reasons) |
| Parallel results | One `tool_result` per `tool_use`, all in one user message, before any text | [Parallel tool use](https://platform.claude.com/docs/en/agents-and-tools/tool-use/parallel-tool-use) |
| Tool failure | Return `tool_result` with `is_error: true`; the loop keeps running | [Tutorial: Build a tool-using agent](https://platform.claude.com/docs/en/agents-and-tools/tool-use/build-a-tool-using-agent) |
| SDK limits | `max_turns` (tool-use turns) and `max_budget_usd`; no limit by default | [How the agent loop works](https://code.claude.com/docs/en/agent-sdk/agent-loop) |
| `result` field | Present only on subtype `success` | [How the agent loop works](https://code.claude.com/docs/en/agent-sdk/agent-loop) |

## Check yourself

- Why must the loop check `stop_reason` rather than whether the reply contains text?
- When the response has `stop_reason: "pause_turn"`, what should the application send next?
- How must the loop answer a response with three `tool_use` blocks?
- Why should code check the Agent SDK `ResultMessage` `subtype` before reading `result`?
- When does a model-driven agent fit better than a hard-coded tool sequence?

## Sources

- [How tool use works](https://platform.claude.com/docs/en/agents-and-tools/tool-use/how-tool-use-works)
- [Stop reasons and fallback](https://platform.claude.com/docs/en/build-with-claude/handling-stop-reasons)
- [Tutorial: Build a tool-using agent](https://platform.claude.com/docs/en/agents-and-tools/tool-use/build-a-tool-using-agent)
- [Parallel tool use](https://platform.claude.com/docs/en/agents-and-tools/tool-use/parallel-tool-use)
- [Handle tool calls](https://platform.claude.com/docs/en/agents-and-tools/tool-use/handle-tool-calls)
- [How the agent loop works](https://code.claude.com/docs/en/agent-sdk/agent-loop)
- [Building effective AI agents](https://www.anthropic.com/research/building-effective-agents)
- [Claude Certified Architect – Foundations Exam Guide v1.0](https://everpath-course-content.s3-accelerate.amazonaws.com/instructor%2F6nizmqk8tpzpfjvt6qmmav7rh%2Fpublic%2F1783542750%2FClaude+Certified+Architect+%E2%80%93+Foundations+Exam+Guide.pdf)

## About the full unit

The "Core concepts", "Exam traps" and "Worked scenario" sections of this unit, its 11 linked practice questions, and unlimited timed mock exams are included with the Claude Certified Architect - Foundations pack (USD $19): https://www.certifyforce.com/practice-exams
