epochs

Learn · Module 10 · Agents · Lesson 1 of 6

The agent loop

An agent is a model that calls tools in a loop until a task is done. The loop is simple; making it reliable is the hard part.

4 minute read

In short
  • An agent calls tools in a loop, reading each result to decide the next step.
  • Each step re-sends the growing conversation, so cost rises quickly.
  • Always cap steps and cost, and check tool calls in code.

In plain words

In the Tools lesson, the model asked for one tool call and then answered. An agent keeps going: it looks at the result, decides the next step, calls another tool, and repeats until it decides the task is finished or hits a limit.

The whole loop
messages = [task]
for step in range(MAX_STEPS):
    reply = model(messages, tools)
    if reply.is_final_answer:
        return reply.text
    result = run_tool(reply.tool_call)   # your code, with checks
    messages += [reply, result]
return "Stopped: step limit reached"

Worked example: fixing a failing test

  1. 1
    Step 1: look

    The agent runs the tests to see what fails.

    run_tests() → test_total fails: expected 110, got 100
  2. 2
    Step 2: read

    It opens the file named in the error.

    read_file("cart.py")
  3. 3
    Step 3: change

    It spots that tax is never added and edits the function.

    edit_file("cart.py", …)
  4. 4
    Step 4: check

    It runs the tests again to confirm.

    run_tests() → all pass
  5. 5
    Step 5: finish

    It reports what it changed and why.

    "Fixed: total() now adds 10% tax."

Worked example: the cost grows each step

Every step re-sends the whole conversation so far. If the task starts at 2,000 tokens and each step adds 500 tokens of tool results, six steps send:

Step 12000 tokens
Step 22500 tokens
Step 33000 tokens
Step 43500 tokens
Step 54000 tokens
Step 64500 tokens

That's 19,500 input tokens in total, about $0.06 at $3 per million, compared with 4,500 tokens if it had been one call. Agents are powerful and expensive; prompt caching and trimming old tool results help a lot.

Your turn

What is the single most important safety feature of an agent loop?

What to remember

Give the agent a way to see its own results (run tests, read output), because checking work is what makes agents reliable. Cap steps, tokens and time. Log every step so you can replay what it did.

Common mistakes

  • Calling any chatbot an agent; the key is that the model chooses actions in a loop.
  • Building an agent without a step limit or a way to stop.
  • Ignoring that each extra step adds cost, latency and another chance to fail.
Check yourself

Answer in your own words, out loud or on paper, before you open the model answer.

What is an AI agent? How is it different from a single LLM call?

A single LLM call takes input and returns output once. An agent runs a model in a loop where the model decides what to do next. At each step it looks at the goal and everything so far, chooses an action, like calling a search tool, reading a file or running code, sees the result, and decides the next step, until it finishes or hits a limit. So the model controls the path, rather than the developer fixing it in advance. That makes agents good for open-ended tasks where the steps aren't known upfront, like research or debugging. The costs are less predictability, more latency and spend, and errors that compound across steps.

More to think about
  • How would you stop an agent from running forever?
  • How do you control the cost of an agent?

Go further

Was this lesson clear?