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

You will be able to explain how an agent loop works and when a single call or a fixed workflow is better.

Marcus's assistant from module 4 could answer one question with one or two tool calls. Then a customer wrote: "I ordered a grinder last week and a kettle yesterday. Can you combine them into one delivery, and if the kettle is out of stock, suggest something similar under S$80?" Answering that needs several lookups, and which ones depends on what the earlier ones return. If the kettle is in stock, there is nothing to suggest. If it is not, the assistant has to search for alternatives and check their prices.

A fixed script cannot plan for every branch like that. What Marcus needed was an agent. This lesson explains what one is, and also when you are better off without one.

The loop at the heart of every agent

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. The model looks at the goal and everything so far and chooses an action, usually a tool call, which your code checks and runs. The result is added to the conversation, and then the model looks again and decides what to do next. That cycle repeats until something ends it.

For Marcus's customer, a run might go like this. Look up the grinder order. Look up the kettle order. Ask the shipping tool whether the two can go out 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 alternatives. That is five tool calls, chosen one at a time, each depending on the last.

You already wrote most of this in lesson 4.4, Give an assistant a calculator and a lookup tool. The round limit you added there was the start of an agent loop. The difference now is that the model may take many steps and choose its own path.

How the loop ends

A loop needs a way out, and an agent has two.

The first is the normal one. The model decides it has finished and replies with a final answer instead of a tool call. Your code sees there is no tool call in the reply and stops.

The second is a limit you set: a maximum number of steps, a cost budget or a time limit. When one is reached, your code stops the loop whatever the model wants to do next, and returns a clear message. Lesson 5.2, Set limits on steps, time and spend, covers how to set these. Without them, an agent that cannot find what it needs can keep trying until your budget runs out.

Never rely on the first exit alone. Models do not always recognise when they are stuck.

Most tasks do not need an agent

Agents are slower, more expensive and less predictable than the alternatives, so use one only when you need one.

There are three ways to put a model to work, from simplest to most flexible.

A single call handles a task that needs one look at the input. Summarising feedback is one. So is 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 is for a task whose steps always run in the same order. You write the sequence, and your code follows it, calling the model at each point where it is needed. Marcus's weekly report is a workflow: pull the week's orders, summarise them, draft an email to his supplier. The path never changes, so there is nothing for an agent to decide. If you took Automate your work with AI and no-code tools, nearly everything you built there was a fixed workflow.

An agent handles a task where the next step depends on what the last one found, and you cannot write the path in advance. The combined delivery question is like that. So is researching a question across documents when you do not know which ones hold the answer.

If you can draw the steps as a flowchart before you start, build a workflow. It will cost less, run faster and fail in ways you can predict and test.

Every step has a price

Each loop step is at least one model call. Remember from lesson 1.1 that the model keeps no memory, so every step resends the goal, the tool definitions and every earlier result. A ten-step run does not cost ten times a single call. It costs more, because each step carries a longer history than the one before.

Each step also adds waiting time, and each is another chance for the model to choose badly, pass a wrong argument or misread a result. A small mistake at step two then shapes every step after it, and lesson 5.3, How agents fail: loops, drift and false success, looks at what that does to a run.

So the question to ask of every task is not "could an agent do this?", because an agent could do most things. Ask what the simplest design is that does the job reliably. The activity below has you put three of your own tasks through that question.

Take three tasks you would like to automate and mark each as a single call, a fixed workflow or an agent, with a reason.

Course

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