An agent is a loop: decide, call a tool, look, repeat

Explain how an agent loop works, and say when a single call or a fixed workflow would do the job better.

Marcus's assistant from module 4 could answer a customer's question with one or two tool calls. Then a customer wrote in asking to combine two orders into one delivery: a grinder ordered last week and a kettle ordered yesterday. If the kettle was out of stock, the customer also wanted Marcus to suggest something similar under S$80.

Why one or two tool calls were not enough

To answer, the assistant has to do several lookups, and which lookups it needs depends on what the earlier ones return. If the kettle is in stock, there is nothing to suggest. If it is out of stock, the assistant has to search for alternatives and check their prices. Marcus needed an agent here, because a fixed script can't plan for every branch of a task whose path depends on results nobody has seen yet.

One run might look like this. It makes five tool calls, chosen one at a time, and each one depends on the result of the last. Then it writes the reply:

look up the grinder order look up the kettle order ask the shipping tool whether both items can ship together check kettle stock, which comes back as zero search products for kettles under S$80 write a reply that explains the delivery options and suggests two alternative kettles

The agent loop and its four moves

In AI fundamentals, lesson 7.4, What an agent is and where it goes wrong, an agent was described as a model that works towards a goal over several steps. In code, an agent is a loop around the tool calling you built in module 4.

The loop has four moves. First, the model looks at the goal and everything so far and chooses an action, usually a tool call. Second, your code checks that action and runs it. Third, the result is added to the conversation. Fourth, the model looks again and decides what to do next, and the cycle repeats until something ends it.

You already wrote most of this loop in lesson 4.4, Give an assistant a calculator and a lookup tool, and the round limit you added there was the start of an agent loop. The difference is in how far the model can go. The lesson 4.4 assistant handled a question in one or two tool calls, while an agent may take many steps and choose its own path through them.

The two ways an agent loop ends

The first exit is the normal one. The model decides it is finished and replies with a final answer instead of a tool call, so your code finds no tool call in the reply and stops the loop.

The second exit is a limit you set, such as a maximum number of steps, a cost budget or a time limit. When the loop reaches that limit, your code stops it, whatever the model wants to do next, and returns a clear message saying the run was stopped. Lesson 5.2, Set limits on steps, time and spend, shows how to set each of these.

Never rely on the first exit alone, because models don't always recognise when they are stuck. An agent with no limits that can't find what it needs can keep trying until your budget runs out.

Choosing a single call, a fixed workflow or an agent

Agents are slower, more expensive and less predictable than the alternatives, so use one only when the task needs it. There are three ways to put a model to work. From simplest to most flexible, they are a single call, a fixed workflow and an agent.

A single call suits a task that needs one look at the input. Examples are summarising feedback, pulling fields out of an email, or putting a support ticket in the right category. Most of what you built in modules 2 and 3 is this kind.

A fixed workflow suits a task whose steps always run in the same order. You write the sequence, and your code follows it and calls the model at each point where it's needed. Marcus's weekly report works this way: pull the week's orders, summarise them, then draft an email to the supplier. The path never changes, so an agent would have nothing to decide. Nearly everything you built in the course Automate your work with AI and no-code tools was a fixed workflow.

An agent suits a task where the next step depends on what the last one found, so you can't write the path in advance. The combined delivery question is one example. Another is researching a question across documents when you don't know which ones hold the answer.

To choose, start with the simplest option. If the task needs only one look at the input, use a single call. If you can draw the steps as a flowchart before you start, build a workflow, because it costs less, runs faster, and fails in ways you can predict and test. Use an agent, with limits set, only when the path depends on what each step turns up.

What each agent step costs you

Every step in the loop is at least one model call. As covered in lesson 1.1, the model keeps no memory, so each step resends the goal, the tool definitions and every earlier result. Each step also adds waiting time, and because each one carries a longer history than the one before, a ten-step run costs more than ten times a single call.

Each step is also another chance for the model to choose badly, pass a wrong argument or misread a result, and a small mistake at step two shapes every step after it. Lesson 5.3, How agents fail: loops, drift and false success, covers what those early mistakes do to a run.

An agent could do most tasks, so asking whether an agent could do one tells you little. Ask instead what the simplest design is that does the job reliably. For the activity, pick three of your own tasks and work through that question for each one.

Pick three tasks you would like to automate. Mark each one as a single call, a fixed workflow or an agent, and write one line on why.

Course

Junxiong-WFG Organisation is an authorised representative of AIA Financial Advisers Private Limited (Reg. No. 201715016G).