Free sample lesson ยท Agentic Architecture & Orchestration

Drive the Agentic Loop

Why did a support agent stop before looking up order 8891?

Lesson 1 of the Wondering course Agentic Architecture & Orchestration

Driving the Agentic Loop

Driving the Agentic Loop

Master client-tool loops, stop reasons, and safety bounds to design and diagnose reliable Claude agent workflows.

The Client Tool Loop

A client tool loop is a repeating cycle between your application and Claude. The process starts when you send the conversation history to the API.

If Claude needs data, it returns a tool call. Your application must execute the tool and return the result to continue the conversation.

The Client-Tool Loop Cycle

Do not stop just because you see text. A support agent might explain a process while simultaneously calling lookup_order for order 8891.

Managing the Conversation State

To keep the loop running, you must append the assistant response and the tool result to your history. This preserves the conversation contract.

Sending only the tool result without the assistant tool call message will break the logic. Claude needs to see its own previous request to reason about the new data.

Maintaining Conversation State
  • Append Assistant: Add the assistant response containing the tool call to your history.
  • Append Result: Add the corresponding tool execution result to preserve the conversation contract.
  • Check State: Use structured response state rather than natural language phrases to decide your next move.

Always use the structured response state to decide your next move. Natural language phrases in the text are not reliable signals for loop control.

Understanding Stop Reasons

The stop_reason field acts like a traffic signal for your workflow. It tells you exactly why the model stopped generating.

Wondering lens
  • Handle every stop reason

An end_turn signal means the assistant is finished. However, a tool_use signal means the loop must continue.

Stop Reason Signals
  • end_turn
  • tool_use

Safety Bounds and Success

Safety caps protect your budget and prevent runaway execution. You might set a limit of 20 turns or a specific cost threshold.

Do not confuse reaching a cap with successful completion. A max_tokens response indicates the output was truncated and is incomplete.

Reliable agents distinguish between a successful final answer and a safety cutoff. Always handle truncation or context window limits as errors or pauses rather than finished work.

Safety Bounds vs Completion

Putting It All Together

Agent Workflow Execution Guide
  • 1. Loop Control: Initialize and maintain the client-tool loop correctly.
  • 2. State Preservation: Maintain exact message ordering and history.
  • 3. Stop Reason Handling: Inspect the stop reason to determine next actions.

Check yourself

When continuing a client-tool loop, the application must append _____ to the conversation history before sending the next API request.

  • both the assistant message and the tool result
  • only the tool execution result

Answer: both the assistant message and the tool result

To preserve the conversation state, both the assistant message (containing the tool call) and the tool result message must be added to the history array so the model can see its own request context.

In a client-tool loop, a stop reason of _____ signals that the loop must continue, while _____ indicates that generation is finished.

Answer: In a client-tool loop, a stop reason of tool_use signals that the loop must continue, while end_turn indicates that generation is finished.

The stop_reason field directs workflow logic: tool_use requires executing requested client tools and continuing, whereas end_turn signals completed assistant output.

An agent halts after reaching its configured maximum limit of 20 turns without returning an end_turn signal. How should the application interpret this state?

  • As an incomplete execution stopped by a safety bound
  • As a successful completion of the underlying task
  • As a normal prompt truncation caused by max_tokens
  • As an indicator that tool execution failed

Answer: As an incomplete execution stopped by a safety bound

Turn caps and cost limits are independent safety safeguards to prevent runaway execution, not proof that the task was completed successfully.

Keep going

This is lesson 1 of 7 in Agentic Architecture & Orchestration. Next up:

  1. Orchestrate Through a Coordinator
  2. Pass Context with Provenance
  3. Enforce Workflows and Handoffs
  4. Place Hooks at Boundaries
  5. Decompose for the Task Shape

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.