liveBook · Manning

Control flow

As explained in Build an AI Agent (From Scratch)

Control flow is the sequence and selection of computational operations that determines what a program does next.

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

How Build an AI Agent (From Scratch) covers it

How deep it goes
Explains selection, repetition, exception handling, and agent looping through concrete scenarios and Python listings.
Examples
Uses research agents, multi-file processing, and budget-and-rating product filtering as concrete examples.
What it assumes
Assumes familiarity with basic programming constructs while grounding agent behavior in tools, goals, context, and repeated decisions.
What you can do after
Apply control flow through Python loops and conditionals to coordinate tools and automate multi-step processing.
How it connects
Connects control flow with LLM agents, tools, loops, data flow, tool composition, and code execution.

Also explained in

Compare how 4 books explain control flow →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build an AI Agent (From Scratch). The section links point to the parts of the book it draws on.