AI

CCA-F Study Day 21: Agent SDK โ€” Agentic Loops & stop_reason Termination

๐Ÿ“š CCA-F Study Day 21: Agent SDK Overview โ€” Agentic Loops & stop_reason-based Termination

Domain: 1. Agentic Architecture & Orchestration (~25% of exam)

Week 5 ยท Day 1 | Phase A begins

๐Ÿ“– Reading/Watching (20 min)

Agent SDK Overview & How the Agent Loop Works

https://console.anthropic.com/docs/en/agent-sdk/overview

Focus on: the query() function signature, built-in tools (Read, Edit, Bash, Glob, Grep), and how the SDK manages the agentic loop internally. Note how the loop continues until Claude emits an end_turn stop_reason.

How the Agent Loop Works (deep dive)

https://console.anthropic.com/docs/en/agent-sdk/core-concepts/how-the-agent-loop-works

Focus on: the loop cycle (send message โ†’ check stop_reason โ†’ if tool_use, execute tool and loop; if end_turn, terminate). Understand that the loop is driven by Claude's own determination of task completion, NOT by a hard iteration count.

Anthropic Cookbook โ€” Agent Patterns

https://github.com/anthropics/anthropic-cookbook/tree/main/patterns/agents

Skim the agentic loop examples. Pay attention to how stop_reason is checked after each API call to determine whether to continue.

๐Ÿ› ๏ธ Hands-on Exercise (20 min)

Build a minimal agentic loop from scratch (no SDK):

Using the raw Anthropic Messages API (Python), implement a basic agentic loop:

Define 2 simple tools: get_weather (returns hardcoded data) and get_time (returns current time)

Send a prompt that requires both tools: "What's the weather in Seattle and what time is it?"

Implement the loop:

while response.stop_reason == "tool_use":

# extract tool calls, execute them, append results

# send updated messages back to Claude

# loop exits when stop_reason == "end_turn"

Deliberately break it: Add a hard cap of 1 iteration and observe how the agent fails to complete the task. This demonstrates Anti-Pattern #2.

Key insight: The loop should terminate because Claude decides it's done (stop_reason = "end_turn"), not because you imposed an arbitrary cap.

๐Ÿง  Reflection & Notes (20 min)

Reflection Questions:

What is the difference between stop_reason: "tool_use" and stop_reason: "end_turn"? Why is this distinction the foundation of agentic architecture?

If you add a max_iterations = 5 cap, what failure modes does this introduce? When (if ever) is a safety cap appropriate, and how should it differ from a "primary stopping mechanism"?

How does the Agent SDK's query() function abstract away the loop mechanics? What does this buy you vs. implementing the loop yourself?

Depth Check: Explain why an agent tasked with "refactor this 500-line file" might need 12+ tool invocations, and why capping at 5 iterations would produce incomplete work. How would you design a safety mechanism that protects against infinite loops WITHOUT using an arbitrary iteration cap? (Hint: think about token budgets and cost limits.)

Exam-Style Question:

A developer builds a customer support agent that processes refund requests. The agent needs to look up order details, verify eligibility, and process the refund. The developer adds max_turns = 3 to ensure the agent doesn't loop forever. During testing, complex refund cases (split orders, partial refunds) frequently fail to complete. What is the BEST fix?

A) Increase max_turns to 10 to handle complex cases

B) Remove the arbitrary turn cap and let the agent terminate naturally via stop_reason, adding a token/cost budget as a safety net

C) Add a prompt instruction telling Claude to complete within 3 turns

D) Split each refund into multiple sequential agent invocations of 3 turns each

Correct Answer: B

Why the others are wrong:

A โ€” Increasing the cap is still arbitrary; you're guessing at the right number instead of letting the model determine completion.

C โ€” Prompt instructions for loop termination is Anti-Pattern #1 (parsing natural language for loop control). The model might comply by giving incomplete answers to fit within 3 turns.

D โ€” Splitting into sequential invocations loses conversational context and adds fragile orchestration complexity.

โš ๏ธ Anti-Pattern Alert

Anti-Pattern #1: Parsing natural language for loop termination

โŒ Wrong: Checking if Claude's text output contains "DONE" or "TASK COMPLETE" to decide whether to stop the loop

โœ… Right: Check response.stop_reason == "end_turn" โ€” this is a structured, deterministic signal from the API

Anti-Pattern #2: Arbitrary iteration caps as primary stopping mechanism

โŒ Wrong: for i in range(5): call_claude() as the loop structure

โœ… Right: while response.stop_reason == "tool_use": call_claude() with a cost/token budget as a safety backstop (not the primary termination mechanism)

๐Ÿ“ฐ News: Agent SDK Billing Change Paused

Relevant to today's topic: Anthropic paused a planned billing change for the Agent SDK on June 15 that would have introduced separate "Agent SDK credits" distinct from interactive usage. For now, existing pricing remains. This signals Anthropic's commitment to the Agent SDK as a distinct product tier. No action needed, but worth tracking as you build with the SDK.