Agentic Loop

Patterns

Definition

The core pattern for AI agents: call Claude, check stop_reason, if 'tool_use' execute the requested tools and append results, then call Claude again. Repeat until stop_reason is 'end_turn'. The number of loop iterations is determined dynamically by task complexity.

Example Usage

while response.stop_reason == 'tool_use': execute tools, append tool_result blocks, call Claude again.