Lv.1 0 XP

The Agentic Loop — How It Actually Works

Core 8 min +35 XP
💡
THE ANALOGY

A chef who checks if the dish is done, acts if not, checks again — looping until complete. Never stops based on a timer. Always stops based on the actual state of the dish.

⚠️ EXAM TRAP — The Wrong Answer People Choose

Parsing natural language from Claude's response to detect completion — e.g. checking if the word 'done' appears in the text. This has a non-zero failure rate and breaks silently.

KEY POINTS
1 The agentic loop has exactly two exit conditions: stop_reason is 'end_turn' OR stop_reason is 'max_tokens'. Never anything else.
2 When stop_reason is 'tool_use': execute the tool, append the tool_result to messages, call the API again. The loop continues.
3 When stop_reason is 'end_turn': extract the text content from the response. The task is complete. Return it.
4 Tool results MUST be appended to the conversation history so Claude can reason about what happened and decide the next action.
5 Never set arbitrary iteration caps as the PRIMARY stopping mechanism — they mask errors instead of surfacing them.