BeginnerLesson 7 of 305 min read

What an AI Agent Is: Tools in a Loop

Meet the agent idea by running a loop with a stub planner, two tools, a polite request for help and a step limit.

In this lesson you will

  • Tell a workflow from an agent
  • Describe the tool loop
  • Explain why step limits matter
  • Apply least privilege to tools

About 22 minutes

A helper that decides for itself

A customer writes to Ravi: ‘Where is order 1042?’ A workflow with fixed steps could handle that, but suppose the questions vary. One customer gives no order number. Another asks about two orders. A third wants to change the delivery address. Writing a fixed path for every case would take forever. This is where the idea of an agent comes in.

Workflows and agents

In a workflow, you decide the order of the steps in advance, and the AI is used inside some of them. In an agent, an AI model chooses the next step itself, looking at what has happened so far. It is often described as a model using tools in a loop: look at the situation, pick a tool, run it, look at the result, pick again, and stop when the job is done. The tools might be ‘look up an order’, ‘draft a reply’ or ‘ask a person’. A widely shared piece of advice from teams who build these systems is to use the simplest approach that works, and to add an agent only when fixed steps are not enough. Agents tend to cost more and take longer, and they are harder to predict.

The loop, in plain code

The program below shows the loop with a stub planner, a small function standing in for the AI model. A real agent would ask a model what to do next. Our stub follows simple rules, which lets us run it here and see exactly how the loop, the tools and the limits fit together. The function takes the current state, a record of what is known, and returns an action and an argument. The loop runs that action, updates the state and asks again. It also has a step limit, so that it can never go on forever.

orders = {"1042": "shipped", "1043": "printing"}

def planner(state):
    # a STAND-IN for a model choosing the next step
    if state["done"]:
        return ("stop", None)
    if "order_id" not in state:
        return ("ask_human", "Which order number?")
    if "status" not in state:
        return ("lookup_order", state["order_id"])
    if "draft" not in state:
        return ("draft_reply", state["status"])
    return ("stop", None)

def run(state, max_steps=6, planner=planner):
    calls = []
    for step in range(1, max_steps + 1):
        action, arg = planner(state)
        if action == "stop":
            return calls, "finished"
        calls.append(action)
        print(f"  step {step}: {action}({arg})")
        if action == "lookup_order":
            state["status"] = orders.get(arg, "unknown")
        elif action == "draft_reply":
            state["draft"] = f"Hi, your order is {arg}."
            state["done"] = True
        elif action == "ask_human":
            return calls, "waiting for a person"
    return calls, "step limit"

print("case 1: order number known")
calls, why = run({"order_id": "1042", "done": False})
print("  stopped:", why, "| tools:", len(calls))
print("case 2: no order number")
calls, why = run({"done": False})
print("  stopped:", why)
print("case 3: a planner that never stops")
calls, why = run({"done": False}, max_steps=4, planner=lambda s: ("lookup_order", "1042"))
print("  stopped:", why, "| calls:", len(calls))
case 1: order number known
  step 1: lookup_order(1042)
  step 2: draft_reply(shipped)
  stopped: finished | tools: 2
case 2: no order number
  step 1: ask_human(Which order number?)
  stopped: waiting for a person
case 3: a planner that never stops
  step 1: lookup_order(1042)
  step 2: lookup_order(1042)
  step 3: lookup_order(1042)
  step 4: lookup_order(1042)
  stopped: step limit | calls: 4

Three cases were run. In the first, the order number was known. The planner looked up the order, then drafted a reply, and stopped with 2 tool calls. In the second, no order number was given, so the planner chose the tool ‘ask a person’ and the run paused, waiting. That is a good behaviour: when the information is missing, ask, do not invent. The third case is the dangerous one. A planner that never decides to stop kept calling the same lookup, and only the step limit ended the run, after 4 calls. Real agents can fall into loops like this, so a limit on steps, time and cost is not optional. It is the seat belt.

Tools are the agent’s hands

An agent can only do what its tools allow. That is a useful safety fact. If the only tools are ‘look up an order’ and ‘draft a reply’, the agent cannot issue a refund however it is asked. Give each tool a clear name and a short description, and give the agent the least power it needs, which is the principle of least privilege. A tool that can delete, pay or send to everyone should come with an approval step, as lesson 9 explains.

When an agent is worth it

  • The steps really do vary from case to case.
  • A wrong move is cheap or can be undone.
  • You can watch and review what it did, step by step.
  • The saving justifies the extra cost and checking.

If your task has the same five steps every time, a plain workflow is simpler, cheaper and easier to trust.

Common mistake: too many tools too soon

Starting with a powerful agent that has many tools. Begin with one or two read-only tools, like a lookup, watch it work for a while and add more only when it behaves.

A teaching stub

The planner here is a hand-written stub, not an AI model, and the orders are made up. Real agent tools and their features differ by product, plan and region, and agents can behave unpredictably, so no promises about results are made.

Think of a task of yours where the steps vary. List the tools an agent would need, mark each as read-only or able to change things, and decide which would need a person’s approval.

Practice for this lesson

Check what you learned

Five quick questions. Pick an answer and you will see straight away whether it is right and why.

What is the difference between a workflow and an agent?

  1. A workflow is paid, an agent is free
  2. An agent has no tools
  3. A workflow follows steps you set; an agent picks its next step itself
  4. They are the same

Agents choose their own path using tools, in a loop.

Why did the second case end by asking a person?

  1. The order number was missing, so the planner chose to ask
  2. The order was shipped
  3. The step limit was hit
  4. The tool broke

When information is missing, asking is better than inventing.

What ended the third case?

  1. A finished reply
  2. A person
  3. The step limit
  4. A refund

The planner never chose to stop, so the limit cut it off.

What does least privilege mean for an agent?

  1. Give it only the tools and power it needs
  2. Give it every tool
  3. Let it choose its own limits
  4. Remove all limits

Fewer powers means fewer ways to cause harm.

When is a plain workflow better than an agent?

  1. When steps vary a lot
  2. When the steps are the same every time
  3. When you want surprises
  4. Never

Fixed steps are simpler, cheaper and easier to trust.

Summary

  • A workflow fixes the order; an agent chooses its next step.
  • Agents work in a loop with tools and a stop rule.
  • Step limits stop endless loops.
  • Give an agent the fewest tools and powers it needs.

Next lesson

Lesson 8 covers what happens when things go wrong: errors, retries, limits and costs.

Questions

Is an agent smarter than a workflow?

Not necessarily. It is more flexible, but also less predictable and usually costlier. Many tasks are better served by a simple workflow.

Can an agent do anything on my computer?

Only what its tools allow. That is why choosing tools carefully is the main safety control.

Is the planner in the lesson a real AI?

No. It is a hand-written stand-in that lets you see the loop clearly.

Progress is saved in this browser.