Control flow determines what a program does next: it selects paths, repeats tasks, and handles exceptional situations. Traditional software fixes this sequence in code through conditionals, loops, and ordered function calls. section 1.2.2 section 8.1.2
In an LLM agent, part of control flow becomes autonomous. The LLM chooses actions from current context and goals, tools expand the available actions, and a loop repeats decisions until the goal is achieved. section 1.2.2
Code execution makes control flow practical for multi-step agent work:
- Conditionals select actions based on constraints such as price, rating, or available budget. section 8.1.2
- Loops repeat tool calls across products or files without requiring a separate model decision for every item. section 8.1.2
- Control flow combines with data flow and tool composition to transform results and coordinate several operations. section 8.1.2
Filtering products for a shopping cart
Iterate over products returned by search_products, retrieve each price, and add only items priced within the remaining budget and rated at least 4.5. After each purchase, subtract its price and record the product. section 8.1.2
Figure 1.3 The LLM agent’s decision loop is an iterative process of LLM decision-making and tool use.
Use the decision-loop diagram to trace repeated LLM decisions, tool calls, and stopping behavior. section 1.2.2



