Task 1.1
Agentic Loops
The core execution cycle that powers every Claude-based agent
📘What You Need to Know
What You Need to Know
An agentic loop is the core execution cycle that powers every Claude-based agent. It is a deterministic control flow pattern — not a prompt trick, not a retry loop, and not a chatbot turn. Understanding this lifecycle is non-negotiable for the exam and for production systems.
The Agentic Loop Lifecycle
The loop follows four steps, repeated until completion:
- •Send a request to Claude via the Messages API, including conversation history (system prompt, prior messages, and any tool results from the previous iteration).
- •Inspect the stop_reason field in the response. This is the authoritative signal for what happens next.
- •If stop_reason is "tool_use": execute the requested tool(s), append tool results to conversation history as a new message, and send the updated conversation back to Claude.
- •If stop_reason is "end_turn": the agent has finished. Present the final response to the user.
The stop_reason field is the only reliable signal for loop control. It is deterministic and unambiguous. Never use natural language parsing, text content checks, or arbitrary iteration caps as your primary stopping mechanism.
Model-Driven Decision-Making
In an agentic loop, Claude decides which tool to call based on the current context. This is model-driven decision-making — the model reasons about the task, evaluates available tools, and selects the appropriate one. This contrasts with pre-configured decision trees or fixed tool sequences where the developer hard-codes which tool runs when.
The exam favours model-driven approaches for flexibility. Claude can adapt to unexpected situations, handle edge cases, and chain tools in ways the developer did not anticipate. However, when business logic requires deterministic compliance (financial operations, security checks, regulatory requirements), programmatic enforcement overrides model-driven flexibility.
The Three Anti-Patterns
Anti-Pattern 1: Parsing natural language signals. Checking if Claude said "I'm done" to determine loop termination is wrong — natural language is ambiguous.
Anti-Pattern 2: Arbitrary iteration caps as the primary stopping mechanism. Caps are acceptable as a safety net, but never as the primary control mechanism.
Anti-Pattern 3: Checking assistant text content as a completion indicator. Claude can return text alongside tool_use blocks in the same response.
Practical Example: The Premature Termination Bug
A developer builds a customer support agent that checks if response.content[0].type == "text" to determine completion. Claude returns a text explanation ("Let me look up your order") alongside a tool_use block. The code sees text in position [0], concludes the agent is finished, and returns an incomplete response.
The fix: replace the content-type check with a stop_reason check. Continue when stop_reason == "tool_use", terminate when stop_reason == "end_turn".
while True:
response = client.messages.create(...)
if response.stop_reason == "end_turn":
return extract_text(response)
if response.stop_reason == "tool_use":
results = execute_tools(response)
messages.append({"role": "assistant", "content": response.content})
messages.append({"role": "user", "content": results})
# Safety cap only — not primary control
if iterations >= MAX_ITERATIONS:
log.warning("Safety cap reached")
break⚠️Exam Traps
Trap: Using response.content[0].type == "text" to determine loop completion
Why it's wrong: Claude can return text alongside tool_use blocks in the same response. Text presence does not indicate completion.
Trap: Setting arbitrary iteration caps (e.g., stop after 10 loops) as the primary stopping mechanism
Why it's wrong: Caps either cut off useful work or run unnecessary iterations.
Trap: Parsing natural language phrases like "I'm done" to decide loop termination
Why it's wrong: Natural language is ambiguous and unreliable.
Trap: Forcing tool_choice to "any" to prevent the agent from returning text
Why it's wrong: This forces tool use even when the agent is genuinely finished, creating an infinite loop.
✓Concept Check
A developer's agent sometimes terminates prematurely when Claude returns text alongside a tool call. Their loop checks response.content[0].type == "text" to determine if the agent is finished. What should they change?
🔨Build Exercise
Intermediate45 minutesBuild a Multi-Tool Agent Loop
What you'll learn
- •Implement the agentic loop lifecycle with the Messages API
- •Handle tool_use and end_turn stop_reason values correctly
- •Append tool results to conversation history for multi-turn execution