Agentic Loops & Core API
Understand how agentic loops work using the Claude Agent SDK. Learn to manage the lifecycle of agentic interactions, including the stop_reason signals, tool result appending, and the control flow of agent execution.
Core concepts you must master for the exam:
Agentic loop lifecycle: stop_reason values ('tool_use' vs 'end_turn') control loop continuation
Tool result appending: after each tool call, results are appended to the conversation for the next iteration
Agent SDK control flow: the SDK handles the loop automatically, but you must understand the mechanics
The agent continues looping as long as stop_reason is 'tool_use'; it terminates on 'end_turn'
Anti-Patterns to Avoid
Parsing natural language output to decide whether to continue the loop instead of checking stop_reason
Setting arbitrary iteration caps as the primary stopping mechanism
Checking assistant text content to determine loop termination
The agentic loop is the core execution pattern for Claude-based agents. Unlike simple request-response interactions, an agentic loop allows Claude to iteratively plan, act, observe, and decide whether to continue or stop.
How the loop works:
The `stop_reason` field in the API response is the only reliable signal for controlling this loop:
The Agent SDK handles this loop automatically, but you must understand the mechanics because the exam tests your knowledge of why certain approaches work and why alternatives fail.
1import anthropic23client = anthropic.Anthropic()4tools = [{"name": "lookup_customer", "description": "...", "input_schema": {}}]5messages = [{"role": "user", "content": "Find customer John Smith"}]67# The Agentic Loop8while True:9 response = client.messages.create(10 model="claude-sonnet-4-20250514",11 max_tokens=4096,12 tools=tools,13 messages=messages14 )15 16 # KEY: Check stop_reason to control the loop17 if response.stop_reason == "end_turn":18 break # Claude is done19 20 if response.stop_reason == "tool_use":21 tool_block = next(22 b for b in response.content if b.type == "tool_use"23 )24 result = execute_tool(tool_block.name, tool_block.input)25 26 messages.append({"role": "assistant", "content": response.content})27 messages.append({28 "role": "user",29 "content": [{"type": "tool_result",30 "tool_use_id": tool_block.id,31 "content": result}]32 })# ANTI-PATTERN: Parsing natural language
while True:
response = get_response()
text = response.content[0].text
if "task complete" in text.lower():
break
if "I'm done" in text.lower():
break# CORRECT: Check stop_reason field
while True:
response = get_response()
if response.stop_reason == "end_turn":
break # Claude decided it's done
if response.stop_reason == "tool_use":
execute_and_continue()Multi-Agent Orchestration
Design and implement multi-agent systems using hub-and-spoke architecture. Learn coordinator roles, subagent context isolation, and parallel execution patterns.
Multi-agent patterns tested on the exam:
Hub-and-spoke architecture: a central coordinator delegates tasks to specialized subagents
Context isolation: subagents have their own context and do not share state directly
Task tool for spawning subagents: allowedTools must include 'Task' for subagent creation
Parallel execution: multiple Task calls in a single response enable parallel subagent work
fork_session: creates branched sessions for parallel exploration without context pollution
Anti-Patterns to Avoid
Overly narrow task decomposition leading to coverage gaps between subagents
Sharing full coordinator context with every subagent (context pollution)
Not providing explicit context when delegating to subagents
Multi-agent orchestration uses a hub-and-spoke architecture where a central coordinator delegates tasks to specialized subagents. Each subagent operates in its own isolated context and returns results to the coordinator.
Why hub-and-spoke beats flat architectures:
The Task tool spawns subagents. Critical configuration:
Context passing rule: Pass ONLY the context specific to each subagent's task. Never share the full coordinator conversation history — it wastes tokens and confuses the subagent with irrelevant information.
1from claude_agent import Agent, Task23coordinator = Agent(4 model="claude-sonnet-4-20250514",5 tools=[6 Task, # Required for spawning subagents7 summarize_results, # Coordinator-level synthesis8 format_report, # Final output formatting9 ]10)1112# Subagent with scoped tool access (4 tools each)13market_researcher = Agent(14 model="claude-sonnet-4-20250514",15 tools=[web_search, read_doc, extract_data, format_citation],16)1718tech_analyst = Agent(19 model="claude-sonnet-4-20250514",20 tools=[read_code, grep_patterns, analyze_deps, format_report],21)2223# Coordinator delegates with EXPLICIT context per subtask24coordinator.run("""25Research AI infrastructure market. Delegate:261. Market research → market_researcher272. Technology analysis → tech_analyst28Pass each subagent ONLY the context relevant to their task.29""")# Sharing FULL coordinator context with subagent
Task(
prompt="Research market size",
context=coordinator.full_conversation_history,
# 90% of this context is irrelevant
)# Passing EXPLICIT relevant context per subtask
Task(
prompt="Research AI infrastructure market size",
context="Focus: market size in USD, YoY growth, top 3 vendors",
# Only what this subagent needs
)Hooks & Programmatic Enforcement
Use hooks for data normalization, tool call interception, and compliance enforcement. Understand when to use programmatic enforcement vs prompt-based guidance.
Hooks and enforcement concepts:
PostToolUse hooks: intercept and modify tool outputs for data normalization
Programmatic enforcement for critical business rules (deterministic, not probabilistic)
Prompt-based guidance for soft preferences and style suggestions
Hook-based blocking: e.g., blocking refunds above $500 and redirecting to escalation
Anti-Patterns to Avoid
Using prompt-based enforcement for critical business rules (unreliable)
Self-reported confidence scores for escalation decisions (model confidence is unreliable)
Sentiment-based escalation (sentiment does not equal complexity)
Hooks provide deterministic, programmatic enforcement of business rules. They intercept tool calls before or after execution, allowing you to block, modify, or augment behavior without relying on the model's compliance.
The critical distinction the exam tests:
Types of hooks:
Example: A $500 refund limit is a critical business rule. If you put it in a prompt, the model might process a $700 refund anyway. A PostToolUse hook guarantees the block.
Valid escalation triggers:
Invalid triggers (anti-patterns):
1from claude_agent import Agent, Hook23# PostToolUse hook: Block refunds above $5004def refund_limit_hook(tool_name, tool_input, tool_output):5 if tool_name == "process_refund":6 amount = tool_input.get("amount", 0)7 if amount > 500:8 return {9 "blocked": True,10 "reason": f"Refund ${amount} exceeds $500 limit",11 "action": "escalate_to_human",12 "context": {13 "customer_id": tool_input.get("customer_id"),14 "requested_amount": amount,15 "agent_limit": 500,16 }17 }18 return tool_output # Allow all other tool calls1920agent = Agent(21 model="claude-sonnet-4-20250514",22 tools=[lookup_customer, check_order, process_refund],23 hooks={"PostToolUse": [refund_limit_hook]},24)# ANTI-PATTERN: Prompt-based enforcement system_prompt = """ IMPORTANT: Never process refunds above $500. If a refund is above $500, escalate to a human. """ # This is probabilistic — the model CAN and # WILL sometimes ignore this instruction
# CORRECT: Hook-based enforcement
def refund_limit_hook(tool_name, tool_input, output):
if tool_name == "process_refund":
if tool_input["amount"] > 500:
return {"blocked": True, "action": "escalate"}
return output
# This runs as CODE, not as a suggestionSession Management & Workflows
Manage agent sessions, including resuming, forking, and preventing stale context. Understand task decomposition strategies from prompt chaining to dynamic adaptive decomposition.
Session and workflow management:
--resume flag: continue previous sessions with preserved context
fork_session: branch sessions for exploration without polluting the main context
Named sessions for organized multi-session workflows
Stale context detection and mitigation in long-running sessions
Prompt chaining vs dynamic adaptive decomposition: choose based on task predictability
Anti-Patterns to Avoid
Ignoring stale context in extended sessions
Using static prompt chains for tasks that require dynamic adaptation
Session management controls how agent conversations persist, resume, and branch.
Key session operations:
--resume): Continue a previous session with full contextfork_session): Create a branch for exploration without polluting the main session--session-name): Organize multi-session workflowsStale context is a critical risk in long-running sessions — data retrieved early may become outdated. Mitigation: periodically re-fetch critical data, use scratchpad files.
Task decomposition strategies:
Dynamic adaptive decomposition is preferred when the task has unknown complexity or intermediate results may change the approach. Prompt chaining works when the workflow is well-defined and each step's input/output is predictable.
1# Resume a previous session (preserves full context)2claude --resume34# Resume a specific named session5claude --resume --session-name "feature-auth-redesign"67# Fork for exploration (inherits context, diverges)8# Changes in fork do NOT affect the main session9claude fork_session --reason "Exploring alternative API"1011# Start a new named session12claude --session-name "sprint-47-backend"# Static prompt chain for a DYNAMIC task
steps = [
"Step 1: Read the codebase",
"Step 2: Find all bugs",
"Step 3: Fix each bug",
]
# What if step 2 finds no bugs?
# Static chains can't adapt# Dynamic adaptive decomposition
agent.run("""
Analyze the codebase for issues. For each:
1. Assess severity and complexity
2. If simple: fix directly
3. If complex: create a plan first
4. After each fix: run relevant tests
Adapt your approach based on what you find.
""")Exam Tips for Domain 1
Always check stop_reason for loop control, never parse natural language
Programmatic hooks for business rules, prompts for preferences
Subagents need explicit context — don't assume they inherit coordinator knowledge
Understand fork_session vs --resume and when to use each
Related Exam Scenarios
Customer Support Resolution Agent
Design an AI-powered customer support agent that handles inquiries, resolves issues, and escalates complex cases. Tests Agent SDK usage, MCP tools, and escalation logic.
Multi-Agent Research System
Build a coordinator-subagent system for parallel research tasks. Tests multi-agent orchestration, context passing, error propagation, and result synthesis.
Test Your Knowledge of Agentic Architecture
Practice with scenario-based questions covering this domain.