Free sample lesson ยท Claude Agent Loops and Orchestration

The Four-Step Agent Loop

A refund agent calls lookup_order, gets a result, then asks the same question again. Why?

Lesson 1 of the Wondering course Claude Agent Loops and Orchestration

Overview of the Four-Step Agent Loop

Overview of the Four-Step Agent Loop

Master the deterministic control flow of agentic loops to build robust Claude applications, orchestrate multi-agent systems, and prepare for CCAR-F Domain 1.

The Engine of an Agent

An agentic loop is the core execution cycle behind every Claude-based agent. It is a deterministic control flow defined in your code.

This loop is not a prompt trick or a simple retry after a network error. It is a repeating four-step process.

The loop lives in the developer code, not inside the model. Claude contributes exactly one decision per iteration.

Agentic Loop vs Simple Prompt
  • Agentic Loop
  • Simple Prompt

The Four-Step Cycle

First, send the entire conversation history to the Messages API. Second, inspect the stop_reason field in the response.

If the reason is tool_use, execute the requested tools. If it is end_turn, the agent has finished its task.

Finally, append any tool results to the history and send the updated conversation back to the API to start again.

The Four-Step Loop Lifecycle

The Importance of Appending

Executing a tool and appending its result are two separate acts. Only the second act changes the next request.

If you run a tool like lookup_order but do not append the result, the next request is identical to the last one.

Claude will see the same history and request the same tool again. The loop will spin without making any progress.

The Tool Result Lifecycle
  • Execute Tool: Run the requested tool such as lookup_order in your code.
  • Append Result: Add the tool result message to the conversation history.
  • Next Iteration: Send the updated history back to the API so progress can continue.

Debugging the Loop

Naming these four steps helps you turn vague complaints into specific bugs. A confused agent often indicates a break in step three.

For example, a support agent might call a tool and then ask the same question. This means the tool result never reached the history.

Always ensure tool results are formatted as tool_result messages before the next iteration begins.

Agent Bug Diagnostic Guide

How to Apply It in Real Life

Debugging a Silent Tool Failure
  • Inspect Stop Reason: Read the stop_reason field to confirm the model requested tool_use.
  • Verify Tool Execution: Ensure the code executes the requested tool successfully.
  • Append to History: Explicitly append the output as a tool_result message before the next API call.

Check yourself

In a Claude application, the four-step agentic loop runs inside _____.

  • the developer's application code
  • the Claude model internally

Answer: the developer's application code

The agentic loop is a deterministic control flow defined and executed in developer code, not inside the model.

The four-step agent loop runs in a fixed order: first _____, second _____, third _____, and fourth _____.

Answer: The four-step agent loop runs in a fixed order: first send history, second check stop_reason, third execute requested tools, and fourth append tool results.

The deterministic cycle begins by sending conversation history, inspecting the stop_reason, running tools if requested, and appending results back to history before the next turn.

An agent executes a requested tool like lookup_order, but the code fails to append the resulting tool_result message to history before the next API call. What will happen on the next iteration?

  • Claude sees the same conversation history and requests the same tool call again.
  • Claude automatically reads the executed tool output directly from the environment.
  • The API throws an error because tool execution automatically updates server-side state.
  • The loop immediately terminates because stop_reason changes to end_turn.

Answer: Claude sees the same conversation history and requests the same tool call again.

Because the tool output was never appended, the request history sent to the Messages API is identical to the previous one. Seeing no new information, Claude requests the exact same tool again, causing the loop to spin.

Keep going

This is lesson 1 of 11 in Claude Agent Loops and Orchestration. Next up:

  1. Reading stop_reason, Not Text
  2. Workflows Versus Agents
  3. The Coordinator at the Hub
  4. Subagents Inherit Nothing
  5. The Subagent Spawn Gate

Start this course free

Wondering turns any topic into short lessons you can read, watch, or listen to, with practice questions and an AI tutor.